blob: 96d9453dbc11f975b44afb8a1de7037eed3f2231 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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 Moolenaarf9b5ef82010-09-29 13:02:53 +020034#ifdef DYNAMIC_RUBY
Bram Moolenaar071d4272004-06-13 20:20:40 +000035/*
36 * This is tricky. In ruby.h there is (inline) function rb_class_of()
37 * definition. This function use these variables. But we want function to
38 * use dll_* variables.
39 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000040# define rb_cFalseClass (*dll_rb_cFalseClass)
41# define rb_cFixnum (*dll_rb_cFixnum)
Bram Moolenaardb3fbe52013-03-07 15:16:21 +010042# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
43# define rb_cFloat (*dll_rb_cFloat)
44# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000045# define rb_cNilClass (*dll_rb_cNilClass)
46# define rb_cSymbol (*dll_rb_cSymbol)
47# define rb_cTrueClass (*dll_rb_cTrueClass)
48# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
49/*
Bram Moolenaar42d57f02010-03-10 12:47:00 +010050 * On ver 1.8, all Ruby functions are exported with "__declspec(dllimport)"
51 * in ruby.h. But it causes trouble for these variables, because it is
Bram Moolenaar071d4272004-06-13 20:20:40 +000052 * defined in this file. When defined this RUBY_EXPORT it modified to
53 * "extern" and be able to avoid this problem.
54 */
55# define RUBY_EXPORT
56# endif
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020057
Bram Moolenaar2d73ff42010-10-27 17:40:59 +020058#if !(defined(WIN32) || defined(_WIN64))
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020059# include <dlfcn.h>
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020060# define HINSTANCE void*
61# define RUBY_PROC void*
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020062# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
63# define symbol_from_dll dlsym
64# define close_dll dlclose
65#else
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020066# define RUBY_PROC FARPROC
Bram Moolenaarebbcb822010-10-23 14:02:54 +020067# define load_dll vimLoadLib
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020068# define symbol_from_dll GetProcAddress
69# define close_dll FreeLibrary
Bram Moolenaar071d4272004-06-13 20:20:40 +000070#endif
71
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020072#endif /* ifdef DYNAMIC_RUBY */
73
Bram Moolenaar0b69c732010-02-17 15:11:50 +010074/* suggested by Ariya Mizutani */
75#if (_MSC_VER == 1200)
76# undef _WIN32_WINNT
77#endif
78
Bram Moolenaar639a2552010-03-17 18:15:23 +010079#if (defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
80 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
81# define RUBY19_OR_LATER 1
82#endif
83
Bram Moolenaar0d27f642015-12-28 22:05:28 +010084#if (defined(RUBY_VERSION) && RUBY_VERSION >= 20) \
85 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20)
86# define RUBY20_OR_LATER 1
87#endif
88
Bram Moolenaar42d57f02010-03-10 12:47:00 +010089#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
90/* Ruby 1.9 defines a number of static functions which use rb_num2long and
91 * rb_int2big */
92# define rb_num2long rb_num2long_stub
93# define rb_int2big rb_int2big_stub
94#endif
95
Bram Moolenaar498af702014-03-28 21:58:21 +010096#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \
97 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
98/* Ruby 1.9 defines a number of static functions which use rb_fix2int and
Bram Moolenaara2aa31a2014-02-23 22:52:40 +010099 * rb_num2int if VIM_SIZEOF_INT < VIM_SIZEOF_LONG (64bit) */
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200100# define rb_fix2int rb_fix2int_stub
101# define rb_num2int rb_num2int_stub
102#endif
103
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100104#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100105/* Ruby 2.1 adds new GC called RGenGC and RARRAY_PTR uses
106 * rb_gc_writebarrier_unprotect_promoted if USE_RGENGC */
Bram Moolenaar90140742014-11-27 17:44:08 +0100107# define rb_gc_writebarrier_unprotect_promoted rb_gc_writebarrier_unprotect_promoted_stub
108#endif
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100109#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
110# define rb_gc_writebarrier_unprotect rb_gc_writebarrier_unprotect_stub
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100111#endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100112
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#include <ruby.h>
Bram Moolenaar639a2552010-03-17 18:15:23 +0100114#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100115# include <ruby/encoding.h>
116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100118#undef off_t /* ruby defines off_t as _int64, Mingw uses long */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119#undef EXTERN
120#undef _
121
122/* T_DATA defined both by Ruby and Mac header files, hack around it... */
Bram Moolenaarcb635362007-05-12 13:02:42 +0000123#if defined(MACOS_X_UNIX) || defined(macintosh)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124# define __OPENTRANSPORT__
125# define __OPENTRANSPORTPROTOCOL__
126# define __OPENTRANSPORTPROVIDERS__
127#endif
128
Bram Moolenaar165641d2010-02-17 16:23:09 +0100129/*
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100130 * The TypedData_XXX macro family can be used since Ruby 1.9.2 but
131 * rb_data_type_t changed in 1.9.3, therefore require at least 2.0.
132 * The old Data_XXX macro family was deprecated on Ruby 2.2.
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100133 * Use TypedData_XXX if available.
134 */
Bram Moolenaar0d27f642015-12-28 22:05:28 +0100135#if defined(TypedData_Wrap_Struct) && defined(RUBY20_OR_LATER)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100136# define USE_TYPEDDATA 1
137#endif
138
139/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200140 * Backward compatibility for Ruby 1.8 and earlier.
Bram Moolenaar165641d2010-02-17 16:23:09 +0100141 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
142 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
143 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
144 */
145#ifndef StringValuePtr
146# define StringValuePtr(s) STR2CSTR(s)
147#endif
148#ifndef RARRAY_LEN
149# define RARRAY_LEN(s) RARRAY(s)->len
150#endif
151#ifndef RARRAY_PTR
152# define RARRAY_PTR(s) RARRAY(s)->ptr
153#endif
154#ifndef RSTRING_LEN
155# define RSTRING_LEN(s) RSTRING(s)->len
156#endif
157#ifndef RSTRING_PTR
158# define RSTRING_PTR(s) RSTRING(s)->ptr
159#endif
160
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161#include "vim.h"
162#include "version.h"
163
164#if defined(PROTO) && !defined(FEAT_RUBY)
165/* Define these to be able to generate the function prototypes. */
166# define VALUE int
167# define RUBY_DATA_FUNC int
168#endif
169
170static int ruby_initialized = 0;
Bram Moolenaar99685e62013-05-11 13:56:18 +0200171static void *ruby_stack_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172static VALUE objtbl;
173
174static VALUE mVIM;
175static VALUE cBuffer;
176static VALUE cVimWindow;
177static VALUE eDeletedBufferError;
178static VALUE eDeletedWindowError;
179
180static int ensure_ruby_initialized(void);
181static void error_print(int);
182static void ruby_io_init(void);
183static void ruby_vim_init(void);
184
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200185#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
186# if defined(__ia64) && !defined(ruby_init_stack)
187# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
188# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189#endif
190
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200191#if defined(DYNAMIC_RUBY) || defined(PROTO)
Bram Moolenaaref269542016-01-19 13:22:12 +0100192# if defined(PROTO) && !defined(HINSTANCE)
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200193# define HINSTANCE int /* for generating prototypes */
194# endif
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
197 * Wrapper defines
198 */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200199# define rb_assoc_new dll_rb_assoc_new
200# define rb_cObject (*dll_rb_cObject)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100201# define rb_check_type dll_rb_check_type
202# ifdef USE_TYPEDDATA
203# define rb_check_typeddata dll_rb_check_typeddata
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100204# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200205# define rb_class_path dll_rb_class_path
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100206# ifdef USE_TYPEDDATA
207# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
208# define rb_data_typed_object_wrap dll_rb_data_typed_object_wrap
209# else
210# define rb_data_typed_object_alloc dll_rb_data_typed_object_alloc
211# endif
212# else
213# define rb_data_object_alloc dll_rb_data_object_alloc
214# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200215# define rb_define_class_under dll_rb_define_class_under
216# define rb_define_const dll_rb_define_const
217# define rb_define_global_function dll_rb_define_global_function
218# define rb_define_method dll_rb_define_method
219# define rb_define_module dll_rb_define_module
220# define rb_define_module_function dll_rb_define_module_function
221# define rb_define_singleton_method dll_rb_define_singleton_method
222# define rb_define_virtual_variable dll_rb_define_virtual_variable
223# define rb_stdout (*dll_rb_stdout)
224# define rb_eArgError (*dll_rb_eArgError)
225# define rb_eIndexError (*dll_rb_eIndexError)
226# define rb_eRuntimeError (*dll_rb_eRuntimeError)
227# define rb_eStandardError (*dll_rb_eStandardError)
228# define rb_eval_string_protect dll_rb_eval_string_protect
229# define rb_global_variable dll_rb_global_variable
230# define rb_hash_aset dll_rb_hash_aset
231# define rb_hash_new dll_rb_hash_new
232# define rb_inspect dll_rb_inspect
233# define rb_int2inum dll_rb_int2inum
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100234# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar498af702014-03-28 21:58:21 +0100235# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 18
236# define rb_fix2int dll_rb_fix2int
237# define rb_num2int dll_rb_num2int
238# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200239# define rb_num2uint dll_rb_num2uint
240# endif
241# define rb_lastline_get dll_rb_lastline_get
242# define rb_lastline_set dll_rb_lastline_set
243# define rb_load_protect dll_rb_load_protect
244# ifndef RUBY19_OR_LATER
245# define rb_num2long dll_rb_num2long
246# endif
247# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 19
248# define rb_num2ulong dll_rb_num2ulong
249# endif
250# define rb_obj_alloc dll_rb_obj_alloc
251# define rb_obj_as_string dll_rb_obj_as_string
252# define rb_obj_id dll_rb_obj_id
253# define rb_raise dll_rb_raise
254# define rb_str_cat dll_rb_str_cat
255# define rb_str_concat dll_rb_str_concat
256# define rb_str_new dll_rb_str_new
257# ifdef rb_str_new2
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200258/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200259# define need_rb_str_new_cstr 1
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200260/* Ruby's headers #define rb_str_new_cstr to make use of GCC's
261 * __builtin_constant_p extension. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200262# undef rb_str_new_cstr
263# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200264# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200265# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200266# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200267# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200268# define rb_string_value dll_rb_string_value
269# define rb_string_value_ptr dll_rb_string_value_ptr
270# define rb_float_new dll_rb_float_new
271# define rb_ary_new dll_rb_ary_new
272# define rb_ary_push dll_rb_ary_push
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200273# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
274# ifdef __ia64
275# define rb_ia64_bsp dll_rb_ia64_bsp
276# undef ruby_init_stack
277# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
278# else
279# define ruby_init_stack dll_ruby_init_stack
280# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200281# endif
282# else
283# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200284# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200285# ifdef RUBY19_OR_LATER
286# define rb_errinfo dll_rb_errinfo
287# else
288# define ruby_errinfo (*dll_ruby_errinfo)
289# endif
290# define ruby_init dll_ruby_init
291# define ruby_init_loadpath dll_ruby_init_loadpath
292# ifdef WIN3264
293# define NtInitialize dll_NtInitialize
294# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
295# define rb_w32_snprintf dll_rb_w32_snprintf
296# endif
297# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200299# ifdef RUBY19_OR_LATER
300# define ruby_script dll_ruby_script
301# define rb_enc_find_index dll_rb_enc_find_index
302# define rb_enc_find dll_rb_enc_find
303# define rb_enc_str_new dll_rb_enc_str_new
304# define rb_sprintf dll_rb_sprintf
305# define rb_require dll_rb_require
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100306# define ruby_options dll_ruby_options
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200307# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100308
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309/*
310 * Pointers for dynamic link
311 */
312static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200313VALUE *dll_rb_cFalseClass;
314VALUE *dll_rb_cFixnum;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200315# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100316VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200317# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200318VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319static VALUE *dll_rb_cObject;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200320VALUE *dll_rb_cSymbol;
321VALUE *dll_rb_cTrueClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322static void (*dll_rb_check_type) (VALUE,int);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100323# ifdef USE_TYPEDDATA
324static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *);
325# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326static VALUE (*dll_rb_class_path) (VALUE);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100327# ifdef USE_TYPEDDATA
328# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
329static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *);
330# else
331static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *);
332# endif
333# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100335# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
337static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
338static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
339static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
340static VALUE (*dll_rb_define_module) (const char*);
341static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
342static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
343static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
344static VALUE *dll_rb_stdout;
345static VALUE *dll_rb_eArgError;
346static VALUE *dll_rb_eIndexError;
347static VALUE *dll_rb_eRuntimeError;
348static VALUE *dll_rb_eStandardError;
349static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
350static void (*dll_rb_global_variable) (VALUE*);
351static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
352static VALUE (*dll_rb_hash_new) (void);
353static VALUE (*dll_rb_inspect) (VALUE);
354static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100355# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200356static long (*dll_rb_fix2int) (VALUE);
357static long (*dll_rb_num2int) (VALUE);
358static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200359# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360static VALUE (*dll_rb_lastline_get) (void);
361static void (*dll_rb_lastline_set) (VALUE);
362static void (*dll_rb_load_protect) (VALUE, int, int*);
363static long (*dll_rb_num2long) (VALUE);
364static unsigned long (*dll_rb_num2ulong) (VALUE);
365static VALUE (*dll_rb_obj_alloc) (VALUE);
366static VALUE (*dll_rb_obj_as_string) (VALUE);
367static VALUE (*dll_rb_obj_id) (VALUE);
368static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200369# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200370static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200371# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200373# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
375static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
376static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200377# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200378/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
379static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200380# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200382# endif
383# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100384static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200385# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200387# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388static void (*dll_ruby_init) (void);
389static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200390# ifdef WIN3264
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100391static void (*dll_NtInitialize) (int*, char***);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200392# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200393static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200394# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200395# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200396# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100397static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
398static VALUE (*dll_rb_float_new) (double);
399static VALUE (*dll_rb_ary_new) (void);
400static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200401# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
402# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200403static void * (*dll_rb_ia64_bsp) (void);
404static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200405# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200406static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200407# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200408# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200409# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200410# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100411static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200412# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200414# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100415static void (*dll_ruby_script) (const char*);
416static int (*dll_rb_enc_find_index) (const char*);
417static rb_encoding* (*dll_rb_enc_find) (const char*);
418static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
419static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100420static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100421static void* (*ruby_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200422# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100423
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100424# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100425# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100426static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100427# else
428static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
429# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100430# endif
431
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200432# if defined(RUBY19_OR_LATER) && !defined(PROTO)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200433# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
434long rb_num2long_stub(VALUE x)
435# else
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100436SIGNED_VALUE rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200437# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100438{
439 return dll_rb_num2long(x);
440}
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100441VALUE rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100442{
443 return dll_rb_int2big(x);
444}
Bram Moolenaar498af702014-03-28 21:58:21 +0100445# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \
446 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200447long rb_fix2int_stub(VALUE x)
448{
449 return dll_rb_fix2int(x);
450}
451long rb_num2int_stub(VALUE x)
452{
453 return dll_rb_num2int(x);
454}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200455# endif
456# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaar886ed692013-02-26 13:41:35 +0100457VALUE
458rb_float_new_in_heap(double d)
459{
460 return dll_rb_float_new(d);
461}
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200462# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
463unsigned long rb_num2ulong(VALUE x)
464# else
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100465VALUE rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200466# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100467{
468 return (long)RSHIFT((SIGNED_VALUE)(x),1);
469}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200470# endif
471# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100472
Bram Moolenaar3e9a1612014-11-12 16:05:04 +0100473 /* Do not generate a prototype here, VALUE isn't always defined. */
474# if defined(USE_RGENGC) && USE_RGENGC && !defined(PROTO)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100475# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100476void rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
477{
Bram Moolenaar90140742014-11-27 17:44:08 +0100478 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100479}
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100480# else
481void rb_gc_writebarrier_unprotect_stub(VALUE obj)
482{
483 dll_rb_gc_writebarrier_unprotect(obj);
484}
485# endif
486# endif
487
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200488static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489
490/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000491 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493static struct
494{
495 char *name;
496 RUBY_PROC *ptr;
497} ruby_funcname_table[] =
498{
499 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
500 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
501 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200502# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100503 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200504# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
506 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
507 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
508 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
509 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100510# ifdef USE_TYPEDDATA
511 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
512# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100514# ifdef USE_TYPEDDATA
515# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
516 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
517# else
518 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
519# endif
520# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100522# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
524 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
525 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
526 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
527 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
528 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
529 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
530 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
531 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
532 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
533 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
534 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
535 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
536 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
537 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
538 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
539 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
540 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
541 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100542# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200543 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
544 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
545 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200546# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
548 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
549 {"rb_load_protect", (RUBY_PROC*)&dll_rb_load_protect},
550 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
551 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
552 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
553 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
554 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
555 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200556# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200557 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200558# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200560# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
562 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
563 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200564# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200565 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200566# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200568# endif
569# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100570 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200571# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200573# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
575 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200576# ifdef WIN3264
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100577 {
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200578# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER < 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100579 "NtInitialize",
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200580# else
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100581 "ruby_sysinit",
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200582# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100583 (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200584# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200586# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200587# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200588# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100589 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200590# if DYNAMIC_RUBY_VER <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100591 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200592# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100593 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200594# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100595 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
596 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200597# endif
598# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100599 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100600 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
601 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
602 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
603 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
604 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100605 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100606 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200607# endif
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200608# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
609# ifdef __ia64
610 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
611# endif
612 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
613# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100614# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100615# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100616 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100617# else
618 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
619# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100620# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 {"", NULL},
622};
623
624/*
625 * Free ruby.dll
626 */
627 static void
628end_dynamic_ruby()
629{
630 if (hinstRuby)
631 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200632 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200633 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 }
635}
636
637/*
638 * Load library and get all pointers.
639 * Parameter 'libname' provides name of DLL.
640 * Return OK or FAIL.
641 */
642 static int
643ruby_runtime_link_init(char *libname, int verbose)
644{
645 int i;
646
647 if (hinstRuby)
648 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200649 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 if (!hinstRuby)
651 {
652 if (verbose)
653 EMSG2(_(e_loadlib), libname);
654 return FAIL;
655 }
656
657 for (i = 0; ruby_funcname_table[i].ptr; ++i)
658 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200659 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 ruby_funcname_table[i].name)))
661 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200662 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200663 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 if (verbose)
665 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
666 return FAIL;
667 }
668 }
669 return OK;
670}
671
672/*
673 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
674 * else FALSE.
675 */
676 int
677ruby_enabled(verbose)
678 int verbose;
679{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100680 return ruby_runtime_link_init((char *)p_rubydll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681}
682#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
683
684 void
685ruby_end()
686{
687#ifdef DYNAMIC_RUBY
688 end_dynamic_ruby();
689#endif
690}
691
692void ex_ruby(exarg_T *eap)
693{
694 int state;
695 char *script = NULL;
696
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000697 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 if (!eap->skip && ensure_ruby_initialized())
699 {
700 if (script == NULL)
701 rb_eval_string_protect((char *)eap->arg, &state);
702 else
703 rb_eval_string_protect(script, &state);
704 if (state)
705 error_print(state);
706 }
707 vim_free(script);
708}
709
Bram Moolenaar165641d2010-02-17 16:23:09 +0100710/*
711 * In Ruby 1.9 or later, ruby String object has encoding.
712 * conversion buffer string of vim to ruby String object using
713 * VIM encoding option.
714 */
715 static VALUE
716vim_str2rb_enc_str(const char *s)
717{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100718#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100719 int isnum;
720 long lval;
721 char_u *sval;
722 rb_encoding *enc;
723
724 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
725 if (isnum == 0)
726 {
727 enc = rb_enc_find((char *)sval);
728 vim_free(sval);
729 if (enc) {
730 return rb_enc_str_new(s, strlen(s), enc);
731 }
732 }
733#endif
734 return rb_str_new2(s);
735}
736
737 static VALUE
738eval_enc_string_protect(const char *str, int *state)
739{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100740#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100741 int isnum;
742 long lval;
743 char_u *sval;
744 rb_encoding *enc;
745 VALUE v;
746
747 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
748 if (isnum == 0)
749 {
750 enc = rb_enc_find((char *)sval);
751 vim_free(sval);
752 if (enc)
753 {
754 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
755 return rb_eval_string_protect(StringValuePtr(v), state);
756 }
757 }
758#endif
759 return rb_eval_string_protect(str, state);
760}
761
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762void ex_rubydo(exarg_T *eap)
763{
764 int state;
765 linenr_T i;
766
767 if (ensure_ruby_initialized())
768 {
769 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
770 return;
771 for (i = eap->line1; i <= eap->line2; i++) {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100772 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100774 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100776 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 if (state) {
778 error_print(state);
779 break;
780 }
781 line = rb_lastline_get();
782 if (!NIL_P(line)) {
783 if (TYPE(line) != T_STRING) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000784 EMSG(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 return;
786 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100787 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 changed();
789#ifdef SYNTAX_HL
790 syn_changed(i); /* recompute syntax hl. for this line */
791#endif
792 }
793 }
794 check_cursor();
795 update_curbuf(NOT_VALID);
796 }
797}
798
799void ex_rubyfile(exarg_T *eap)
800{
801 int state;
802
803 if (ensure_ruby_initialized())
804 {
805 rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
806 if (state) error_print(state);
807 }
808}
809
810void ruby_buffer_free(buf_T *buf)
811{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000812 if (buf->b_ruby_ref)
813 {
814 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
815 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 }
817}
818
819void ruby_window_free(win_T *win)
820{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000821 if (win->w_ruby_ref)
822 {
823 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
824 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 }
826}
827
828static int ensure_ruby_initialized(void)
829{
830 if (!ruby_initialized)
831 {
832#ifdef DYNAMIC_RUBY
833 if (ruby_enabled(TRUE))
834 {
835#endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100836#ifdef _WIN32
837 /* suggested by Ariya Mizutani */
838 int argc = 1;
839 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +0100840 char **argvp = argv;
841 NtInitialize(&argc, &argvp);
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100842#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200843 {
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200844#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +0200845 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100846#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200847 ruby_init();
848 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100849#ifdef RUBY19_OR_LATER
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100850 {
851 int dummy_argc = 2;
852 char *dummy_argv[] = {"vim-ruby", "-e0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100853 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100854 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100855 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100856#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100858#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100859 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 ruby_vim_init();
861 ruby_initialized = 1;
862#ifdef DYNAMIC_RUBY
863 }
864 else
865 {
866 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
867 return 0;
868 }
869#endif
870 }
871 return ruby_initialized;
872}
873
874static void error_print(int state)
875{
876#ifndef DYNAMIC_RUBY
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100877#if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
878 && !(defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 RUBYEXTERN VALUE ruby_errinfo;
880#endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 VALUE eclass;
883 VALUE einfo;
884 char buff[BUFSIZ];
885
886#define TAG_RETURN 0x1
887#define TAG_BREAK 0x2
888#define TAG_NEXT 0x3
889#define TAG_RETRY 0x4
890#define TAG_REDO 0x5
891#define TAG_RAISE 0x6
892#define TAG_THROW 0x7
893#define TAG_FATAL 0x8
894#define TAG_MASK 0xf
895
896 switch (state) {
897 case TAG_RETURN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000898 EMSG(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 break;
900 case TAG_NEXT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000901 EMSG(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 break;
903 case TAG_BREAK:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000904 EMSG(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 break;
906 case TAG_REDO:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000907 EMSG(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 break;
909 case TAG_RETRY:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000910 EMSG(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 break;
912 case TAG_RAISE:
913 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +0100914#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100915 eclass = CLASS_OF(rb_errinfo());
916 einfo = rb_obj_as_string(rb_errinfo());
917#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 eclass = CLASS_OF(ruby_errinfo);
919 einfo = rb_obj_as_string(ruby_errinfo);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100920#endif
921 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000922 EMSG(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 }
924 else {
925 VALUE epath;
926 char *p;
927
928 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000929 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar165641d2010-02-17 16:23:09 +0100930 RSTRING_PTR(epath), RSTRING_PTR(einfo));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 p = strchr(buff, '\n');
932 if (p) *p = '\0';
933 EMSG(buff);
934 }
935 break;
936 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000937 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 EMSG(buff);
939 break;
940 }
941}
942
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000943static VALUE vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944{
945 char *buff, *p;
946
947 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +0200948 if (RSTRING_LEN(str) > 0)
949 {
950 /* Only do this when the string isn't empty, alloc(0) causes trouble. */
951 buff = ALLOCA_N(char, RSTRING_LEN(str));
952 strcpy(buff, RSTRING_PTR(str));
953 p = strchr(buff, '\n');
954 if (p) *p = '\0';
955 MSG(buff);
956 }
957 else
958 {
959 MSG("");
960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 return Qnil;
962}
963
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000964static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100966 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 update_screen(NOT_VALID);
968 return Qnil;
969}
970
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000971static VALUE vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100973 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 return Qnil;
975}
976
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100977#ifdef FEAT_EVAL
978static VALUE vim_to_ruby(typval_T *tv)
979{
980 VALUE result = Qnil;
981
982 if (tv->v_type == VAR_STRING)
983 {
Bram Moolenaar94127e42010-03-19 23:08:48 +0100984 result = rb_str_new2(tv->vval.v_string == NULL
985 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100986 }
987 else if (tv->v_type == VAR_NUMBER)
988 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100989 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100990 }
991# ifdef FEAT_FLOAT
992 else if (tv->v_type == VAR_FLOAT)
993 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100994 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100995 }
996# endif
997 else if (tv->v_type == VAR_LIST)
998 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100999 list_T *list = tv->vval.v_list;
1000 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001001
Bram Moolenaar639a2552010-03-17 18:15:23 +01001002 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001003
Bram Moolenaar639a2552010-03-17 18:15:23 +01001004 if (list != NULL)
1005 {
1006 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
1007 {
1008 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
1009 }
1010 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001011 }
1012 else if (tv->v_type == VAR_DICT)
1013 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001014 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001015
Bram Moolenaar639a2552010-03-17 18:15:23 +01001016 if (tv->vval.v_dict != NULL)
1017 {
1018 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1019 long_u todo = ht->ht_used;
1020 hashitem_T *hi;
1021 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001022
Bram Moolenaar639a2552010-03-17 18:15:23 +01001023 for (hi = ht->ht_array; todo > 0; ++hi)
1024 {
1025 if (!HASHITEM_EMPTY(hi))
1026 {
1027 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001028
Bram Moolenaar639a2552010-03-17 18:15:23 +01001029 di = dict_lookup(hi);
1030 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001031 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001032 }
1033 }
1034 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001035 } /* else return Qnil; */
1036
1037 return result;
1038}
1039#endif
1040
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001041static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042{
1043#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001044 typval_T *tv;
1045 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001047 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1048 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001050 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001052 result = vim_to_ruby(tv);
1053
1054 free_tv(tv);
1055
1056 return result;
1057#else
1058 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060}
1061
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001062#ifdef USE_TYPEDDATA
1063static size_t buffer_dsize(const void *buf);
1064
1065static const rb_data_type_t buffer_type = {
1066 "vim_buffer",
1067 {0, 0, buffer_dsize, {0, 0}},
1068 0, 0,
1069# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1070 0,
1071# endif
1072};
1073
1074static size_t buffer_dsize(const void *buf UNUSED)
1075{
1076 return sizeof(buf_T);
1077}
1078#endif
1079
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080static VALUE buffer_new(buf_T *buf)
1081{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001082 if (buf->b_ruby_ref)
1083 {
1084 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001086 else
1087 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001088#ifdef USE_TYPEDDATA
1089 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1090#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001092#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001093 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1095 return obj;
1096 }
1097}
1098
1099static buf_T *get_buf(VALUE obj)
1100{
1101 buf_T *buf;
1102
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001103#ifdef USE_TYPEDDATA
1104 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1105#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 if (buf == NULL)
1109 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1110 return buf;
1111}
1112
1113static VALUE buffer_s_current()
1114{
1115 return buffer_new(curbuf);
1116}
1117
1118static VALUE buffer_s_count()
1119{
1120 buf_T *b;
1121 int n = 0;
1122
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001123 for (b = firstbuf; b != NULL; b = b->b_next)
1124 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001125 /* Deleted buffers should not be counted
1126 * SegPhault - 01/07/05 */
1127 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001128 n++;
1129 }
1130
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 return INT2NUM(n);
1132}
1133
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001134static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135{
1136 buf_T *b;
1137 int n = NUM2INT(num);
1138
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001139 for (b = firstbuf; b != NULL; b = b->b_next)
1140 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001141 /* Deleted buffers should not be counted
1142 * SegPhault - 01/07/05 */
1143 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001144 continue;
1145
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001146 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001148
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001149 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 }
1151 return Qnil;
1152}
1153
1154static VALUE buffer_name(VALUE self)
1155{
1156 buf_T *buf = get_buf(self);
1157
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001158 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159}
1160
1161static VALUE buffer_number(VALUE self)
1162{
1163 buf_T *buf = get_buf(self);
1164
1165 return INT2NUM(buf->b_fnum);
1166}
1167
1168static VALUE buffer_count(VALUE self)
1169{
1170 buf_T *buf = get_buf(self);
1171
1172 return INT2NUM(buf->b_ml.ml_line_count);
1173}
1174
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001175static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001177 if (n <= 0 || n > buf->b_ml.ml_line_count)
1178 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1179 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180}
1181
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001182static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183{
1184 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001185
1186 if (buf != NULL)
1187 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
1188 return Qnil; /* For stop warning */
1189}
1190
1191static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
1192{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001193 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001194 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001196 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1197 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001198 /* set curwin/curbuf for "buf" and save some things */
1199 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001200
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 if (u_savesub(n) == OK) {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001202 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 changed();
1204#ifdef SYNTAX_HL
1205 syn_changed(n); /* recompute syntax hl. for this line */
1206#endif
1207 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001208
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001209 /* restore curwin/curbuf and a few other things */
1210 aucmd_restbuf(&aco);
1211 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001212
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 update_curbuf(NOT_VALID);
1214 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001215 else
1216 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001217 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 }
1219 return str;
1220}
1221
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001222static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
1223{
1224 buf_T *buf = get_buf(self);
1225
1226 if (buf != NULL)
1227 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1228 return str;
1229}
1230
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231static VALUE buffer_delete(VALUE self, VALUE num)
1232{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001233 buf_T *buf = get_buf(self);
1234 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001235 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001237 if (n > 0 && n <= buf->b_ml.ml_line_count)
1238 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001239 /* set curwin/curbuf for "buf" and save some things */
1240 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001241
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 if (u_savedel(n, 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001244
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001245 /* Changes to non-active buffers should properly refresh
1246 * SegPhault - 01/09/05 */
1247 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001248
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 changed();
1250 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001251
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001252 /* restore curwin/curbuf and a few other things */
1253 aucmd_restbuf(&aco);
1254 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001255
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 update_curbuf(NOT_VALID);
1257 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001258 else
1259 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001260 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 }
1262 return Qnil;
1263}
1264
1265static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
1266{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001267 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001268 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001269 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001270 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271
Bram Moolenaar3c531602010-11-16 14:46:19 +01001272 if (line == NULL)
1273 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001274 rb_raise(rb_eIndexError, "NULL line");
1275 }
1276 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001277 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001278 /* set curwin/curbuf for "buf" and save some things */
1279 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001280
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 if (u_inssub(n + 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001283
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001284 /* Changes to non-active buffers should properly refresh screen
1285 * SegPhault - 12/20/04 */
1286 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001287
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001288 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001290
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001291 /* restore curwin/curbuf and a few other things */
1292 aucmd_restbuf(&aco);
1293 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001294
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 update_curbuf(NOT_VALID);
1296 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001297 else
1298 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001299 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 }
1301 return str;
1302}
1303
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001304#ifdef USE_TYPEDDATA
1305static size_t window_dsize(const void *buf);
1306
1307static const rb_data_type_t window_type = {
1308 "vim_window",
1309 {0, 0, window_dsize, {0, 0}},
1310 0, 0,
1311# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1312 0,
1313# endif
1314};
1315
1316static size_t window_dsize(const void *win UNUSED)
1317{
1318 return sizeof(win_T);
1319}
1320#endif
1321
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322static VALUE window_new(win_T *win)
1323{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001324 if (win->w_ruby_ref)
1325 {
1326 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001328 else
1329 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001330#ifdef USE_TYPEDDATA
1331 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1332#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001334#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001335 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1337 return obj;
1338 }
1339}
1340
1341static win_T *get_win(VALUE obj)
1342{
1343 win_T *win;
1344
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001345#ifdef USE_TYPEDDATA
1346 TypedData_Get_Struct(obj, win_T, &window_type, win);
1347#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 if (win == NULL)
1351 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1352 return win;
1353}
1354
1355static VALUE window_s_current()
1356{
1357 return window_new(curwin);
1358}
1359
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001360/*
1361 * Added line manipulation functions
1362 * SegPhault - 03/07/05
1363 */
1364static VALUE line_s_current()
1365{
1366 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1367}
1368
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001369static VALUE set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001370{
1371 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1372}
1373
1374static VALUE current_line_number()
1375{
1376 return INT2FIX((int)curwin->w_cursor.lnum);
1377}
1378
1379
1380
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381static VALUE window_s_count()
1382{
1383#ifdef FEAT_WINDOWS
1384 win_T *w;
1385 int n = 0;
1386
Bram Moolenaarf740b292006-02-16 22:11:02 +00001387 for (w = firstwin; w != NULL; w = w->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 n++;
1389 return INT2NUM(n);
1390#else
1391 return INT2NUM(1);
1392#endif
1393}
1394
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001395static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396{
1397 win_T *w;
1398 int n = NUM2INT(num);
1399
1400#ifndef FEAT_WINDOWS
1401 w = curwin;
1402#else
1403 for (w = firstwin; w != NULL; w = w->w_next, --n)
1404#endif
1405 if (n == 0)
1406 return window_new(w);
1407 return Qnil;
1408}
1409
1410static VALUE window_buffer(VALUE self)
1411{
1412 win_T *win = get_win(self);
1413
1414 return buffer_new(win->w_buffer);
1415}
1416
1417static VALUE window_height(VALUE self)
1418{
1419 win_T *win = get_win(self);
1420
1421 return INT2NUM(win->w_height);
1422}
1423
1424static VALUE window_set_height(VALUE self, VALUE height)
1425{
1426 win_T *win = get_win(self);
1427 win_T *savewin = curwin;
1428
1429 curwin = win;
1430 win_setheight(NUM2INT(height));
1431 curwin = savewin;
1432 return height;
1433}
1434
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001435static VALUE window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001436{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001437 return INT2NUM(W_WIDTH(get_win(self)));
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001438}
1439
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001440static VALUE window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001441{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001442#ifdef FEAT_VERTSPLIT
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001443 win_T *win = get_win(self);
1444 win_T *savewin = curwin;
1445
1446 curwin = win;
1447 win_setwidth(NUM2INT(width));
1448 curwin = savewin;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001449#endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001450 return width;
1451}
1452
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453static VALUE window_cursor(VALUE self)
1454{
1455 win_T *win = get_win(self);
1456
1457 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1458}
1459
1460static VALUE window_set_cursor(VALUE self, VALUE pos)
1461{
1462 VALUE lnum, col;
1463 win_T *win = get_win(self);
1464
1465 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001466 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001468 lnum = RARRAY_PTR(pos)[0];
1469 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 win->w_cursor.lnum = NUM2LONG(lnum);
1471 win->w_cursor.col = NUM2UINT(col);
1472 check_cursor(); /* put cursor on an existing line */
1473 update_screen(NOT_VALID);
1474 return Qnil;
1475}
1476
Bram Moolenaar6217cdc2012-04-25 12:28:09 +02001477static VALUE f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001478{
1479 return Qnil;
1480}
1481
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001482static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483{
1484 int i;
1485 VALUE str = rb_str_new("", 0);
1486
1487 for (i = 0; i < argc; i++) {
1488 if (i > 0) rb_str_cat(str, ", ", 2);
1489 rb_str_concat(str, rb_inspect(argv[i]));
1490 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001491 MSG(RSTRING_PTR(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 return Qnil;
1493}
1494
1495static void ruby_io_init(void)
1496{
1497#ifndef DYNAMIC_RUBY
1498 RUBYEXTERN VALUE rb_stdout;
1499#endif
1500
1501 rb_stdout = rb_obj_alloc(rb_cObject);
1502 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001503 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 rb_define_global_function("p", f_p, -1);
1505}
1506
1507static void ruby_vim_init(void)
1508{
1509 objtbl = rb_hash_new();
1510 rb_global_variable(&objtbl);
1511
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001512 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001513 * alias "VIM" for backwards compatibility. */
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001514 mVIM = rb_define_module("Vim");
1515 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1517 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1518 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1519 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1520 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1521 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1522 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1523 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1524 rb_define_module_function(mVIM, "message", vim_message, 1);
1525 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1526 rb_define_module_function(mVIM, "command", vim_command, 1);
1527 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
1528
1529 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1530 rb_eStandardError);
1531 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1532 rb_eStandardError);
1533
1534 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1535 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1536 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1537 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1538 rb_define_method(cBuffer, "name", buffer_name, 0);
1539 rb_define_method(cBuffer, "number", buffer_number, 0);
1540 rb_define_method(cBuffer, "count", buffer_count, 0);
1541 rb_define_method(cBuffer, "length", buffer_count, 0);
1542 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1543 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1544 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1545 rb_define_method(cBuffer, "append", buffer_append, 2);
1546
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001547 /* Added line manipulation functions
1548 * SegPhault - 03/07/05 */
1549 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1550 rb_define_method(cBuffer, "line", line_s_current, 0);
1551 rb_define_method(cBuffer, "line=", set_current_line, 1);
1552
1553
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1555 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1556 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1557 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1558 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1559 rb_define_method(cVimWindow, "height", window_height, 0);
1560 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001561 rb_define_method(cVimWindow, "width", window_width, 0);
1562 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1564 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1565
1566 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1567 rb_define_virtual_variable("$curwin", window_s_current, 0);
1568}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001569
1570void vim_ruby_init(void *stack_start)
1571{
1572 /* should get machine stack start address early in main function */
1573 ruby_stack_start = stack_start;
1574}