blob: 34fc0ed1cb310cbec2ce814e341a67732bd1f18d [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 Moolenaarb09c6842018-12-27 22:11:01 +0100126#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 26
127# define rb_ary_detransient (*dll_rb_ary_detransient)
128#endif
129
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#include <ruby.h>
Bram Moolenaar639a2552010-03-17 18:15:23 +0100131#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100132# include <ruby/encoding.h>
133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100135#undef off_t /* ruby defines off_t as _int64, Mingw uses long */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136#undef EXTERN
137#undef _
138
139/* T_DATA defined both by Ruby and Mac header files, hack around it... */
Bram Moolenaard0573012017-10-28 21:11:06 +0200140#if defined(MACOS_X)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141# define __OPENTRANSPORT__
142# define __OPENTRANSPORTPROTOCOL__
143# define __OPENTRANSPORTPROVIDERS__
144#endif
145
Bram Moolenaar165641d2010-02-17 16:23:09 +0100146/*
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100147 * The TypedData_XXX macro family can be used since Ruby 1.9.2 but
148 * rb_data_type_t changed in 1.9.3, therefore require at least 2.0.
149 * The old Data_XXX macro family was deprecated on Ruby 2.2.
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100150 * Use TypedData_XXX if available.
151 */
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100152#if defined(TypedData_Wrap_Struct) && defined(RUBY20_OR_LATER)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100153# define USE_TYPEDDATA 1
154#endif
155
156/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200157 * Backward compatibility for Ruby 1.8 and earlier.
Bram Moolenaar165641d2010-02-17 16:23:09 +0100158 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
159 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
160 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
161 */
162#ifndef StringValuePtr
163# define StringValuePtr(s) STR2CSTR(s)
164#endif
165#ifndef RARRAY_LEN
166# define RARRAY_LEN(s) RARRAY(s)->len
167#endif
168#ifndef RARRAY_PTR
169# define RARRAY_PTR(s) RARRAY(s)->ptr
170#endif
171#ifndef RSTRING_LEN
172# define RSTRING_LEN(s) RSTRING(s)->len
173#endif
174#ifndef RSTRING_PTR
175# define RSTRING_PTR(s) RSTRING(s)->ptr
176#endif
177
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100178#ifdef HAVE_DUP
179# undef HAVE_DUP
180#endif
181
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#include "vim.h"
183#include "version.h"
184
185#if defined(PROTO) && !defined(FEAT_RUBY)
186/* Define these to be able to generate the function prototypes. */
187# define VALUE int
188# define RUBY_DATA_FUNC int
189#endif
190
191static int ruby_initialized = 0;
Bram Moolenaar99685e62013-05-11 13:56:18 +0200192static void *ruby_stack_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193static VALUE objtbl;
194
195static VALUE mVIM;
196static VALUE cBuffer;
197static VALUE cVimWindow;
198static VALUE eDeletedBufferError;
199static VALUE eDeletedWindowError;
200
201static int ensure_ruby_initialized(void);
202static void error_print(int);
203static void ruby_io_init(void);
204static void ruby_vim_init(void);
205
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200206#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
207# if defined(__ia64) && !defined(ruby_init_stack)
208# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
209# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210#endif
211
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200212#if defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaaref269542016-01-19 13:22:12 +0100213# if defined(PROTO) && !defined(HINSTANCE)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200214# define HINSTANCE int /* for generating prototypes */
215# endif
216
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217/*
218 * Wrapper defines
219 */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200220# define rb_assoc_new dll_rb_assoc_new
221# define rb_cObject (*dll_rb_cObject)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100222# define rb_check_type dll_rb_check_type
223# ifdef USE_TYPEDDATA
224# define rb_check_typeddata dll_rb_check_typeddata
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100225# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200226# define rb_class_path dll_rb_class_path
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100227# ifdef USE_TYPEDDATA
228# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
229# define rb_data_typed_object_wrap dll_rb_data_typed_object_wrap
230# else
231# define rb_data_typed_object_alloc dll_rb_data_typed_object_alloc
232# endif
233# else
234# define rb_data_object_alloc dll_rb_data_object_alloc
235# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200236# define rb_define_class_under dll_rb_define_class_under
237# define rb_define_const dll_rb_define_const
238# define rb_define_global_function dll_rb_define_global_function
239# define rb_define_method dll_rb_define_method
240# define rb_define_module dll_rb_define_module
241# define rb_define_module_function dll_rb_define_module_function
242# define rb_define_singleton_method dll_rb_define_singleton_method
243# define rb_define_virtual_variable dll_rb_define_virtual_variable
244# define rb_stdout (*dll_rb_stdout)
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200245# define rb_stderr (*dll_rb_stderr)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200246# define rb_eArgError (*dll_rb_eArgError)
247# define rb_eIndexError (*dll_rb_eIndexError)
248# define rb_eRuntimeError (*dll_rb_eRuntimeError)
249# define rb_eStandardError (*dll_rb_eStandardError)
250# define rb_eval_string_protect dll_rb_eval_string_protect
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200251# ifdef RUBY21_OR_LATER
252# define rb_funcallv dll_rb_funcallv
253# else
254# define rb_funcall2 dll_rb_funcall2
255# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200256# define rb_global_variable dll_rb_global_variable
257# define rb_hash_aset dll_rb_hash_aset
258# define rb_hash_new dll_rb_hash_new
259# define rb_inspect dll_rb_inspect
260# define rb_int2inum dll_rb_int2inum
Bram Moolenaar218beb32018-08-04 17:24:44 +0200261
262// ruby.h may redefine rb_intern to use RUBY_CONST_ID_CACHE(), but that won't
263// work. Not using the cache appears to be the best solution.
264# undef rb_intern
265# define rb_intern dll_rb_intern
266
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100267# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar498af702014-03-28 21:58:21 +0100268# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 18
269# define rb_fix2int dll_rb_fix2int
270# define rb_num2int dll_rb_num2int
271# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200272# define rb_num2uint dll_rb_num2uint
273# endif
274# define rb_lastline_get dll_rb_lastline_get
275# define rb_lastline_set dll_rb_lastline_set
Bram Moolenaard0573012017-10-28 21:11:06 +0200276# define rb_protect dll_rb_protect
277# define rb_load dll_rb_load
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200278# ifndef RUBY19_OR_LATER
279# define rb_num2long dll_rb_num2long
280# endif
281# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 19
282# define rb_num2ulong dll_rb_num2ulong
283# endif
284# define rb_obj_alloc dll_rb_obj_alloc
285# define rb_obj_as_string dll_rb_obj_as_string
286# define rb_obj_id dll_rb_obj_id
287# define rb_raise dll_rb_raise
288# define rb_str_cat dll_rb_str_cat
289# define rb_str_concat dll_rb_str_concat
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100290# undef rb_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200291# define rb_str_new dll_rb_str_new
292# ifdef rb_str_new2
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200293/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200294# define need_rb_str_new_cstr 1
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200295/* Ruby's headers #define rb_str_new_cstr to make use of GCC's
296 * __builtin_constant_p extension. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200297# undef rb_str_new_cstr
298# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200299# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200300# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200301# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200302# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200303# define rb_string_value dll_rb_string_value
304# define rb_string_value_ptr dll_rb_string_value_ptr
305# define rb_float_new dll_rb_float_new
306# define rb_ary_new dll_rb_ary_new
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200307# ifdef rb_ary_new4
308# define RB_ARY_NEW4_MACRO 1
309# undef rb_ary_new4
310# endif
311# define rb_ary_new4 dll_rb_ary_new4
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200312# define rb_ary_push dll_rb_ary_push
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200313# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
314# ifdef __ia64
315# define rb_ia64_bsp dll_rb_ia64_bsp
316# undef ruby_init_stack
317# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
318# else
319# define ruby_init_stack dll_ruby_init_stack
320# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200321# endif
322# else
323# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200324# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200325# ifdef RUBY19_OR_LATER
326# define rb_errinfo dll_rb_errinfo
327# else
328# define ruby_errinfo (*dll_ruby_errinfo)
329# endif
330# define ruby_init dll_ruby_init
331# define ruby_init_loadpath dll_ruby_init_loadpath
332# ifdef WIN3264
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100333# ifdef RUBY19_OR_LATER
334# define ruby_sysinit dll_ruby_sysinit
335# else
336# define NtInitialize dll_NtInitialize
337# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200338# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
339# define rb_w32_snprintf dll_rb_w32_snprintf
340# endif
341# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200343# ifdef RUBY19_OR_LATER
344# define ruby_script dll_ruby_script
345# define rb_enc_find_index dll_rb_enc_find_index
346# define rb_enc_find dll_rb_enc_find
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100347# undef rb_enc_str_new
348# define rb_enc_str_new dll_rb_enc_str_new
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200349# define rb_sprintf dll_rb_sprintf
350# define rb_require dll_rb_require
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100351# define ruby_options dll_ruby_options
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200352# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100353
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354/*
355 * Pointers for dynamic link
356 */
357static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200358VALUE *dll_rb_cFalseClass;
359VALUE *dll_rb_cFixnum;
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200360# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200361VALUE *dll_rb_cInteger;
362# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200363# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100364VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200365# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200366VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367static VALUE *dll_rb_cObject;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200368VALUE *dll_rb_cSymbol;
369VALUE *dll_rb_cTrueClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370static void (*dll_rb_check_type) (VALUE,int);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100371# ifdef USE_TYPEDDATA
372static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *);
373# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374static VALUE (*dll_rb_class_path) (VALUE);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100375# ifdef USE_TYPEDDATA
376# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
377static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *);
378# else
379static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *);
380# endif
381# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100383# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
385static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
386static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
387static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
388static VALUE (*dll_rb_define_module) (const char*);
389static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
390static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
391static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
392static VALUE *dll_rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200393static VALUE *dll_rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394static VALUE *dll_rb_eArgError;
395static VALUE *dll_rb_eIndexError;
396static VALUE *dll_rb_eRuntimeError;
397static VALUE *dll_rb_eStandardError;
398static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200399# ifdef RUBY21_OR_LATER
400static VALUE (*dll_rb_funcallv) (VALUE, ID, int, const VALUE*);
401# else
402static VALUE (*dll_rb_funcall2) (VALUE, ID, int, const VALUE*);
403# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404static void (*dll_rb_global_variable) (VALUE*);
405static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
406static VALUE (*dll_rb_hash_new) (void);
407static VALUE (*dll_rb_inspect) (VALUE);
408static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200409static ID (*dll_rb_intern) (const char*);
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100410# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200411static long (*dll_rb_fix2int) (VALUE);
412static long (*dll_rb_num2int) (VALUE);
413static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200414# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415static VALUE (*dll_rb_lastline_get) (void);
416static void (*dll_rb_lastline_set) (VALUE);
Bram Moolenaar37badc82018-01-31 20:15:30 +0100417static VALUE (*dll_rb_protect) (VALUE (*)(VALUE), VALUE, int*);
Bram Moolenaard0573012017-10-28 21:11:06 +0200418static void (*dll_rb_load) (VALUE, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419static long (*dll_rb_num2long) (VALUE);
420static unsigned long (*dll_rb_num2ulong) (VALUE);
421static VALUE (*dll_rb_obj_alloc) (VALUE);
422static VALUE (*dll_rb_obj_as_string) (VALUE);
423static VALUE (*dll_rb_obj_id) (VALUE);
424static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200425# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200426static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200427# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200429# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
431static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
432static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200433# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200434/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
435static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200436# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200438# endif
439# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100440static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200441# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200443# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444static void (*dll_ruby_init) (void);
445static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200446# ifdef WIN3264
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100447# ifdef RUBY19_OR_LATER
Bram Moolenaar4f391792017-01-15 16:59:07 +0100448static void (*dll_ruby_sysinit) (int*, char***);
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100449# else
450static void (*dll_NtInitialize) (int*, char***);
451# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200452# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200453static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200454# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200455# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200456# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100457static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
458static VALUE (*dll_rb_float_new) (double);
459static VALUE (*dll_rb_ary_new) (void);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200460static VALUE (*dll_rb_ary_new4) (long n, const VALUE *elts);
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100461static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100462# if DYNAMIC_RUBY_VER >= 26
463static void (*dll_rb_ary_detransient) (VALUE);
464# endif
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200465# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
466# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200467static void * (*dll_rb_ia64_bsp) (void);
468static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200469# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200470static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200471# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200472# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200473# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200474# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100475static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200476# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200478# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100479static void (*dll_ruby_script) (const char*);
480static int (*dll_rb_enc_find_index) (const char*);
481static rb_encoding* (*dll_rb_enc_find) (const char*);
482static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
483static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100484static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100485static void* (*ruby_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200486# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100487
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100488# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100489# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100490static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100491# else
492static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
493# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100494# endif
495
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200496# if defined(RUBY19_OR_LATER) && !defined(PROTO)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200497# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
498long rb_num2long_stub(VALUE x)
499# else
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100500SIGNED_VALUE rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200501# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100502{
503 return dll_rb_num2long(x);
504}
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100505VALUE rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100506{
507 return dll_rb_int2big(x);
508}
Bram Moolenaar498af702014-03-28 21:58:21 +0100509# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \
510 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200511long rb_fix2int_stub(VALUE x)
512{
513 return dll_rb_fix2int(x);
514}
515long rb_num2int_stub(VALUE x)
516{
517 return dll_rb_num2int(x);
518}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200519# endif
520# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaar886ed692013-02-26 13:41:35 +0100521VALUE
522rb_float_new_in_heap(double d)
523{
524 return dll_rb_float_new(d);
525}
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200526# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
527unsigned long rb_num2ulong(VALUE x)
528# else
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100529VALUE rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200530# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100531{
532 return (long)RSHIFT((SIGNED_VALUE)(x),1);
533}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200534# endif
535# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100536
Bram Moolenaar3e9a1612014-11-12 16:05:04 +0100537 /* Do not generate a prototype here, VALUE isn't always defined. */
538# if defined(USE_RGENGC) && USE_RGENGC && !defined(PROTO)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100539# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100540void rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
541{
Bram Moolenaar90140742014-11-27 17:44:08 +0100542 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100543}
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100544# else
545void rb_gc_writebarrier_unprotect_stub(VALUE obj)
546{
547 dll_rb_gc_writebarrier_unprotect(obj);
548}
549# endif
550# endif
551
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200552static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553
554/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000555 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557static struct
558{
559 char *name;
560 RUBY_PROC *ptr;
561} ruby_funcname_table[] =
562{
563 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
564 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
Bram Moolenaar2016ae52016-06-13 20:08:43 +0200565# if defined(USE_RUBY_INTEGER)
Bram Moolenaar06469e92016-06-11 22:26:53 +0200566 {"rb_cInteger", (RUBY_PROC*)&dll_rb_cInteger},
Bram Moolenaar6abda992017-01-09 21:10:31 +0100567# else
568 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar06469e92016-06-11 22:26:53 +0200569# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200570# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100571 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200572# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
574 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
575 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
576 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
577 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100578# ifdef USE_TYPEDDATA
579 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
580# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100582# ifdef USE_TYPEDDATA
583# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
584 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
585# else
586 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
587# endif
588# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100590# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
592 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
593 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
594 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
595 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
596 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
597 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
598 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
599 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +0200600 {"rb_stderr", (RUBY_PROC*)&dll_rb_stderr},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
602 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
603 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
604 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
605 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200606# ifdef RUBY21_OR_LATER
607 {"rb_funcallv", (RUBY_PROC*)&dll_rb_funcallv},
608# else
609 {"rb_funcall2", (RUBY_PROC*)&dll_rb_funcall2},
610# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
612 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
613 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
614 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
615 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200616 {"rb_intern", (RUBY_PROC*)&dll_rb_intern},
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100617# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200618 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
619 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
620 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200621# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
623 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
Bram Moolenaard0573012017-10-28 21:11:06 +0200624 {"rb_protect", (RUBY_PROC*)&dll_rb_protect},
625 {"rb_load", (RUBY_PROC*)&dll_rb_load},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
627 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
628 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
629 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
630 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
631 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200632# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200633 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200634# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200636# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
638 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
639 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200640# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200641 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200642# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200644# endif
645# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100646 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200647# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200649# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
651 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200652# ifdef WIN3264
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100653# ifdef RUBY19_OR_LATER
Bram Moolenaar4f391792017-01-15 16:59:07 +0100654 {"ruby_sysinit", (RUBY_PROC*)&dll_ruby_sysinit},
Bram Moolenaar9d5c84a2018-12-19 20:48:46 +0100655# else
656 {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200657# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200658# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200660# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200661# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200662# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100663 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200664# if DYNAMIC_RUBY_VER <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100665 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200666# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100667 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200668# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100669 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +0200670# ifdef RB_ARY_NEW4_MACRO
671 {"rb_ary_new_from_values", (RUBY_PROC*)&dll_rb_ary_new4},
672# else
673 {"rb_ary_new4", (RUBY_PROC*)&dll_rb_ary_new4},
674# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100675 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100676# if DYNAMIC_RUBY_VER >= 26
677 {"rb_ary_detransient", (RUBY_PROC*)&dll_rb_ary_detransient},
678# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200679# endif
680# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100681 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100682 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
683 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
684 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
685 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
686 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100687 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100688 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200689# endif
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200690# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
691# ifdef __ia64
692 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
693# endif
694 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
695# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100696# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100697# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100698 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100699# else
700 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
701# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100702# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 {"", NULL},
704};
705
706/*
707 * Free ruby.dll
708 */
709 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100710end_dynamic_ruby(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711{
712 if (hinstRuby)
713 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200714 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200715 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 }
717}
718
719/*
720 * Load library and get all pointers.
721 * Parameter 'libname' provides name of DLL.
722 * Return OK or FAIL.
723 */
724 static int
725ruby_runtime_link_init(char *libname, int verbose)
726{
727 int i;
728
729 if (hinstRuby)
730 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200731 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 if (!hinstRuby)
733 {
734 if (verbose)
735 EMSG2(_(e_loadlib), libname);
736 return FAIL;
737 }
738
739 for (i = 0; ruby_funcname_table[i].ptr; ++i)
740 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200741 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 ruby_funcname_table[i].name)))
743 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200744 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200745 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 if (verbose)
747 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
748 return FAIL;
749 }
750 }
751 return OK;
752}
753
754/*
755 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
756 * else FALSE.
757 */
758 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100759ruby_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100761 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762}
763#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
764
765 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +0100766ruby_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767{
768#ifdef DYNAMIC_RUBY
769 end_dynamic_ruby();
770#endif
771}
772
773void ex_ruby(exarg_T *eap)
774{
775 int state;
776 char *script = NULL;
777
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000778 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 if (!eap->skip && ensure_ruby_initialized())
780 {
781 if (script == NULL)
782 rb_eval_string_protect((char *)eap->arg, &state);
783 else
784 rb_eval_string_protect(script, &state);
785 if (state)
786 error_print(state);
787 }
788 vim_free(script);
789}
790
Bram Moolenaar165641d2010-02-17 16:23:09 +0100791/*
792 * In Ruby 1.9 or later, ruby String object has encoding.
793 * conversion buffer string of vim to ruby String object using
794 * VIM encoding option.
795 */
796 static VALUE
797vim_str2rb_enc_str(const char *s)
798{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100799#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100800 int isnum;
801 long lval;
802 char_u *sval;
803 rb_encoding *enc;
804
805 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
806 if (isnum == 0)
807 {
808 enc = rb_enc_find((char *)sval);
809 vim_free(sval);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200810 if (enc)
811 {
Bram Moolenaar9b0ac222016-06-01 20:31:43 +0200812 return rb_enc_str_new(s, (long)strlen(s), enc);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100813 }
814 }
815#endif
816 return rb_str_new2(s);
817}
818
819 static VALUE
820eval_enc_string_protect(const char *str, int *state)
821{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100822#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100823 int isnum;
824 long lval;
825 char_u *sval;
826 rb_encoding *enc;
827 VALUE v;
828
829 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
830 if (isnum == 0)
831 {
832 enc = rb_enc_find((char *)sval);
833 vim_free(sval);
834 if (enc)
835 {
836 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
837 return rb_eval_string_protect(StringValuePtr(v), state);
838 }
839 }
840#endif
841 return rb_eval_string_protect(str, state);
842}
843
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844void ex_rubydo(exarg_T *eap)
845{
846 int state;
847 linenr_T i;
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100848 buf_T *was_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849
850 if (ensure_ruby_initialized())
851 {
852 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
853 return;
Bram Moolenaar758535a2016-03-30 22:06:16 +0200854 for (i = eap->line1; i <= eap->line2; i++)
855 {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100856 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100858 if (i > curbuf->b_ml.ml_line_count)
859 break;
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100860 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100862 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar758535a2016-03-30 22:06:16 +0200863 if (state)
864 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 error_print(state);
866 break;
867 }
Bram Moolenaarc593fee2017-01-29 23:11:25 +0100868 if (was_curbuf != curbuf)
869 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 line = rb_lastline_get();
Bram Moolenaar758535a2016-03-30 22:06:16 +0200871 if (!NIL_P(line))
872 {
873 if (TYPE(line) != T_STRING)
874 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000875 EMSG(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 return;
877 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100878 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 changed();
880#ifdef SYNTAX_HL
881 syn_changed(i); /* recompute syntax hl. for this line */
882#endif
883 }
884 }
885 check_cursor();
886 update_curbuf(NOT_VALID);
887 }
888}
889
Bram Moolenaar0b394642018-05-17 13:11:46 +0200890static VALUE rb_load_wrap(VALUE file_to_load)
Bram Moolenaar37badc82018-01-31 20:15:30 +0100891{
892 rb_load(file_to_load, 0);
893 return Qnil;
894}
895
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896void ex_rubyfile(exarg_T *eap)
897{
898 int state;
899
900 if (ensure_ruby_initialized())
901 {
Bram Moolenaar37badc82018-01-31 20:15:30 +0100902 VALUE file_to_load = rb_str_new2((const char *)eap->arg);
903 rb_protect(rb_load_wrap, file_to_load, &state);
904 if (state)
905 error_print(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 }
907}
908
909void ruby_buffer_free(buf_T *buf)
910{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000911 if (buf->b_ruby_ref)
912 {
913 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
914 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 }
916}
917
918void ruby_window_free(win_T *win)
919{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000920 if (win->w_ruby_ref)
921 {
922 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
923 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 }
925}
926
927static int ensure_ruby_initialized(void)
928{
929 if (!ruby_initialized)
930 {
931#ifdef DYNAMIC_RUBY
932 if (ruby_enabled(TRUE))
933 {
934#endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100935#ifdef _WIN32
936 /* suggested by Ariya Mizutani */
937 int argc = 1;
938 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +0100939 char **argvp = argv;
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100940# ifdef RUBY19_OR_LATER
941 ruby_sysinit(&argc, &argvp);
942# else
Bram Moolenaar90140742014-11-27 17:44:08 +0100943 NtInitialize(&argc, &argvp);
Bram Moolenaarfe6ce332017-01-14 20:12:01 +0100944# endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100945#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200946 {
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200947#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +0200948 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100949#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200950 ruby_init();
951 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100952#ifdef RUBY19_OR_LATER
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100953 {
954 int dummy_argc = 2;
Bram Moolenaard1bc96ce2017-09-26 21:21:44 +0200955 char *dummy_argv[] = {"vim-ruby", "-e_=0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100956 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100957 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100958 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100959#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100961#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100962 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 ruby_vim_init();
964 ruby_initialized = 1;
965#ifdef DYNAMIC_RUBY
966 }
967 else
968 {
969 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
970 return 0;
971 }
972#endif
973 }
974 return ruby_initialized;
975}
976
977static void error_print(int state)
978{
Bram Moolenaarb09c6842018-12-27 22:11:01 +0100979#if !defined(DYNAMIC_RUBY) && !defined(RUBY19_OR_LATER)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 RUBYEXTERN VALUE ruby_errinfo;
981#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200982 VALUE error;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 VALUE eclass;
984 VALUE einfo;
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200985 VALUE bt;
986 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 char buff[BUFSIZ];
Bram Moolenaarf711cb22018-08-01 18:42:13 +0200988 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989
990#define TAG_RETURN 0x1
991#define TAG_BREAK 0x2
992#define TAG_NEXT 0x3
993#define TAG_RETRY 0x4
994#define TAG_REDO 0x5
995#define TAG_RAISE 0x6
996#define TAG_THROW 0x7
997#define TAG_FATAL 0x8
998#define TAG_MASK 0xf
999
Bram Moolenaar758535a2016-03-30 22:06:16 +02001000 switch (state)
1001 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 case TAG_RETURN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001003 EMSG(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 break;
1005 case TAG_NEXT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001006 EMSG(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 break;
1008 case TAG_BREAK:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001009 EMSG(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 break;
1011 case TAG_REDO:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001012 EMSG(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 break;
1014 case TAG_RETRY:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001015 EMSG(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 break;
1017 case TAG_RAISE:
1018 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +01001019#ifdef RUBY19_OR_LATER
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001020 error = rb_errinfo();
Bram Moolenaar165641d2010-02-17 16:23:09 +01001021#else
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001022 error = ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +01001023#endif
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001024 eclass = CLASS_OF(error);
1025 einfo = rb_obj_as_string(error);
Bram Moolenaar758535a2016-03-30 22:06:16 +02001026 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0)
1027 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001028 EMSG(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 }
Bram Moolenaar758535a2016-03-30 22:06:16 +02001030 else
1031 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 VALUE epath;
1033 char *p;
1034
1035 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001036 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar165641d2010-02-17 16:23:09 +01001037 RSTRING_PTR(epath), RSTRING_PTR(einfo));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 p = strchr(buff, '\n');
1039 if (p) *p = '\0';
1040 EMSG(buff);
1041 }
Bram Moolenaarf711cb22018-08-01 18:42:13 +02001042
1043 attr = syn_name2attr((char_u *)"Error");
1044# ifdef RUBY21_OR_LATER
1045 bt = rb_funcallv(error, rb_intern("backtrace"), 0, 0);
1046 for (i = 0; i < RARRAY_LEN(bt); i++)
1047 msg_attr((char_u *)RSTRING_PTR(RARRAY_AREF(bt, i)), attr);
1048# else
1049 bt = rb_funcall2(error, rb_intern("backtrace"), 0, 0);
1050 for (i = 0; i < RARRAY_LEN(bt); i++)
1051 msg_attr((char_u *)RSTRING_PTR(RARRAY_PTR(bt)[i]), attr);
1052# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 break;
1054 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001055 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 EMSG(buff);
1057 break;
1058 }
1059}
1060
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001061static VALUE vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062{
1063 char *buff, *p;
1064
1065 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001066 if (RSTRING_LEN(str) > 0)
1067 {
1068 /* Only do this when the string isn't empty, alloc(0) causes trouble. */
Bram Moolenaar00ccf542017-09-03 15:17:48 +02001069 buff = ALLOCA_N(char, RSTRING_LEN(str) + 1);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +02001070 strcpy(buff, RSTRING_PTR(str));
1071 p = strchr(buff, '\n');
1072 if (p) *p = '\0';
1073 MSG(buff);
1074 }
1075 else
1076 {
1077 MSG("");
1078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 return Qnil;
1080}
1081
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001082static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001084 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 update_screen(NOT_VALID);
1086 return Qnil;
1087}
1088
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001089static VALUE vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001091 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 return Qnil;
1093}
1094
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001095#ifdef FEAT_EVAL
1096static VALUE vim_to_ruby(typval_T *tv)
1097{
1098 VALUE result = Qnil;
1099
1100 if (tv->v_type == VAR_STRING)
1101 {
Bram Moolenaar94127e42010-03-19 23:08:48 +01001102 result = rb_str_new2(tv->vval.v_string == NULL
1103 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001104 }
1105 else if (tv->v_type == VAR_NUMBER)
1106 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001107 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001108 }
1109# ifdef FEAT_FLOAT
1110 else if (tv->v_type == VAR_FLOAT)
1111 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001112 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001113 }
1114# endif
1115 else if (tv->v_type == VAR_LIST)
1116 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001117 list_T *list = tv->vval.v_list;
1118 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001119
Bram Moolenaar639a2552010-03-17 18:15:23 +01001120 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001121
Bram Moolenaar639a2552010-03-17 18:15:23 +01001122 if (list != NULL)
1123 {
1124 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
1125 {
1126 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
1127 }
1128 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001129 }
1130 else if (tv->v_type == VAR_DICT)
1131 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001132 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001133
Bram Moolenaar639a2552010-03-17 18:15:23 +01001134 if (tv->vval.v_dict != NULL)
1135 {
1136 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1137 long_u todo = ht->ht_used;
1138 hashitem_T *hi;
1139 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001140
Bram Moolenaar639a2552010-03-17 18:15:23 +01001141 for (hi = ht->ht_array; todo > 0; ++hi)
1142 {
1143 if (!HASHITEM_EMPTY(hi))
1144 {
1145 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001146
Bram Moolenaar639a2552010-03-17 18:15:23 +01001147 di = dict_lookup(hi);
1148 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001149 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001150 }
1151 }
1152 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001153 }
1154 else if (tv->v_type == VAR_SPECIAL)
1155 {
Bram Moolenaard84b26a2018-07-28 17:18:09 +02001156 if (tv->vval.v_number == VVAL_TRUE)
1157 result = Qtrue;
1158 else if (tv->vval.v_number == VVAL_FALSE)
1159 result = Qfalse;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001160 } /* else return Qnil; */
1161
1162 return result;
1163}
1164#endif
1165
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001166static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167{
1168#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001169 typval_T *tv;
1170 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001172 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1173 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001175 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001177 result = vim_to_ruby(tv);
1178
1179 free_tv(tv);
1180
1181 return result;
1182#else
1183 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185}
1186
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001187#ifdef USE_TYPEDDATA
1188static size_t buffer_dsize(const void *buf);
1189
1190static const rb_data_type_t buffer_type = {
1191 "vim_buffer",
1192 {0, 0, buffer_dsize, {0, 0}},
1193 0, 0,
1194# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1195 0,
1196# endif
1197};
1198
1199static size_t buffer_dsize(const void *buf UNUSED)
1200{
1201 return sizeof(buf_T);
1202}
1203#endif
1204
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205static VALUE buffer_new(buf_T *buf)
1206{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001207 if (buf->b_ruby_ref)
1208 {
1209 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001211 else
1212 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001213#ifdef USE_TYPEDDATA
1214 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1215#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001217#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001218 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1220 return obj;
1221 }
1222}
1223
1224static buf_T *get_buf(VALUE obj)
1225{
1226 buf_T *buf;
1227
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001228#ifdef USE_TYPEDDATA
1229 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1230#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 if (buf == NULL)
1234 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1235 return buf;
1236}
1237
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001238static VALUE buffer_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239{
1240 return buffer_new(curbuf);
1241}
1242
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001243static VALUE buffer_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244{
1245 buf_T *b;
1246 int n = 0;
1247
Bram Moolenaar29323592016-07-24 22:04:11 +02001248 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001249 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001250 /* Deleted buffers should not be counted
1251 * SegPhault - 01/07/05 */
1252 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001253 n++;
1254 }
1255
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 return INT2NUM(n);
1257}
1258
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001259static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260{
1261 buf_T *b;
1262 int n = NUM2INT(num);
1263
Bram Moolenaar29323592016-07-24 22:04:11 +02001264 FOR_ALL_BUFFERS(b)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001265 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001266 /* Deleted buffers should not be counted
1267 * SegPhault - 01/07/05 */
1268 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001269 continue;
1270
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001271 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001273
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001274 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 }
1276 return Qnil;
1277}
1278
1279static VALUE buffer_name(VALUE self)
1280{
1281 buf_T *buf = get_buf(self);
1282
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001283 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284}
1285
1286static VALUE buffer_number(VALUE self)
1287{
1288 buf_T *buf = get_buf(self);
1289
1290 return INT2NUM(buf->b_fnum);
1291}
1292
1293static VALUE buffer_count(VALUE self)
1294{
1295 buf_T *buf = get_buf(self);
1296
1297 return INT2NUM(buf->b_ml.ml_line_count);
1298}
1299
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001300static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001302 if (n <= 0 || n > buf->b_ml.ml_line_count)
1303 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1304 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305}
1306
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001307static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308{
1309 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001310
1311 if (buf != NULL)
1312 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
1313 return Qnil; /* For stop warning */
1314}
1315
1316static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
1317{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001318 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001319 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001321 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1322 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001323 /* set curwin/curbuf for "buf" and save some things */
1324 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001325
Bram Moolenaar758535a2016-03-30 22:06:16 +02001326 if (u_savesub(n) == OK)
1327 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001328 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 changed();
1330#ifdef SYNTAX_HL
1331 syn_changed(n); /* recompute syntax hl. for this line */
1332#endif
1333 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001334
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001335 /* restore curwin/curbuf and a few other things */
1336 aucmd_restbuf(&aco);
1337 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001338
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 update_curbuf(NOT_VALID);
1340 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001341 else
1342 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001343 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 }
1345 return str;
1346}
1347
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001348static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
1349{
1350 buf_T *buf = get_buf(self);
1351
1352 if (buf != NULL)
1353 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1354 return str;
1355}
1356
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357static VALUE buffer_delete(VALUE self, VALUE num)
1358{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001359 buf_T *buf = get_buf(self);
1360 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001361 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001363 if (n > 0 && n <= buf->b_ml.ml_line_count)
1364 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001365 /* set curwin/curbuf for "buf" and save some things */
1366 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001367
Bram Moolenaar758535a2016-03-30 22:06:16 +02001368 if (u_savedel(n, 1) == OK)
1369 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001371
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001372 /* Changes to non-active buffers should properly refresh
1373 * SegPhault - 01/09/05 */
1374 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001375
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 changed();
1377 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001378
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001379 /* restore curwin/curbuf and a few other things */
1380 aucmd_restbuf(&aco);
1381 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001382
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 update_curbuf(NOT_VALID);
1384 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001385 else
1386 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001387 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 }
1389 return Qnil;
1390}
1391
1392static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
1393{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001394 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001395 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001396 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001397 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398
Bram Moolenaar3c531602010-11-16 14:46:19 +01001399 if (line == NULL)
1400 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001401 rb_raise(rb_eIndexError, "NULL line");
1402 }
1403 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001404 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001405 /* set curwin/curbuf for "buf" and save some things */
1406 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001407
Bram Moolenaar758535a2016-03-30 22:06:16 +02001408 if (u_inssub(n + 1) == OK)
1409 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001411
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001412 /* Changes to non-active buffers should properly refresh screen
1413 * SegPhault - 12/20/04 */
1414 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001415
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001416 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001418
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001419 /* restore curwin/curbuf and a few other things */
1420 aucmd_restbuf(&aco);
1421 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001422
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 update_curbuf(NOT_VALID);
1424 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001425 else
1426 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001427 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 }
1429 return str;
1430}
1431
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001432#ifdef USE_TYPEDDATA
1433static size_t window_dsize(const void *buf);
1434
1435static const rb_data_type_t window_type = {
1436 "vim_window",
1437 {0, 0, window_dsize, {0, 0}},
1438 0, 0,
1439# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1440 0,
1441# endif
1442};
1443
1444static size_t window_dsize(const void *win UNUSED)
1445{
1446 return sizeof(win_T);
1447}
1448#endif
1449
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450static VALUE window_new(win_T *win)
1451{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001452 if (win->w_ruby_ref)
1453 {
1454 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001456 else
1457 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001458#ifdef USE_TYPEDDATA
1459 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1460#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001462#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001463 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1465 return obj;
1466 }
1467}
1468
1469static win_T *get_win(VALUE obj)
1470{
1471 win_T *win;
1472
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001473#ifdef USE_TYPEDDATA
1474 TypedData_Get_Struct(obj, win_T, &window_type, win);
1475#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 if (win == NULL)
1479 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1480 return win;
1481}
1482
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001483static VALUE window_s_current(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484{
1485 return window_new(curwin);
1486}
1487
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001488/*
1489 * Added line manipulation functions
1490 * SegPhault - 03/07/05
1491 */
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001492static VALUE line_s_current(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001493{
1494 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1495}
1496
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001497static VALUE set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001498{
1499 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1500}
1501
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001502static VALUE current_line_number(void)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001503{
1504 return INT2FIX((int)curwin->w_cursor.lnum);
1505}
1506
1507
1508
Bram Moolenaar68c2f632016-01-30 17:24:07 +01001509static VALUE window_s_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 win_T *w;
1512 int n = 0;
1513
Bram Moolenaar29323592016-07-24 22:04:11 +02001514 FOR_ALL_WINDOWS(w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 n++;
1516 return INT2NUM(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517}
1518
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001519static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520{
1521 win_T *w;
1522 int n = NUM2INT(num);
1523
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 for (w = firstwin; w != NULL; w = w->w_next, --n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 if (n == 0)
1526 return window_new(w);
1527 return Qnil;
1528}
1529
1530static VALUE window_buffer(VALUE self)
1531{
1532 win_T *win = get_win(self);
1533
1534 return buffer_new(win->w_buffer);
1535}
1536
1537static VALUE window_height(VALUE self)
1538{
1539 win_T *win = get_win(self);
1540
1541 return INT2NUM(win->w_height);
1542}
1543
1544static VALUE window_set_height(VALUE self, VALUE height)
1545{
1546 win_T *win = get_win(self);
1547 win_T *savewin = curwin;
1548
1549 curwin = win;
1550 win_setheight(NUM2INT(height));
1551 curwin = savewin;
1552 return height;
1553}
1554
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001555static VALUE window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001556{
Bram Moolenaare745d752017-09-22 16:56:22 +02001557 return INT2NUM(get_win(self)->w_width);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001558}
1559
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001560static VALUE window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001561{
1562 win_T *win = get_win(self);
1563 win_T *savewin = curwin;
1564
1565 curwin = win;
1566 win_setwidth(NUM2INT(width));
1567 curwin = savewin;
1568 return width;
1569}
1570
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571static VALUE window_cursor(VALUE self)
1572{
1573 win_T *win = get_win(self);
1574
1575 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1576}
1577
1578static VALUE window_set_cursor(VALUE self, VALUE pos)
1579{
1580 VALUE lnum, col;
1581 win_T *win = get_win(self);
1582
1583 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001584 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001586 lnum = RARRAY_PTR(pos)[0];
1587 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 win->w_cursor.lnum = NUM2LONG(lnum);
1589 win->w_cursor.col = NUM2UINT(col);
Bram Moolenaar53901442018-07-25 22:02:36 +02001590 win->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 check_cursor(); /* put cursor on an existing line */
1592 update_screen(NOT_VALID);
1593 return Qnil;
1594}
1595
Bram Moolenaar6217cdc2012-04-25 12:28:09 +02001596static VALUE f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001597{
1598 return Qnil;
1599}
1600
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001601static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602{
1603 int i;
1604 VALUE str = rb_str_new("", 0);
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001605 VALUE ret = Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606
Bram Moolenaar758535a2016-03-30 22:06:16 +02001607 for (i = 0; i < argc; i++)
1608 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 if (i > 0) rb_str_cat(str, ", ", 2);
1610 rb_str_concat(str, rb_inspect(argv[i]));
1611 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001612 MSG(RSTRING_PTR(str));
Bram Moolenaar51e9fbf2018-08-11 14:24:11 +02001613
1614 if (argc == 1)
1615 ret = argv[0];
1616 else if (argc > 1)
1617 ret = rb_ary_new4(argc, argv);
1618 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619}
1620
1621static void ruby_io_init(void)
1622{
1623#ifndef DYNAMIC_RUBY
1624 RUBYEXTERN VALUE rb_stdout;
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001625 RUBYEXTERN VALUE rb_stderr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626#endif
1627
1628 rb_stdout = rb_obj_alloc(rb_cObject);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001629 rb_stderr = rb_obj_alloc(rb_cObject);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001631 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaarb6c8cd82018-07-24 05:41:30 +02001632 rb_define_singleton_method(rb_stderr, "write", vim_message, 1);
1633 rb_define_singleton_method(rb_stderr, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 rb_define_global_function("p", f_p, -1);
1635}
1636
1637static void ruby_vim_init(void)
1638{
1639 objtbl = rb_hash_new();
1640 rb_global_variable(&objtbl);
1641
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001642 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001643 * alias "VIM" for backwards compatibility. */
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001644 mVIM = rb_define_module("Vim");
1645 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1647 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1648 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1649 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1650 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1651 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1652 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1653 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1654 rb_define_module_function(mVIM, "message", vim_message, 1);
1655 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1656 rb_define_module_function(mVIM, "command", vim_command, 1);
1657 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
1658
1659 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1660 rb_eStandardError);
1661 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1662 rb_eStandardError);
1663
1664 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1665 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1666 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1667 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1668 rb_define_method(cBuffer, "name", buffer_name, 0);
1669 rb_define_method(cBuffer, "number", buffer_number, 0);
1670 rb_define_method(cBuffer, "count", buffer_count, 0);
1671 rb_define_method(cBuffer, "length", buffer_count, 0);
1672 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1673 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1674 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1675 rb_define_method(cBuffer, "append", buffer_append, 2);
1676
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001677 /* Added line manipulation functions
1678 * SegPhault - 03/07/05 */
1679 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1680 rb_define_method(cBuffer, "line", line_s_current, 0);
1681 rb_define_method(cBuffer, "line=", set_current_line, 1);
1682
1683
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1685 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1686 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1687 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1688 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1689 rb_define_method(cVimWindow, "height", window_height, 0);
1690 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001691 rb_define_method(cVimWindow, "width", window_width, 0);
1692 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1694 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1695
1696 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1697 rb_define_virtual_variable("$curwin", window_s_current, 0);
1698}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001699
1700void vim_ruby_init(void *stack_start)
1701{
1702 /* should get machine stack start address early in main function */
1703 ruby_stack_start = stack_start;
1704}