blob: d67801dfa53e7cf1c2e8f9b8bb891b85947622a0 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004 *
5 * Ruby interface by Shugo Maeda
6 * with improvements by SegPhault (Ryan Paul)
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +02007 * with improvements by Jon Maken
Bram Moolenaar071d4272004-06-13 20:20:40 +00008 *
9 * Do ":help uganda" in Vim to read copying and usage conditions.
10 * Do ":help credits" in Vim to see a list of people who contributed.
11 * See README.txt for an overview of the Vim source code.
12 */
13
Bram Moolenaar2d73ff42010-10-27 17:40:59 +020014#ifdef HAVE_CONFIG_H
15# include "auto/config.h"
16#endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020017
Bram Moolenaare2793352011-01-17 19:53:27 +010018#include <stdio.h>
19#include <string.h>
20
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#ifdef _WIN32
22# if !defined(DYNAMIC_RUBY_VER) || (DYNAMIC_RUBY_VER < 18)
23# define NT
24# endif
25# ifndef DYNAMIC_RUBY
26# define IMPORT /* For static dll usage __declspec(dllimport) */
27# define RUBYEXTERN __declspec(dllimport)
28# endif
29#endif
30#ifndef RUBYEXTERN
31# define RUBYEXTERN extern
32#endif
33
Bram Moolenaar2016ae52016-06-13 20:08:43 +020034#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 24
35# define USE_RUBY_INTEGER
Bram Moolenaar06469e92016-06-11 22:26:53 +020036#endif
37
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020038#ifdef DYNAMIC_RUBY
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
40 * This is tricky. In ruby.h there is (inline) function rb_class_of()
41 * definition. This function use these variables. But we want function to
42 * use dll_* variables.
43 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000044# define rb_cFalseClass (*dll_rb_cFalseClass)
45# define rb_cFixnum (*dll_rb_cFixnum)
Bram Moolenaar2016ae52016-06-13 20:08:43 +020046# if defined(USE_RUBY_INTEGER)
47# define rb_cInteger (*dll_rb_cInteger)
Bram Moolenaar06469e92016-06-11 22:26:53 +020048# endif
Bram Moolenaardb3fbe52013-03-07 15:16:21 +010049# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
50# define rb_cFloat (*dll_rb_cFloat)
51# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# define rb_cNilClass (*dll_rb_cNilClass)
53# define rb_cSymbol (*dll_rb_cSymbol)
54# define rb_cTrueClass (*dll_rb_cTrueClass)
55# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
56/*
Bram Moolenaar42d57f02010-03-10 12:47:00 +010057 * On ver 1.8, all Ruby functions are exported with "__declspec(dllimport)"
58 * in ruby.h. But it causes trouble for these variables, because it is
Bram Moolenaar071d4272004-06-13 20:20:40 +000059 * defined in this file. When defined this RUBY_EXPORT it modified to
60 * "extern" and be able to avoid this problem.
61 */
62# define RUBY_EXPORT
63# endif
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020064
Bram Moolenaar2d73ff42010-10-27 17:40:59 +020065#if !(defined(WIN32) || defined(_WIN64))
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020066# include <dlfcn.h>
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020067# define HINSTANCE void*
68# define RUBY_PROC void*
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020069# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
70# define symbol_from_dll dlsym
71# define close_dll dlclose
72#else
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020073# define RUBY_PROC FARPROC
Bram Moolenaarebbcb822010-10-23 14:02:54 +020074# define load_dll vimLoadLib
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020075# define symbol_from_dll GetProcAddress
76# define close_dll FreeLibrary
Bram Moolenaar071d4272004-06-13 20:20:40 +000077#endif
78
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020079#endif /* ifdef DYNAMIC_RUBY */
80
Bram Moolenaar0b69c732010-02-17 15:11:50 +010081/* suggested by Ariya Mizutani */
82#if (_MSC_VER == 1200)
83# undef _WIN32_WINNT
84#endif
85
Bram Moolenaar639a2552010-03-17 18:15:23 +010086#if (defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
87 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
88# define RUBY19_OR_LATER 1
89#endif
90
Bram Moolenaar0d27f642015-12-28 22:05:28 +010091#if (defined(RUBY_VERSION) && RUBY_VERSION >= 20) \
92 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20)
93# define RUBY20_OR_LATER 1
94#endif
95
Bram Moolenaar42d57f02010-03-10 12:47:00 +010096#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
97/* Ruby 1.9 defines a number of static functions which use rb_num2long and
98 * rb_int2big */
99# define rb_num2long rb_num2long_stub
100# define rb_int2big rb_int2big_stub
101#endif
102
Bram Moolenaar498af702014-03-28 21:58:21 +0100103#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \
104 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
105/* Ruby 1.9 defines a number of static functions which use rb_fix2int and
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100106 * rb_num2int if VIM_SIZEOF_INT < VIM_SIZEOF_LONG (64bit) */
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200107# define rb_fix2int rb_fix2int_stub
108# define rb_num2int rb_num2int_stub
109#endif
110
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100111#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100112/* Ruby 2.1 adds new GC called RGenGC and RARRAY_PTR uses
113 * rb_gc_writebarrier_unprotect_promoted if USE_RGENGC */
Bram Moolenaar90140742014-11-27 17:44:08 +0100114# define rb_gc_writebarrier_unprotect_promoted rb_gc_writebarrier_unprotect_promoted_stub
115#endif
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100116#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
117# define rb_gc_writebarrier_unprotect rb_gc_writebarrier_unprotect_stub
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100118#endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100119
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#include <ruby.h>
Bram Moolenaar639a2552010-03-17 18:15:23 +0100121#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100122# include <ruby/encoding.h>
123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100125#undef off_t /* ruby defines off_t as _int64, Mingw uses long */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#undef EXTERN
127#undef _
128
129/* T_DATA defined both by Ruby and Mac header files, hack around it... */
Bram Moolenaard0573012017-10-28 21:11:06 +0200130#if defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131# define __OPENTRANSPORT__
132# define __OPENTRANSPORTPROTOCOL__
133# define __OPENTRANSPORTPROVIDERS__
134#endif
135
Bram Moolenaar165641d2010-02-17 16:23:09 +0100136/*
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100137 * The TypedData_XXX macro family can be used since Ruby 1.9.2 but
138 * rb_data_type_t changed in 1.9.3, therefore require at least 2.0.
139 * The old Data_XXX macro family was deprecated on Ruby 2.2.
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100140 * Use TypedData_XXX if available.
141 */
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100142#if defined(TypedData_Wrap_Struct) && defined(RUBY20_OR_LATER)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100143# define USE_TYPEDDATA 1
144#endif
145
146/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200147 * Backward compatibility for Ruby 1.8 and earlier.
Bram Moolenaar165641d2010-02-17 16:23:09 +0100148 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
149 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
150 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
151 */
152#ifndef StringValuePtr
153# define StringValuePtr(s) STR2CSTR(s)
154#endif
155#ifndef RARRAY_LEN
156# define RARRAY_LEN(s) RARRAY(s)->len
157#endif
158#ifndef RARRAY_PTR
159# define RARRAY_PTR(s) RARRAY(s)->ptr
160#endif
161#ifndef RSTRING_LEN
162# define RSTRING_LEN(s) RSTRING(s)->len
163#endif
164#ifndef RSTRING_PTR
165# define RSTRING_PTR(s) RSTRING(s)->ptr
166#endif
167
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100168#ifdef HAVE_DUP
169# undef HAVE_DUP
170#endif
171
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#include "vim.h"
173#include "version.h"
174
175#if defined(PROTO) && !defined(FEAT_RUBY)
176/* Define these to be able to generate the function prototypes. */
177# define VALUE int
178# define RUBY_DATA_FUNC int
179#endif
180
181static int ruby_initialized = 0;
Bram Moolenaar99685e62013-05-11 13:56:18 +0200182static void *ruby_stack_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183static VALUE objtbl;
184
185static VALUE mVIM;
186static VALUE cBuffer;
187static VALUE cVimWindow;
188static VALUE eDeletedBufferError;
189static VALUE eDeletedWindowError;
190
191static int ensure_ruby_initialized(void);
192static void error_print(int);
193static void ruby_io_init(void);
194static void ruby_vim_init(void);
195
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200196#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
197# if defined(__ia64) && !defined(ruby_init_stack)
198# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
199# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200#endif
201
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200202#if defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaaref269542016-01-19 13:22:12 +0100203# if defined(PROTO) && !defined(HINSTANCE)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200204# define HINSTANCE int /* for generating prototypes */
205# endif
206
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207/*
208 * Wrapper defines
209 */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200210# define rb_assoc_new dll_rb_assoc_new
211# define rb_cObject (*dll_rb_cObject)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100212# define rb_check_type dll_rb_check_type
213# ifdef USE_TYPEDDATA
214# define rb_check_typeddata dll_rb_check_typeddata
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100215# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200216# define rb_class_path dll_rb_class_path
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100217# ifdef USE_TYPEDDATA
218# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
219# define rb_data_typed_object_wrap dll_rb_data_typed_object_wrap
220# else
221# define rb_data_typed_object_alloc dll_rb_data_typed_object_alloc
222# endif
223# else
224# define rb_data_object_alloc dll_rb_data_object_alloc
225# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200226# define rb_define_class_under dll_rb_define_class_under
227# define rb_define_const dll_rb_define_const
228# define rb_define_global_function dll_rb_define_global_function
229# define rb_define_method dll_rb_define_method
230# define rb_define_module dll_rb_define_module
231# define rb_define_module_function dll_rb_define_module_function
232# define rb_define_singleton_method dll_rb_define_singleton_method
233# define rb_define_virtual_variable dll_rb_define_virtual_variable
234# define rb_stdout (*dll_rb_stdout)
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200235# define rb_stderr (*dll_rb_stderr)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200236# define rb_eArgError (*dll_rb_eArgError)
237# define rb_eIndexError (*dll_rb_eIndexError)
238# define rb_eRuntimeError (*dll_rb_eRuntimeError)
239# define rb_eStandardError (*dll_rb_eStandardError)
240# define rb_eval_string_protect dll_rb_eval_string_protect
241# define rb_global_variable dll_rb_global_variable
242# define rb_hash_aset dll_rb_hash_aset
243# define rb_hash_new dll_rb_hash_new
244# define rb_inspect dll_rb_inspect
245# define rb_int2inum dll_rb_int2inum
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100246# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar498af702014-03-28 21:58:21 +0100247# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 18
248# define rb_fix2int dll_rb_fix2int
249# define rb_num2int dll_rb_num2int
250# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200251# define rb_num2uint dll_rb_num2uint
252# endif
253# define rb_lastline_get dll_rb_lastline_get
254# define rb_lastline_set dll_rb_lastline_set
Bram Moolenaard0573012017-10-28 21:11:06 +0200255# define rb_protect dll_rb_protect
256# define rb_load dll_rb_load
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200257# ifndef RUBY19_OR_LATER
258# define rb_num2long dll_rb_num2long
259# endif
260# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 19
261# define rb_num2ulong dll_rb_num2ulong
262# endif
263# define rb_obj_alloc dll_rb_obj_alloc
264# define rb_obj_as_string dll_rb_obj_as_string
265# define rb_obj_id dll_rb_obj_id
266# define rb_raise dll_rb_raise
267# define rb_str_cat dll_rb_str_cat
268# define rb_str_concat dll_rb_str_concat
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100269# undef rb_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200270# define rb_str_new dll_rb_str_new
271# ifdef rb_str_new2
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200272/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200273# define need_rb_str_new_cstr 1
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200274/* Ruby's headers #define rb_str_new_cstr to make use of GCC's
275 * __builtin_constant_p extension. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200276# undef rb_str_new_cstr
277# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200278# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200279# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200280# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200281# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200282# define rb_string_value dll_rb_string_value
283# define rb_string_value_ptr dll_rb_string_value_ptr
284# define rb_float_new dll_rb_float_new
285# define rb_ary_new dll_rb_ary_new
286# define rb_ary_push dll_rb_ary_push
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200287# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
288# ifdef __ia64
289# define rb_ia64_bsp dll_rb_ia64_bsp
290# undef ruby_init_stack
291# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
292# else
293# define ruby_init_stack dll_ruby_init_stack
294# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200295# endif
296# else
297# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200298# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200299# ifdef RUBY19_OR_LATER
300# define rb_errinfo dll_rb_errinfo
301# else
302# define ruby_errinfo (*dll_ruby_errinfo)
303# endif
304# define ruby_init dll_ruby_init
305# define ruby_init_loadpath dll_ruby_init_loadpath
306# ifdef WIN3264
307# define NtInitialize dll_NtInitialize
Bram Moolenaar4f391792017-01-15 16:59:07 +0100308# define ruby_sysinit dll_ruby_sysinit
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200309# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
310# define rb_w32_snprintf dll_rb_w32_snprintf
311# endif
312# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200314# ifdef RUBY19_OR_LATER
315# define ruby_script dll_ruby_script
316# define rb_enc_find_index dll_rb_enc_find_index
317# define rb_enc_find dll_rb_enc_find
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100318# undef rb_enc_str_new
319# define rb_enc_str_new dll_rb_enc_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200320# define rb_sprintf dll_rb_sprintf
321# define rb_require dll_rb_require
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100322# define ruby_options dll_ruby_options
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200323# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100324
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325/*
326 * Pointers for dynamic link
327 */
328static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200329VALUE *dll_rb_cFalseClass;
330VALUE *dll_rb_cFixnum;
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200331# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200332VALUE *dll_rb_cInteger;
333# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200334# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100335VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200336# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200337VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338static VALUE *dll_rb_cObject;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200339VALUE *dll_rb_cSymbol;
340VALUE *dll_rb_cTrueClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341static void (*dll_rb_check_type) (VALUE,int);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100342# ifdef USE_TYPEDDATA
343static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *);
344# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345static VALUE (*dll_rb_class_path) (VALUE);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100346# ifdef USE_TYPEDDATA
347# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
348static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *);
349# else
350static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *);
351# endif
352# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100354# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
356static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
357static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
358static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
359static VALUE (*dll_rb_define_module) (const char*);
360static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
361static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
362static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
363static VALUE *dll_rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200364static VALUE *dll_rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365static VALUE *dll_rb_eArgError;
366static VALUE *dll_rb_eIndexError;
367static VALUE *dll_rb_eRuntimeError;
368static VALUE *dll_rb_eStandardError;
369static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
370static void (*dll_rb_global_variable) (VALUE*);
371static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
372static VALUE (*dll_rb_hash_new) (void);
373static VALUE (*dll_rb_inspect) (VALUE);
374static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100375# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200376static long (*dll_rb_fix2int) (VALUE);
377static long (*dll_rb_num2int) (VALUE);
378static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200379# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380static VALUE (*dll_rb_lastline_get) (void);
381static void (*dll_rb_lastline_set) (VALUE);
Bram Moolenaar37badc82018-01-31 20:15:30 +0100382static VALUE (*dll_rb_protect) (VALUE (*)(VALUE), VALUE, int*);
Bram Moolenaard0573012017-10-28 21:11:06 +0200383static void (*dll_rb_load) (VALUE, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384static long (*dll_rb_num2long) (VALUE);
385static unsigned long (*dll_rb_num2ulong) (VALUE);
386static VALUE (*dll_rb_obj_alloc) (VALUE);
387static VALUE (*dll_rb_obj_as_string) (VALUE);
388static VALUE (*dll_rb_obj_id) (VALUE);
389static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200390# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200391static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200392# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200394# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
396static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
397static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200398# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200399/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
400static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200401# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200403# endif
404# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100405static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200406# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200408# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409static void (*dll_ruby_init) (void);
410static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200411# ifdef WIN3264
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100412static void (*dll_NtInitialize) (int*, char***);
Bram Moolenaar4f391792017-01-15 16:59:07 +0100413static void (*dll_ruby_sysinit) (int*, char***);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200414# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200415static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200416# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200417# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200418# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100419static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
420static VALUE (*dll_rb_float_new) (double);
421static VALUE (*dll_rb_ary_new) (void);
422static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200423# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
424# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200425static void * (*dll_rb_ia64_bsp) (void);
426static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200427# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200428static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200429# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200430# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200431# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200432# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100433static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200434# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200436# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100437static void (*dll_ruby_script) (const char*);
438static int (*dll_rb_enc_find_index) (const char*);
439static rb_encoding* (*dll_rb_enc_find) (const char*);
440static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
441static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100442static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100443static void* (*ruby_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200444# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100445
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100446# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100447# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100448static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100449# else
450static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
451# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100452# endif
453
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200454# if defined(RUBY19_OR_LATER) && !defined(PROTO)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200455# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
456long rb_num2long_stub(VALUE x)
457# else
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100458SIGNED_VALUE rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200459# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100460{
461 return dll_rb_num2long(x);
462}
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100463VALUE rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100464{
465 return dll_rb_int2big(x);
466}
Bram Moolenaar498af702014-03-28 21:58:21 +0100467# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \
468 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200469long rb_fix2int_stub(VALUE x)
470{
471 return dll_rb_fix2int(x);
472}
473long rb_num2int_stub(VALUE x)
474{
475 return dll_rb_num2int(x);
476}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200477# endif
478# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaar886ed692013-02-26 13:41:35 +0100479VALUE
480rb_float_new_in_heap(double d)
481{
482 return dll_rb_float_new(d);
483}
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200484# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
485unsigned long rb_num2ulong(VALUE x)
486# else
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100487VALUE rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200488# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100489{
490 return (long)RSHIFT((SIGNED_VALUE)(x),1);
491}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200492# endif
493# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100494
Bram Moolenaar3e9a1612014-11-12 16:05:04 +0100495 /* Do not generate a prototype here, VALUE isn't always defined. */
496# if defined(USE_RGENGC) && USE_RGENGC && !defined(PROTO)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100497# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100498void rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
499{
Bram Moolenaar90140742014-11-27 17:44:08 +0100500 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100501}
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100502# else
503void rb_gc_writebarrier_unprotect_stub(VALUE obj)
504{
505 dll_rb_gc_writebarrier_unprotect(obj);
506}
507# endif
508# endif
509
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200510static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511
512/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000513 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515static struct
516{
517 char *name;
518 RUBY_PROC *ptr;
519} ruby_funcname_table[] =
520{
521 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
522 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200523# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200524 {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger},
Bram Moolenaar6abda992017-01-09 21:10:31 +0100525# else
526 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar06469e92016-06-11 22:26:53 +0200527# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200528# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100529 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200530# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
532 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
533 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
534 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
535 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100536# ifdef USE_TYPEDDATA
537 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
538# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100540# ifdef USE_TYPEDDATA
541# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
542 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
543# else
544 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
545# endif
546# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100548# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
550 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
551 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
552 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
553 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
554 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
555 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
556 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
557 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200558 {"rb_stderr", (RUBY_PROC*)&dll_rb_stderr},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
560 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
561 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
562 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
563 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
564 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
565 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
566 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
567 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
568 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100569# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200570 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
571 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
572 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200573# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
575 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
Bram Moolenaard0573012017-10-28 21:11:06 +0200576 {"rb_protect", (RUBY_PROC*)&dll_rb_protect},
577 {"rb_load", (RUBY_PROC*)&dll_rb_load},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
579 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
580 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
581 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
582 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
583 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200584# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200585 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200586# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200588# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
590 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
591 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200592# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200593 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200594# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200596# endif
597# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100598 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200599# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200601# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
603 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200604# ifdef WIN3264
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200605# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER < 19
Bram Moolenaar4f391792017-01-15 16:59:07 +0100606 {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200607# else
Bram Moolenaar4f391792017-01-15 16:59:07 +0100608 {"ruby_sysinit", (RUBY_PROC*)&dll_ruby_sysinit},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200609# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200610# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200612# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200613# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200614# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100615 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200616# if DYNAMIC_RUBY_VER <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100617 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200618# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100619 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200620# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100621 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
622 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200623# endif
624# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100625 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100626 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
627 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
628 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
629 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
630 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100631 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100632 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200633# endif
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200634# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
635# ifdef __ia64
636 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
637# endif
638 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
639# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100640# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100641# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100642 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100643# else
644 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
645# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100646# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 {"", NULL},
648};
649
650/*
651 * Free ruby.dll
652 */
653 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100654end_dynamic_ruby(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655{
656 if (hinstRuby)
657 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200658 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200659 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 }
661}
662
663/*
664 * Load library and get all pointers.
665 * Parameter 'libname' provides name of DLL.
666 * Return OK or FAIL.
667 */
668 static int
669ruby_runtime_link_init(char *libname, int verbose)
670{
671 int i;
672
673 if (hinstRuby)
674 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200675 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 if (!hinstRuby)
677 {
678 if (verbose)
679 EMSG2(_(e_loadlib), libname);
680 return FAIL;
681 }
682
683 for (i = 0; ruby_funcname_table[i].ptr; ++i)
684 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200685 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 ruby_funcname_table[i].name)))
687 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200688 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200689 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 if (verbose)
691 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
692 return FAIL;
693 }
694 }
695 return OK;
696}
697
698/*
699 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
700 * else FALSE.
701 */
702 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100703ruby_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100705 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706}
707#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
708
709 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100710ruby_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711{
712#ifdef DYNAMIC_RUBY
713 end_dynamic_ruby();
714#endif
715}
716
717void ex_ruby(exarg_T *eap)
718{
719 int state;
720 char *script = NULL;
721
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000722 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 if (!eap->skip && ensure_ruby_initialized())
724 {
725 if (script == NULL)
726 rb_eval_string_protect((char *)eap->arg, &state);
727 else
728 rb_eval_string_protect(script, &state);
729 if (state)
730 error_print(state);
731 }
732 vim_free(script);
733}
734
Bram Moolenaar165641d2010-02-17 16:23:09 +0100735/*
736 * In Ruby 1.9 or later, ruby String object has encoding.
737 * conversion buffer string of vim to ruby String object using
738 * VIM encoding option.
739 */
740 static VALUE
741vim_str2rb_enc_str(const char *s)
742{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100743#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100744 int isnum;
745 long lval;
746 char_u *sval;
747 rb_encoding *enc;
748
749 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
750 if (isnum == 0)
751 {
752 enc = rb_enc_find((char *)sval);
753 vim_free(sval);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200754 if (enc)
755 {
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200756 return rb_enc_str_new(s, (long)strlen(s), enc);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100757 }
758 }
759#endif
760 return rb_str_new2(s);
761}
762
763 static VALUE
764eval_enc_string_protect(const char *str, int *state)
765{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100766#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100767 int isnum;
768 long lval;
769 char_u *sval;
770 rb_encoding *enc;
771 VALUE v;
772
773 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
774 if (isnum == 0)
775 {
776 enc = rb_enc_find((char *)sval);
777 vim_free(sval);
778 if (enc)
779 {
780 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
781 return rb_eval_string_protect(StringValuePtr(v), state);
782 }
783 }
784#endif
785 return rb_eval_string_protect(str, state);
786}
787
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788void ex_rubydo(exarg_T *eap)
789{
790 int state;
791 linenr_T i;
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100792 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793
794 if (ensure_ruby_initialized())
795 {
796 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
797 return;
Bram Moolenaar758535a2016-03-30 22:06:16 +0200798 for (i = eap->line1; i <= eap->line2; i++)
799 {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100800 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100802 if (i > curbuf->b_ml.ml_line_count)
803 break;
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100804 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100806 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200807 if (state)
808 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 error_print(state);
810 break;
811 }
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100812 if (was_curbuf != curbuf)
813 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 line = rb_lastline_get();
Bram Moolenaar758535a2016-03-30 22:06:16 +0200815 if (!NIL_P(line))
816 {
817 if (TYPE(line) != T_STRING)
818 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000819 EMSG(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 return;
821 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100822 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 changed();
824#ifdef SYNTAX_HL
825 syn_changed(i); /* recompute syntax hl. for this line */
826#endif
827 }
828 }
829 check_cursor();
830 update_curbuf(NOT_VALID);
831 }
832}
833
Bram Moolenaar0b394642018-05-17 13:11:46 +0200834static VALUE rb_load_wrap(VALUE file_to_load)
Bram Moolenaar37badc82018-01-31 20:15:30 +0100835{
836 rb_load(file_to_load, 0);
837 return Qnil;
838}
839
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840void ex_rubyfile(exarg_T *eap)
841{
842 int state;
843
844 if (ensure_ruby_initialized())
845 {
Bram Moolenaar37badc82018-01-31 20:15:30 +0100846 VALUE file_to_load = rb_str_new2((const char *)eap->arg);
847 rb_protect(rb_load_wrap, file_to_load, &state);
848 if (state)
849 error_print(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 }
851}
852
853void ruby_buffer_free(buf_T *buf)
854{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000855 if (buf->b_ruby_ref)
856 {
857 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
858 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 }
860}
861
862void ruby_window_free(win_T *win)
863{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000864 if (win->w_ruby_ref)
865 {
866 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
867 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 }
869}
870
871static int ensure_ruby_initialized(void)
872{
873 if (!ruby_initialized)
874 {
875#ifdef DYNAMIC_RUBY
876 if (ruby_enabled(TRUE))
877 {
878#endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100879#ifdef _WIN32
880 /* suggested by Ariya Mizutani */
881 int argc = 1;
882 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +0100883 char **argvp = argv;
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100884# ifdef RUBY19_OR_LATER
885 ruby_sysinit(&argc, &argvp);
886# else
Bram Moolenaar90140742014-11-27 17:44:08 +0100887 NtInitialize(&argc, &argvp);
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100888# endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100889#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200890 {
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200891#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +0200892 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100893#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200894 ruby_init();
895 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100896#ifdef RUBY19_OR_LATER
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100897 {
898 int dummy_argc = 2;
Bram Moolenaard1bc96ce2017-09-26 21:21:44 +0200899 char *dummy_argv[] = {"vim-ruby", "-e_=0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100900 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100901 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100902 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100903#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100905#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100906 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 ruby_vim_init();
908 ruby_initialized = 1;
909#ifdef DYNAMIC_RUBY
910 }
911 else
912 {
913 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
914 return 0;
915 }
916#endif
917 }
918 return ruby_initialized;
919}
920
921static void error_print(int state)
922{
923#ifndef DYNAMIC_RUBY
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100924#if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
925 && !(defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 RUBYEXTERN VALUE ruby_errinfo;
927#endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 VALUE eclass;
930 VALUE einfo;
931 char buff[BUFSIZ];
932
933#define TAG_RETURN 0x1
934#define TAG_BREAK 0x2
935#define TAG_NEXT 0x3
936#define TAG_RETRY 0x4
937#define TAG_REDO 0x5
938#define TAG_RAISE 0x6
939#define TAG_THROW 0x7
940#define TAG_FATAL 0x8
941#define TAG_MASK 0xf
942
Bram Moolenaar758535a2016-03-30 22:06:16 +0200943 switch (state)
944 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 case TAG_RETURN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000946 EMSG(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 break;
948 case TAG_NEXT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000949 EMSG(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 break;
951 case TAG_BREAK:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000952 EMSG(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 break;
954 case TAG_REDO:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000955 EMSG(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 break;
957 case TAG_RETRY:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000958 EMSG(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 break;
960 case TAG_RAISE:
961 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +0100962#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100963 eclass = CLASS_OF(rb_errinfo());
964 einfo = rb_obj_as_string(rb_errinfo());
965#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 eclass = CLASS_OF(ruby_errinfo);
967 einfo = rb_obj_as_string(ruby_errinfo);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100968#endif
Bram Moolenaar758535a2016-03-30 22:06:16 +0200969 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
970 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000971 EMSG(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 }
Bram Moolenaar758535a2016-03-30 22:06:16 +0200973 else
974 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 VALUE epath;
976 char *p;
977
978 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000979 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar165641d2010-02-17 16:23:09 +0100980 RSTRING_PTR(epath), RSTRING_PTR(einfo));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 p = strchr(buff, '\n');
982 if (p) *p = '\0';
983 EMSG(buff);
984 }
985 break;
986 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000987 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 EMSG(buff);
989 break;
990 }
991}
992
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000993static VALUE vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994{
995 char *buff, *p;
996
997 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +0200998 if (RSTRING_LEN(str) > 0)
999 {
1000 /* Only do this when the string isn't empty, alloc(0) causes trouble. */
Bram Moolenaar00ccf542017-09-03 15:17:48 +02001001 buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001002 strcpy(buff, RSTRING_PTR(str));
1003 p = strchr(buff, '\n');
1004 if (p) *p = '\0';
1005 MSG(buff);
1006 }
1007 else
1008 {
1009 MSG("");
1010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 return Qnil;
1012}
1013
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001014static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001016 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 update_screen(NOT_VALID);
1018 return Qnil;
1019}
1020
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001021static VALUE vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001023 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 return Qnil;
1025}
1026
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001027#ifdef FEAT_EVAL
1028static VALUE vim_to_ruby(typval_T *tv)
1029{
1030 VALUE result = Qnil;
1031
1032 if (tv->v_type == VAR_STRING)
1033 {
Bram Moolenaar94127e42010-03-19 23:08:48 +01001034 result = rb_str_new2(tv->vval.v_string == NULL
1035 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001036 }
1037 else if (tv->v_type == VAR_NUMBER)
1038 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001039 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001040 }
1041# ifdef FEAT_FLOAT
1042 else if (tv->v_type == VAR_FLOAT)
1043 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001044 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001045 }
1046# endif
1047 else if (tv->v_type == VAR_LIST)
1048 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001049 list_T *list = tv->vval.v_list;
1050 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001051
Bram Moolenaar639a2552010-03-17 18:15:23 +01001052 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001053
Bram Moolenaar639a2552010-03-17 18:15:23 +01001054 if (list != NULL)
1055 {
1056 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
1057 {
1058 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
1059 }
1060 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001061 }
1062 else if (tv->v_type == VAR_DICT)
1063 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001064 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001065
Bram Moolenaar639a2552010-03-17 18:15:23 +01001066 if (tv->vval.v_dict != NULL)
1067 {
1068 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1069 long_u todo = ht->ht_used;
1070 hashitem_T *hi;
1071 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001072
Bram Moolenaar639a2552010-03-17 18:15:23 +01001073 for (hi = ht->ht_array; todo > 0; ++hi)
1074 {
1075 if (!HASHITEM_EMPTY(hi))
1076 {
1077 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001078
Bram Moolenaar639a2552010-03-17 18:15:23 +01001079 di = dict_lookup(hi);
1080 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001081 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001082 }
1083 }
1084 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001085 }
1086 else if (tv->v_type == VAR_SPECIAL)
1087 {
1088 if (tv->vval.v_number <= VVAL_TRUE)
1089 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001090 } /* else return Qnil; */
1091
1092 return result;
1093}
1094#endif
1095
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001096static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097{
1098#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001099 typval_T *tv;
1100 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001102 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1103 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001105 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001107 result = vim_to_ruby(tv);
1108
1109 free_tv(tv);
1110
1111 return result;
1112#else
1113 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115}
1116
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001117#ifdef USE_TYPEDDATA
1118static size_t buffer_dsize(const void *buf);
1119
1120static const rb_data_type_t buffer_type = {
1121 "vim_buffer",
1122 {0, 0, buffer_dsize, {0, 0}},
1123 0, 0,
1124# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1125 0,
1126# endif
1127};
1128
1129static size_t buffer_dsize(const void *buf UNUSED)
1130{
1131 return sizeof(buf_T);
1132}
1133#endif
1134
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135static VALUE buffer_new(buf_T *buf)
1136{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001137 if (buf->b_ruby_ref)
1138 {
1139 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001141 else
1142 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001143#ifdef USE_TYPEDDATA
1144 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1145#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001147#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001148 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1150 return obj;
1151 }
1152}
1153
1154static buf_T *get_buf(VALUE obj)
1155{
1156 buf_T *buf;
1157
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001158#ifdef USE_TYPEDDATA
1159 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1160#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 if (buf == NULL)
1164 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1165 return buf;
1166}
1167
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001168static VALUE buffer_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169{
1170 return buffer_new(curbuf);
1171}
1172
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001173static VALUE buffer_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174{
1175 buf_T *b;
1176 int n = 0;
1177
Bram Moolenaar29323592016-07-24 22:04:11 +02001178 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001179 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001180 /* Deleted buffers should not be counted
1181 * SegPhault - 01/07/05 */
1182 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001183 n++;
1184 }
1185
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 return INT2NUM(n);
1187}
1188
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001189static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190{
1191 buf_T *b;
1192 int n = NUM2INT(num);
1193
Bram Moolenaar29323592016-07-24 22:04:11 +02001194 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001195 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001196 /* Deleted buffers should not be counted
1197 * SegPhault - 01/07/05 */
1198 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001199 continue;
1200
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001201 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001203
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001204 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 }
1206 return Qnil;
1207}
1208
1209static VALUE buffer_name(VALUE self)
1210{
1211 buf_T *buf = get_buf(self);
1212
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001213 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214}
1215
1216static VALUE buffer_number(VALUE self)
1217{
1218 buf_T *buf = get_buf(self);
1219
1220 return INT2NUM(buf->b_fnum);
1221}
1222
1223static VALUE buffer_count(VALUE self)
1224{
1225 buf_T *buf = get_buf(self);
1226
1227 return INT2NUM(buf->b_ml.ml_line_count);
1228}
1229
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001230static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001232 if (n <= 0 || n > buf->b_ml.ml_line_count)
1233 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1234 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235}
1236
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001237static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238{
1239 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001240
1241 if (buf != NULL)
1242 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
1243 return Qnil; /* For stop warning */
1244}
1245
1246static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
1247{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001248 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001249 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001251 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1252 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001253 /* set curwin/curbuf for "buf" and save some things */
1254 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001255
Bram Moolenaar758535a2016-03-30 22:06:16 +02001256 if (u_savesub(n) == OK)
1257 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001258 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 changed();
1260#ifdef SYNTAX_HL
1261 syn_changed(n); /* recompute syntax hl. for this line */
1262#endif
1263 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001264
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001265 /* restore curwin/curbuf and a few other things */
1266 aucmd_restbuf(&aco);
1267 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001268
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 update_curbuf(NOT_VALID);
1270 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001271 else
1272 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001273 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
1275 return str;
1276}
1277
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001278static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
1279{
1280 buf_T *buf = get_buf(self);
1281
1282 if (buf != NULL)
1283 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1284 return str;
1285}
1286
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287static VALUE buffer_delete(VALUE self, VALUE num)
1288{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001289 buf_T *buf = get_buf(self);
1290 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001291 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001293 if (n > 0 && n <= buf->b_ml.ml_line_count)
1294 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001295 /* set curwin/curbuf for "buf" and save some things */
1296 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001297
Bram Moolenaar758535a2016-03-30 22:06:16 +02001298 if (u_savedel(n, 1) == OK)
1299 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001301
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001302 /* Changes to non-active buffers should properly refresh
1303 * SegPhault - 01/09/05 */
1304 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001305
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 changed();
1307 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001308
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001309 /* restore curwin/curbuf and a few other things */
1310 aucmd_restbuf(&aco);
1311 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001312
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 update_curbuf(NOT_VALID);
1314 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001315 else
1316 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001317 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 }
1319 return Qnil;
1320}
1321
1322static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
1323{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001324 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001325 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001326 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001327 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328
Bram Moolenaar3c531602010-11-16 14:46:19 +01001329 if (line == NULL)
1330 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001331 rb_raise(rb_eIndexError, "NULL line");
1332 }
1333 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001334 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001335 /* set curwin/curbuf for "buf" and save some things */
1336 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001337
Bram Moolenaar758535a2016-03-30 22:06:16 +02001338 if (u_inssub(n + 1) == OK)
1339 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001341
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001342 /* Changes to non-active buffers should properly refresh screen
1343 * SegPhault - 12/20/04 */
1344 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001345
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001346 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001348
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001349 /* restore curwin/curbuf and a few other things */
1350 aucmd_restbuf(&aco);
1351 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001352
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 update_curbuf(NOT_VALID);
1354 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001355 else
1356 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001357 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 }
1359 return str;
1360}
1361
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001362#ifdef USE_TYPEDDATA
1363static size_t window_dsize(const void *buf);
1364
1365static const rb_data_type_t window_type = {
1366 "vim_window",
1367 {0, 0, window_dsize, {0, 0}},
1368 0, 0,
1369# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1370 0,
1371# endif
1372};
1373
1374static size_t window_dsize(const void *win UNUSED)
1375{
1376 return sizeof(win_T);
1377}
1378#endif
1379
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380static VALUE window_new(win_T *win)
1381{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001382 if (win->w_ruby_ref)
1383 {
1384 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001386 else
1387 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001388#ifdef USE_TYPEDDATA
1389 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1390#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001392#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001393 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1395 return obj;
1396 }
1397}
1398
1399static win_T *get_win(VALUE obj)
1400{
1401 win_T *win;
1402
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001403#ifdef USE_TYPEDDATA
1404 TypedData_Get_Struct(obj, win_T, &window_type, win);
1405#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 if (win == NULL)
1409 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1410 return win;
1411}
1412
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001413static VALUE window_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414{
1415 return window_new(curwin);
1416}
1417
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001418/*
1419 * Added line manipulation functions
1420 * SegPhault - 03/07/05
1421 */
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001422static VALUE line_s_current(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001423{
1424 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1425}
1426
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001427static VALUE set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001428{
1429 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1430}
1431
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001432static VALUE current_line_number(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001433{
1434 return INT2FIX((int)curwin->w_cursor.lnum);
1435}
1436
1437
1438
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001439static VALUE window_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 win_T *w;
1442 int n = 0;
1443
Bram Moolenaar29323592016-07-24 22:04:11 +02001444 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 n++;
1446 return INT2NUM(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447}
1448
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001449static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450{
1451 win_T *w;
1452 int n = NUM2INT(num);
1453
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 for (w = firstwin; w != NULL; w = w->w_next, --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 if (n == 0)
1456 return window_new(w);
1457 return Qnil;
1458}
1459
1460static VALUE window_buffer(VALUE self)
1461{
1462 win_T *win = get_win(self);
1463
1464 return buffer_new(win->w_buffer);
1465}
1466
1467static VALUE window_height(VALUE self)
1468{
1469 win_T *win = get_win(self);
1470
1471 return INT2NUM(win->w_height);
1472}
1473
1474static VALUE window_set_height(VALUE self, VALUE height)
1475{
1476 win_T *win = get_win(self);
1477 win_T *savewin = curwin;
1478
1479 curwin = win;
1480 win_setheight(NUM2INT(height));
1481 curwin = savewin;
1482 return height;
1483}
1484
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001485static VALUE window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001486{
Bram Moolenaare745d752017-09-22 16:56:22 +02001487 return INT2NUM(get_win(self)->w_width);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001488}
1489
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001490static VALUE window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001491{
1492 win_T *win = get_win(self);
1493 win_T *savewin = curwin;
1494
1495 curwin = win;
1496 win_setwidth(NUM2INT(width));
1497 curwin = savewin;
1498 return width;
1499}
1500
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501static VALUE window_cursor(VALUE self)
1502{
1503 win_T *win = get_win(self);
1504
1505 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1506}
1507
1508static VALUE window_set_cursor(VALUE self, VALUE pos)
1509{
1510 VALUE lnum, col;
1511 win_T *win = get_win(self);
1512
1513 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001514 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001516 lnum = RARRAY_PTR(pos)[0];
1517 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 win->w_cursor.lnum = NUM2LONG(lnum);
1519 win->w_cursor.col = NUM2UINT(col);
1520 check_cursor(); /* put cursor on an existing line */
1521 update_screen(NOT_VALID);
1522 return Qnil;
1523}
1524
Bram Moolenaar6217cdc2012-04-25 12:28:09 +02001525static VALUE f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001526{
1527 return Qnil;
1528}
1529
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001530static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531{
1532 int i;
1533 VALUE str = rb_str_new("", 0);
1534
Bram Moolenaar758535a2016-03-30 22:06:16 +02001535 for (i = 0; i < argc; i++)
1536 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 if (i > 0) rb_str_cat(str, ", ", 2);
1538 rb_str_concat(str, rb_inspect(argv[i]));
1539 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001540 MSG(RSTRING_PTR(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 return Qnil;
1542}
1543
1544static void ruby_io_init(void)
1545{
1546#ifndef DYNAMIC_RUBY
1547 RUBYEXTERN VALUE rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001548 RUBYEXTERN VALUE rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549#endif
1550
1551 rb_stdout = rb_obj_alloc(rb_cObject);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001552 rb_stderr = rb_obj_alloc(rb_cObject);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001554 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001555 rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1556 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 rb_define_global_function("p", f_p, -1);
1558}
1559
1560static void ruby_vim_init(void)
1561{
1562 objtbl = rb_hash_new();
1563 rb_global_variable(&objtbl);
1564
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001565 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001566 * alias "VIM" for backwards compatibility. */
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001567 mVIM = rb_define_module("Vim");
1568 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1570 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1571 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1572 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1573 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1574 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1575 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1576 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1577 rb_define_module_function(mVIM, "message", vim_message, 1);
1578 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1579 rb_define_module_function(mVIM, "command", vim_command, 1);
1580 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
1581
1582 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1583 rb_eStandardError);
1584 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1585 rb_eStandardError);
1586
1587 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1588 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1589 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1590 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1591 rb_define_method(cBuffer, "name", buffer_name, 0);
1592 rb_define_method(cBuffer, "number", buffer_number, 0);
1593 rb_define_method(cBuffer, "count", buffer_count, 0);
1594 rb_define_method(cBuffer, "length", buffer_count, 0);
1595 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1596 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1597 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1598 rb_define_method(cBuffer, "append", buffer_append, 2);
1599
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001600 /* Added line manipulation functions
1601 * SegPhault - 03/07/05 */
1602 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1603 rb_define_method(cBuffer, "line", line_s_current, 0);
1604 rb_define_method(cBuffer, "line=", set_current_line, 1);
1605
1606
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1608 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1609 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1610 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1611 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1612 rb_define_method(cVimWindow, "height", window_height, 0);
1613 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001614 rb_define_method(cVimWindow, "width", window_width, 0);
1615 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1617 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1618
1619 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1620 rb_define_virtual_variable("$curwin", window_s_current, 0);
1621}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001622
1623void vim_ruby_init(void *stack_start)
1624{
1625 /* should get machine stack start address early in main function */
1626 ruby_stack_start = stack_start;
1627}