blob: 51a9f03024766eee5b8d96fc98c368cf563c6f60 [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 Moolenaar42d57f02010-03-10 12:47:00 +010084#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
85/* Ruby 1.9 defines a number of static functions which use rb_num2long and
86 * rb_int2big */
87# define rb_num2long rb_num2long_stub
88# define rb_int2big rb_int2big_stub
89#endif
90
Bram Moolenaar498af702014-03-28 21:58:21 +010091#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \
92 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
93/* Ruby 1.9 defines a number of static functions which use rb_fix2int and
Bram Moolenaara2aa31a2014-02-23 22:52:40 +010094 * rb_num2int if VIM_SIZEOF_INT < VIM_SIZEOF_LONG (64bit) */
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +020095# define rb_fix2int rb_fix2int_stub
96# define rb_num2int rb_num2int_stub
97#endif
98
Bram Moolenaar0c7485f2015-01-14 14:04:10 +010099#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100100/* Ruby 2.1 adds new GC called RGenGC and RARRAY_PTR uses
101 * rb_gc_writebarrier_unprotect_promoted if USE_RGENGC */
Bram Moolenaar90140742014-11-27 17:44:08 +0100102# define rb_gc_writebarrier_unprotect_promoted rb_gc_writebarrier_unprotect_promoted_stub
103#endif
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100104#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
105# define rb_gc_writebarrier_unprotect rb_gc_writebarrier_unprotect_stub
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100106#endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100107
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#include <ruby.h>
Bram Moolenaar639a2552010-03-17 18:15:23 +0100109#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100110# include <ruby/encoding.h>
111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100113#undef off_t /* ruby defines off_t as _int64, Mingw uses long */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#undef EXTERN
115#undef _
116
117/* T_DATA defined both by Ruby and Mac header files, hack around it... */
Bram Moolenaarcb635362007-05-12 13:02:42 +0000118#if defined(MACOS_X_UNIX) || defined(macintosh)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119# define __OPENTRANSPORT__
120# define __OPENTRANSPORTPROTOCOL__
121# define __OPENTRANSPORTPROVIDERS__
122#endif
123
Bram Moolenaar165641d2010-02-17 16:23:09 +0100124/*
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100125 * The TypedData_XXX macro family can be used since Ruby 1.9.2, and
126 * the old Data_XXX macro family was deprecated on Ruby 2.2.
127 * Use TypedData_XXX if available.
128 */
129#ifdef TypedData_Wrap_Struct
130# define USE_TYPEDDATA 1
131#endif
132
133/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200134 * Backward compatibility for Ruby 1.8 and earlier.
Bram Moolenaar165641d2010-02-17 16:23:09 +0100135 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
136 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
137 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
138 */
139#ifndef StringValuePtr
140# define StringValuePtr(s) STR2CSTR(s)
141#endif
142#ifndef RARRAY_LEN
143# define RARRAY_LEN(s) RARRAY(s)->len
144#endif
145#ifndef RARRAY_PTR
146# define RARRAY_PTR(s) RARRAY(s)->ptr
147#endif
148#ifndef RSTRING_LEN
149# define RSTRING_LEN(s) RSTRING(s)->len
150#endif
151#ifndef RSTRING_PTR
152# define RSTRING_PTR(s) RSTRING(s)->ptr
153#endif
154
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#include "vim.h"
156#include "version.h"
157
158#if defined(PROTO) && !defined(FEAT_RUBY)
159/* Define these to be able to generate the function prototypes. */
160# define VALUE int
161# define RUBY_DATA_FUNC int
162#endif
163
164static int ruby_initialized = 0;
Bram Moolenaar99685e62013-05-11 13:56:18 +0200165static void *ruby_stack_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166static VALUE objtbl;
167
168static VALUE mVIM;
169static VALUE cBuffer;
170static VALUE cVimWindow;
171static VALUE eDeletedBufferError;
172static VALUE eDeletedWindowError;
173
174static int ensure_ruby_initialized(void);
175static void error_print(int);
176static void ruby_io_init(void);
177static void ruby_vim_init(void);
178
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200179#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
180# if defined(__ia64) && !defined(ruby_init_stack)
181# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
182# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183#endif
184
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200185#if defined(DYNAMIC_RUBY) || defined(PROTO)
186# ifdef PROTO
187# define HINSTANCE int /* for generating prototypes */
188# endif
189
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190/*
191 * Wrapper defines
192 */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200193# define rb_assoc_new dll_rb_assoc_new
194# define rb_cObject (*dll_rb_cObject)
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100195# define rb_check_type dll_rb_check_type
196# ifdef USE_TYPEDDATA
197# define rb_check_typeddata dll_rb_check_typeddata
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100198# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200199# define rb_class_path dll_rb_class_path
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100200# ifdef USE_TYPEDDATA
201# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
202# define rb_data_typed_object_wrap dll_rb_data_typed_object_wrap
203# else
204# define rb_data_typed_object_alloc dll_rb_data_typed_object_alloc
205# endif
206# else
207# define rb_data_object_alloc dll_rb_data_object_alloc
208# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200209# define rb_define_class_under dll_rb_define_class_under
210# define rb_define_const dll_rb_define_const
211# define rb_define_global_function dll_rb_define_global_function
212# define rb_define_method dll_rb_define_method
213# define rb_define_module dll_rb_define_module
214# define rb_define_module_function dll_rb_define_module_function
215# define rb_define_singleton_method dll_rb_define_singleton_method
216# define rb_define_virtual_variable dll_rb_define_virtual_variable
217# define rb_stdout (*dll_rb_stdout)
218# define rb_eArgError (*dll_rb_eArgError)
219# define rb_eIndexError (*dll_rb_eIndexError)
220# define rb_eRuntimeError (*dll_rb_eRuntimeError)
221# define rb_eStandardError (*dll_rb_eStandardError)
222# define rb_eval_string_protect dll_rb_eval_string_protect
223# define rb_global_variable dll_rb_global_variable
224# define rb_hash_aset dll_rb_hash_aset
225# define rb_hash_new dll_rb_hash_new
226# define rb_inspect dll_rb_inspect
227# define rb_int2inum dll_rb_int2inum
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100228# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar498af702014-03-28 21:58:21 +0100229# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 18
230# define rb_fix2int dll_rb_fix2int
231# define rb_num2int dll_rb_num2int
232# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200233# define rb_num2uint dll_rb_num2uint
234# endif
235# define rb_lastline_get dll_rb_lastline_get
236# define rb_lastline_set dll_rb_lastline_set
237# define rb_load_protect dll_rb_load_protect
238# ifndef RUBY19_OR_LATER
239# define rb_num2long dll_rb_num2long
240# endif
241# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 19
242# define rb_num2ulong dll_rb_num2ulong
243# endif
244# define rb_obj_alloc dll_rb_obj_alloc
245# define rb_obj_as_string dll_rb_obj_as_string
246# define rb_obj_id dll_rb_obj_id
247# define rb_raise dll_rb_raise
248# define rb_str_cat dll_rb_str_cat
249# define rb_str_concat dll_rb_str_concat
250# define rb_str_new dll_rb_str_new
251# ifdef rb_str_new2
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200252/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200253# define need_rb_str_new_cstr 1
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200254/* Ruby's headers #define rb_str_new_cstr to make use of GCC's
255 * __builtin_constant_p extension. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200256# undef rb_str_new_cstr
257# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200258# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200259# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200260# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200261# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200262# define rb_string_value dll_rb_string_value
263# define rb_string_value_ptr dll_rb_string_value_ptr
264# define rb_float_new dll_rb_float_new
265# define rb_ary_new dll_rb_ary_new
266# define rb_ary_push dll_rb_ary_push
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200267# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
268# ifdef __ia64
269# define rb_ia64_bsp dll_rb_ia64_bsp
270# undef ruby_init_stack
271# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
272# else
273# define ruby_init_stack dll_ruby_init_stack
274# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200275# endif
276# else
277# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200278# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200279# ifdef RUBY19_OR_LATER
280# define rb_errinfo dll_rb_errinfo
281# else
282# define ruby_errinfo (*dll_ruby_errinfo)
283# endif
284# define ruby_init dll_ruby_init
285# define ruby_init_loadpath dll_ruby_init_loadpath
286# ifdef WIN3264
287# define NtInitialize dll_NtInitialize
288# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
289# define rb_w32_snprintf dll_rb_w32_snprintf
290# endif
291# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200293# ifdef RUBY19_OR_LATER
294# define ruby_script dll_ruby_script
295# define rb_enc_find_index dll_rb_enc_find_index
296# define rb_enc_find dll_rb_enc_find
297# define rb_enc_str_new dll_rb_enc_str_new
298# define rb_sprintf dll_rb_sprintf
299# define rb_require dll_rb_require
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100300# define ruby_options dll_ruby_options
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200301# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100302
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303/*
304 * Pointers for dynamic link
305 */
306static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200307VALUE *dll_rb_cFalseClass;
308VALUE *dll_rb_cFixnum;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200309# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100310VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200311# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200312VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313static VALUE *dll_rb_cObject;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200314VALUE *dll_rb_cSymbol;
315VALUE *dll_rb_cTrueClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316static void (*dll_rb_check_type) (VALUE,int);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100317# ifdef USE_TYPEDDATA
318static void *(*dll_rb_check_typeddata) (VALUE,const rb_data_type_t *);
319# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320static VALUE (*dll_rb_class_path) (VALUE);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100321# ifdef USE_TYPEDDATA
322# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
323static VALUE (*dll_rb_data_typed_object_wrap) (VALUE, void*, const rb_data_type_t *);
324# else
325static VALUE (*dll_rb_data_typed_object_alloc) (VALUE, void*, const rb_data_type_t *);
326# endif
327# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100329# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
331static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
332static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
333static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
334static VALUE (*dll_rb_define_module) (const char*);
335static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
336static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
337static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
338static VALUE *dll_rb_stdout;
339static VALUE *dll_rb_eArgError;
340static VALUE *dll_rb_eIndexError;
341static VALUE *dll_rb_eRuntimeError;
342static VALUE *dll_rb_eStandardError;
343static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
344static void (*dll_rb_global_variable) (VALUE*);
345static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
346static VALUE (*dll_rb_hash_new) (void);
347static VALUE (*dll_rb_inspect) (VALUE);
348static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100349# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200350static long (*dll_rb_fix2int) (VALUE);
351static long (*dll_rb_num2int) (VALUE);
352static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200353# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354static VALUE (*dll_rb_lastline_get) (void);
355static void (*dll_rb_lastline_set) (VALUE);
356static void (*dll_rb_load_protect) (VALUE, int, int*);
357static long (*dll_rb_num2long) (VALUE);
358static unsigned long (*dll_rb_num2ulong) (VALUE);
359static VALUE (*dll_rb_obj_alloc) (VALUE);
360static VALUE (*dll_rb_obj_as_string) (VALUE);
361static VALUE (*dll_rb_obj_id) (VALUE);
362static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200363# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200364static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200365# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200367# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
369static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
370static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200371# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200372/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
373static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200374# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200376# endif
377# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100378static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200379# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200381# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382static void (*dll_ruby_init) (void);
383static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200384# ifdef WIN3264
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100385static void (*dll_NtInitialize) (int*, char***);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200386# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200387static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200388# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200389# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200390# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100391static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
392static VALUE (*dll_rb_float_new) (double);
393static VALUE (*dll_rb_ary_new) (void);
394static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200395# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
396# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200397static void * (*dll_rb_ia64_bsp) (void);
398static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200399# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200400static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200401# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200402# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200403# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200404# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100405static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200406# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200408# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100409static void (*dll_ruby_script) (const char*);
410static int (*dll_rb_enc_find_index) (const char*);
411static rb_encoding* (*dll_rb_enc_find) (const char*);
412static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
413static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100414static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100415static void* (*ruby_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200416# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100417
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100418# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100419# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100420static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100421# else
422static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
423# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100424# endif
425
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200426# if defined(RUBY19_OR_LATER) && !defined(PROTO)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200427# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
428long rb_num2long_stub(VALUE x)
429# else
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100430SIGNED_VALUE rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200431# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100432{
433 return dll_rb_num2long(x);
434}
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100435VALUE rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100436{
437 return dll_rb_int2big(x);
438}
Bram Moolenaar498af702014-03-28 21:58:21 +0100439# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \
440 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200441long rb_fix2int_stub(VALUE x)
442{
443 return dll_rb_fix2int(x);
444}
445long rb_num2int_stub(VALUE x)
446{
447 return dll_rb_num2int(x);
448}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200449# endif
450# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaar886ed692013-02-26 13:41:35 +0100451VALUE
452rb_float_new_in_heap(double d)
453{
454 return dll_rb_float_new(d);
455}
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200456# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
457unsigned long rb_num2ulong(VALUE x)
458# else
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100459VALUE rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200460# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100461{
462 return (long)RSHIFT((SIGNED_VALUE)(x),1);
463}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200464# endif
465# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100466
Bram Moolenaar3e9a1612014-11-12 16:05:04 +0100467 /* Do not generate a prototype here, VALUE isn't always defined. */
468# if defined(USE_RGENGC) && USE_RGENGC && !defined(PROTO)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100469# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100470void rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
471{
Bram Moolenaar90140742014-11-27 17:44:08 +0100472 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100473}
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100474# else
475void rb_gc_writebarrier_unprotect_stub(VALUE obj)
476{
477 dll_rb_gc_writebarrier_unprotect(obj);
478}
479# endif
480# endif
481
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200482static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483
484/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000485 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487static struct
488{
489 char *name;
490 RUBY_PROC *ptr;
491} ruby_funcname_table[] =
492{
493 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
494 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
495 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200496# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100497 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200498# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
500 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
501 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
502 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
503 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100504# ifdef USE_TYPEDDATA
505 {"rb_check_typeddata", (RUBY_PROC*)&dll_rb_check_typeddata},
506# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100508# ifdef USE_TYPEDDATA
509# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 23
510 {"rb_data_typed_object_wrap", (RUBY_PROC*)&dll_rb_data_typed_object_wrap},
511# else
512 {"rb_data_typed_object_alloc", (RUBY_PROC*)&dll_rb_data_typed_object_alloc},
513# endif
514# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
Bram Moolenaarf2f6d292015-12-28 20:57:10 +0100516# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
518 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
519 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
520 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
521 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
522 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
523 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
524 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
525 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
526 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
527 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
528 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
529 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
530 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
531 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
532 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
533 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
534 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
535 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100536# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200537 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
538 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
539 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200540# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
542 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
543 {"rb_load_protect", (RUBY_PROC*)&dll_rb_load_protect},
544 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
545 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
546 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
547 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
548 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
549 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200550# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200551 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200552# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200554# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
556 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
557 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200558# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200559 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200560# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200562# endif
563# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100564 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200565# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200567# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
569 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200570# ifdef WIN3264
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100571 {
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200572# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER < 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100573 "NtInitialize",
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200574# else
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100575 "ruby_sysinit",
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200576# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100577 (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200578# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200580# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200581# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200582# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100583 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200584# if DYNAMIC_RUBY_VER <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100585 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200586# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100587 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200588# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100589 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
590 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200591# endif
592# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100593 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100594 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
595 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
596 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
597 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
598 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100599 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100600 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200601# endif
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200602# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
603# ifdef __ia64
604 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
605# endif
606 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
607# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100608# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100609# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100610 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100611# else
612 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
613# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100614# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 {"", NULL},
616};
617
618/*
619 * Free ruby.dll
620 */
621 static void
622end_dynamic_ruby()
623{
624 if (hinstRuby)
625 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200626 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200627 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 }
629}
630
631/*
632 * Load library and get all pointers.
633 * Parameter 'libname' provides name of DLL.
634 * Return OK or FAIL.
635 */
636 static int
637ruby_runtime_link_init(char *libname, int verbose)
638{
639 int i;
640
641 if (hinstRuby)
642 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200643 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 if (!hinstRuby)
645 {
646 if (verbose)
647 EMSG2(_(e_loadlib), libname);
648 return FAIL;
649 }
650
651 for (i = 0; ruby_funcname_table[i].ptr; ++i)
652 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200653 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 ruby_funcname_table[i].name)))
655 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200656 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200657 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 if (verbose)
659 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
660 return FAIL;
661 }
662 }
663 return OK;
664}
665
666/*
667 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
668 * else FALSE.
669 */
670 int
671ruby_enabled(verbose)
672 int verbose;
673{
Bram Moolenaard94464e2015-11-02 15:28:18 +0100674#ifdef WIN3264
675 char *dll = DYNAMIC_RUBY_DLL;
676#else
677 char *dll = *p_rubydll ? (char *)p_rubydll : DYNAMIC_RUBY_DLL;
678#endif
679 return ruby_runtime_link_init(dll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680}
681#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
682
683 void
684ruby_end()
685{
686#ifdef DYNAMIC_RUBY
687 end_dynamic_ruby();
688#endif
689}
690
691void ex_ruby(exarg_T *eap)
692{
693 int state;
694 char *script = NULL;
695
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000696 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 if (!eap->skip && ensure_ruby_initialized())
698 {
699 if (script == NULL)
700 rb_eval_string_protect((char *)eap->arg, &state);
701 else
702 rb_eval_string_protect(script, &state);
703 if (state)
704 error_print(state);
705 }
706 vim_free(script);
707}
708
Bram Moolenaar165641d2010-02-17 16:23:09 +0100709/*
710 * In Ruby 1.9 or later, ruby String object has encoding.
711 * conversion buffer string of vim to ruby String object using
712 * VIM encoding option.
713 */
714 static VALUE
715vim_str2rb_enc_str(const char *s)
716{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100717#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100718 int isnum;
719 long lval;
720 char_u *sval;
721 rb_encoding *enc;
722
723 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
724 if (isnum == 0)
725 {
726 enc = rb_enc_find((char *)sval);
727 vim_free(sval);
728 if (enc) {
729 return rb_enc_str_new(s, strlen(s), enc);
730 }
731 }
732#endif
733 return rb_str_new2(s);
734}
735
736 static VALUE
737eval_enc_string_protect(const char *str, int *state)
738{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100739#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100740 int isnum;
741 long lval;
742 char_u *sval;
743 rb_encoding *enc;
744 VALUE v;
745
746 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
747 if (isnum == 0)
748 {
749 enc = rb_enc_find((char *)sval);
750 vim_free(sval);
751 if (enc)
752 {
753 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
754 return rb_eval_string_protect(StringValuePtr(v), state);
755 }
756 }
757#endif
758 return rb_eval_string_protect(str, state);
759}
760
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761void ex_rubydo(exarg_T *eap)
762{
763 int state;
764 linenr_T i;
765
766 if (ensure_ruby_initialized())
767 {
768 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
769 return;
770 for (i = eap->line1; i <= eap->line2; i++) {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100771 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100773 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100775 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 if (state) {
777 error_print(state);
778 break;
779 }
780 line = rb_lastline_get();
781 if (!NIL_P(line)) {
782 if (TYPE(line) != T_STRING) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000783 EMSG(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 return;
785 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100786 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 changed();
788#ifdef SYNTAX_HL
789 syn_changed(i); /* recompute syntax hl. for this line */
790#endif
791 }
792 }
793 check_cursor();
794 update_curbuf(NOT_VALID);
795 }
796}
797
798void ex_rubyfile(exarg_T *eap)
799{
800 int state;
801
802 if (ensure_ruby_initialized())
803 {
804 rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
805 if (state) error_print(state);
806 }
807}
808
809void ruby_buffer_free(buf_T *buf)
810{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000811 if (buf->b_ruby_ref)
812 {
813 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
814 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 }
816}
817
818void ruby_window_free(win_T *win)
819{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000820 if (win->w_ruby_ref)
821 {
822 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
823 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 }
825}
826
827static int ensure_ruby_initialized(void)
828{
829 if (!ruby_initialized)
830 {
831#ifdef DYNAMIC_RUBY
832 if (ruby_enabled(TRUE))
833 {
834#endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100835#ifdef _WIN32
836 /* suggested by Ariya Mizutani */
837 int argc = 1;
838 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +0100839 char **argvp = argv;
840 NtInitialize(&argc, &argvp);
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100841#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200842 {
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200843#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +0200844 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100845#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200846 ruby_init();
847 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100848#ifdef RUBY19_OR_LATER
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100849 {
850 int dummy_argc = 2;
851 char *dummy_argv[] = {"vim-ruby", "-e0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100852 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100853 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100854 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100855#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100857#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100858 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 ruby_vim_init();
860 ruby_initialized = 1;
861#ifdef DYNAMIC_RUBY
862 }
863 else
864 {
865 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
866 return 0;
867 }
868#endif
869 }
870 return ruby_initialized;
871}
872
873static void error_print(int state)
874{
875#ifndef DYNAMIC_RUBY
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100876#if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
877 && !(defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 RUBYEXTERN VALUE ruby_errinfo;
879#endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 VALUE eclass;
882 VALUE einfo;
883 char buff[BUFSIZ];
884
885#define TAG_RETURN 0x1
886#define TAG_BREAK 0x2
887#define TAG_NEXT 0x3
888#define TAG_RETRY 0x4
889#define TAG_REDO 0x5
890#define TAG_RAISE 0x6
891#define TAG_THROW 0x7
892#define TAG_FATAL 0x8
893#define TAG_MASK 0xf
894
895 switch (state) {
896 case TAG_RETURN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000897 EMSG(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 break;
899 case TAG_NEXT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000900 EMSG(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 break;
902 case TAG_BREAK:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000903 EMSG(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 break;
905 case TAG_REDO:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000906 EMSG(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 break;
908 case TAG_RETRY:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000909 EMSG(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 break;
911 case TAG_RAISE:
912 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +0100913#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100914 eclass = CLASS_OF(rb_errinfo());
915 einfo = rb_obj_as_string(rb_errinfo());
916#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 eclass = CLASS_OF(ruby_errinfo);
918 einfo = rb_obj_as_string(ruby_errinfo);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100919#endif
920 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000921 EMSG(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 }
923 else {
924 VALUE epath;
925 char *p;
926
927 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000928 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar165641d2010-02-17 16:23:09 +0100929 RSTRING_PTR(epath), RSTRING_PTR(einfo));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 p = strchr(buff, '\n');
931 if (p) *p = '\0';
932 EMSG(buff);
933 }
934 break;
935 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000936 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 EMSG(buff);
938 break;
939 }
940}
941
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000942static VALUE vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943{
944 char *buff, *p;
945
946 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +0200947 if (RSTRING_LEN(str) > 0)
948 {
949 /* Only do this when the string isn't empty, alloc(0) causes trouble. */
950 buff = ALLOCA_N(char, RSTRING_LEN(str));
951 strcpy(buff, RSTRING_PTR(str));
952 p = strchr(buff, '\n');
953 if (p) *p = '\0';
954 MSG(buff);
955 }
956 else
957 {
958 MSG("");
959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 return Qnil;
961}
962
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000963static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100965 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 update_screen(NOT_VALID);
967 return Qnil;
968}
969
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000970static VALUE vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100972 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 return Qnil;
974}
975
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100976#ifdef FEAT_EVAL
977static VALUE vim_to_ruby(typval_T *tv)
978{
979 VALUE result = Qnil;
980
981 if (tv->v_type == VAR_STRING)
982 {
Bram Moolenaar94127e42010-03-19 23:08:48 +0100983 result = rb_str_new2(tv->vval.v_string == NULL
984 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100985 }
986 else if (tv->v_type == VAR_NUMBER)
987 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100988 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100989 }
990# ifdef FEAT_FLOAT
991 else if (tv->v_type == VAR_FLOAT)
992 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100993 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100994 }
995# endif
996 else if (tv->v_type == VAR_LIST)
997 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100998 list_T *list = tv->vval.v_list;
999 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001000
Bram Moolenaar639a2552010-03-17 18:15:23 +01001001 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001002
Bram Moolenaar639a2552010-03-17 18:15:23 +01001003 if (list != NULL)
1004 {
1005 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
1006 {
1007 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
1008 }
1009 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001010 }
1011 else if (tv->v_type == VAR_DICT)
1012 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001013 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001014
Bram Moolenaar639a2552010-03-17 18:15:23 +01001015 if (tv->vval.v_dict != NULL)
1016 {
1017 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
1018 long_u todo = ht->ht_used;
1019 hashitem_T *hi;
1020 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001021
Bram Moolenaar639a2552010-03-17 18:15:23 +01001022 for (hi = ht->ht_array; todo > 0; ++hi)
1023 {
1024 if (!HASHITEM_EMPTY(hi))
1025 {
1026 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001027
Bram Moolenaar639a2552010-03-17 18:15:23 +01001028 di = dict_lookup(hi);
1029 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001030 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +01001031 }
1032 }
1033 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001034 } /* else return Qnil; */
1035
1036 return result;
1037}
1038#endif
1039
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001040static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041{
1042#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001043 typval_T *tv;
1044 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001046 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1047 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001049 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001051 result = vim_to_ruby(tv);
1052
1053 free_tv(tv);
1054
1055 return result;
1056#else
1057 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059}
1060
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001061#ifdef USE_TYPEDDATA
1062static size_t buffer_dsize(const void *buf);
1063
1064static const rb_data_type_t buffer_type = {
1065 "vim_buffer",
1066 {0, 0, buffer_dsize, {0, 0}},
1067 0, 0,
1068# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1069 0,
1070# endif
1071};
1072
1073static size_t buffer_dsize(const void *buf UNUSED)
1074{
1075 return sizeof(buf_T);
1076}
1077#endif
1078
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079static VALUE buffer_new(buf_T *buf)
1080{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001081 if (buf->b_ruby_ref)
1082 {
1083 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001085 else
1086 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001087#ifdef USE_TYPEDDATA
1088 VALUE obj = TypedData_Wrap_Struct(cBuffer, &buffer_type, buf);
1089#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001091#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001092 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1094 return obj;
1095 }
1096}
1097
1098static buf_T *get_buf(VALUE obj)
1099{
1100 buf_T *buf;
1101
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001102#ifdef USE_TYPEDDATA
1103 TypedData_Get_Struct(obj, buf_T, &buffer_type, buf);
1104#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 Data_Get_Struct(obj, buf_T, buf);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 if (buf == NULL)
1108 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1109 return buf;
1110}
1111
1112static VALUE buffer_s_current()
1113{
1114 return buffer_new(curbuf);
1115}
1116
1117static VALUE buffer_s_count()
1118{
1119 buf_T *b;
1120 int n = 0;
1121
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001122 for (b = firstbuf; b != NULL; b = b->b_next)
1123 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001124 /* Deleted buffers should not be counted
1125 * SegPhault - 01/07/05 */
1126 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001127 n++;
1128 }
1129
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 return INT2NUM(n);
1131}
1132
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001133static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134{
1135 buf_T *b;
1136 int n = NUM2INT(num);
1137
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001138 for (b = firstbuf; b != NULL; b = b->b_next)
1139 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001140 /* Deleted buffers should not be counted
1141 * SegPhault - 01/07/05 */
1142 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001143 continue;
1144
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001145 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001147
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001148 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 }
1150 return Qnil;
1151}
1152
1153static VALUE buffer_name(VALUE self)
1154{
1155 buf_T *buf = get_buf(self);
1156
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001157 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158}
1159
1160static VALUE buffer_number(VALUE self)
1161{
1162 buf_T *buf = get_buf(self);
1163
1164 return INT2NUM(buf->b_fnum);
1165}
1166
1167static VALUE buffer_count(VALUE self)
1168{
1169 buf_T *buf = get_buf(self);
1170
1171 return INT2NUM(buf->b_ml.ml_line_count);
1172}
1173
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001174static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001176 if (n <= 0 || n > buf->b_ml.ml_line_count)
1177 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1178 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179}
1180
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001181static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182{
1183 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001184
1185 if (buf != NULL)
1186 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
1187 return Qnil; /* For stop warning */
1188}
1189
1190static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
1191{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001192 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001193 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001195 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1196 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001197 /* set curwin/curbuf for "buf" and save some things */
1198 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001199
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 if (u_savesub(n) == OK) {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001201 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 changed();
1203#ifdef SYNTAX_HL
1204 syn_changed(n); /* recompute syntax hl. for this line */
1205#endif
1206 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001207
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001208 /* restore curwin/curbuf and a few other things */
1209 aucmd_restbuf(&aco);
1210 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001211
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 update_curbuf(NOT_VALID);
1213 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001214 else
1215 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001216 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 }
1218 return str;
1219}
1220
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001221static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
1222{
1223 buf_T *buf = get_buf(self);
1224
1225 if (buf != NULL)
1226 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1227 return str;
1228}
1229
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230static VALUE buffer_delete(VALUE self, VALUE num)
1231{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001232 buf_T *buf = get_buf(self);
1233 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001234 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001236 if (n > 0 && n <= buf->b_ml.ml_line_count)
1237 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001238 /* set curwin/curbuf for "buf" and save some things */
1239 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001240
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 if (u_savedel(n, 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001243
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001244 /* Changes to non-active buffers should properly refresh
1245 * SegPhault - 01/09/05 */
1246 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001247
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 changed();
1249 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001250
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001251 /* restore curwin/curbuf and a few other things */
1252 aucmd_restbuf(&aco);
1253 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001254
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 update_curbuf(NOT_VALID);
1256 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001257 else
1258 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001259 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 }
1261 return Qnil;
1262}
1263
1264static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
1265{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001266 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001267 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001268 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001269 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270
Bram Moolenaar3c531602010-11-16 14:46:19 +01001271 if (line == NULL)
1272 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001273 rb_raise(rb_eIndexError, "NULL line");
1274 }
1275 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001276 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001277 /* set curwin/curbuf for "buf" and save some things */
1278 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001279
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 if (u_inssub(n + 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001282
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001283 /* Changes to non-active buffers should properly refresh screen
1284 * SegPhault - 12/20/04 */
1285 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001286
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001287 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001289
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001290 /* restore curwin/curbuf and a few other things */
1291 aucmd_restbuf(&aco);
1292 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001293
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 update_curbuf(NOT_VALID);
1295 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001296 else
1297 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001298 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 }
1300 return str;
1301}
1302
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001303#ifdef USE_TYPEDDATA
1304static size_t window_dsize(const void *buf);
1305
1306static const rb_data_type_t window_type = {
1307 "vim_window",
1308 {0, 0, window_dsize, {0, 0}},
1309 0, 0,
1310# ifdef RUBY_TYPED_FREE_IMMEDIATELY
1311 0,
1312# endif
1313};
1314
1315static size_t window_dsize(const void *win UNUSED)
1316{
1317 return sizeof(win_T);
1318}
1319#endif
1320
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321static VALUE window_new(win_T *win)
1322{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001323 if (win->w_ruby_ref)
1324 {
1325 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001327 else
1328 {
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001329#ifdef USE_TYPEDDATA
1330 VALUE obj = TypedData_Wrap_Struct(cVimWindow, &window_type, win);
1331#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001333#endif
Bram Moolenaare344bea2005-09-01 20:46:49 +00001334 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1336 return obj;
1337 }
1338}
1339
1340static win_T *get_win(VALUE obj)
1341{
1342 win_T *win;
1343
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001344#ifdef USE_TYPEDDATA
1345 TypedData_Get_Struct(obj, win_T, &window_type, win);
1346#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 Data_Get_Struct(obj, win_T, win);
Bram Moolenaarf2f6d292015-12-28 20:57:10 +01001348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 if (win == NULL)
1350 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1351 return win;
1352}
1353
1354static VALUE window_s_current()
1355{
1356 return window_new(curwin);
1357}
1358
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001359/*
1360 * Added line manipulation functions
1361 * SegPhault - 03/07/05
1362 */
1363static VALUE line_s_current()
1364{
1365 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1366}
1367
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001368static VALUE set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001369{
1370 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1371}
1372
1373static VALUE current_line_number()
1374{
1375 return INT2FIX((int)curwin->w_cursor.lnum);
1376}
1377
1378
1379
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380static VALUE window_s_count()
1381{
1382#ifdef FEAT_WINDOWS
1383 win_T *w;
1384 int n = 0;
1385
Bram Moolenaarf740b292006-02-16 22:11:02 +00001386 for (w = firstwin; w != NULL; w = w->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 n++;
1388 return INT2NUM(n);
1389#else
1390 return INT2NUM(1);
1391#endif
1392}
1393
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001394static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395{
1396 win_T *w;
1397 int n = NUM2INT(num);
1398
1399#ifndef FEAT_WINDOWS
1400 w = curwin;
1401#else
1402 for (w = firstwin; w != NULL; w = w->w_next, --n)
1403#endif
1404 if (n == 0)
1405 return window_new(w);
1406 return Qnil;
1407}
1408
1409static VALUE window_buffer(VALUE self)
1410{
1411 win_T *win = get_win(self);
1412
1413 return buffer_new(win->w_buffer);
1414}
1415
1416static VALUE window_height(VALUE self)
1417{
1418 win_T *win = get_win(self);
1419
1420 return INT2NUM(win->w_height);
1421}
1422
1423static VALUE window_set_height(VALUE self, VALUE height)
1424{
1425 win_T *win = get_win(self);
1426 win_T *savewin = curwin;
1427
1428 curwin = win;
1429 win_setheight(NUM2INT(height));
1430 curwin = savewin;
1431 return height;
1432}
1433
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001434static VALUE window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001435{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001436 return INT2NUM(W_WIDTH(get_win(self)));
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001437}
1438
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001439static VALUE window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001440{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001441#ifdef FEAT_VERTSPLIT
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001442 win_T *win = get_win(self);
1443 win_T *savewin = curwin;
1444
1445 curwin = win;
1446 win_setwidth(NUM2INT(width));
1447 curwin = savewin;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001448#endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001449 return width;
1450}
1451
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452static VALUE window_cursor(VALUE self)
1453{
1454 win_T *win = get_win(self);
1455
1456 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1457}
1458
1459static VALUE window_set_cursor(VALUE self, VALUE pos)
1460{
1461 VALUE lnum, col;
1462 win_T *win = get_win(self);
1463
1464 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001465 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001467 lnum = RARRAY_PTR(pos)[0];
1468 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 win->w_cursor.lnum = NUM2LONG(lnum);
1470 win->w_cursor.col = NUM2UINT(col);
1471 check_cursor(); /* put cursor on an existing line */
1472 update_screen(NOT_VALID);
1473 return Qnil;
1474}
1475
Bram Moolenaar6217cdc2012-04-25 12:28:09 +02001476static VALUE f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001477{
1478 return Qnil;
1479}
1480
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001481static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482{
1483 int i;
1484 VALUE str = rb_str_new("", 0);
1485
1486 for (i = 0; i < argc; i++) {
1487 if (i > 0) rb_str_cat(str, ", ", 2);
1488 rb_str_concat(str, rb_inspect(argv[i]));
1489 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001490 MSG(RSTRING_PTR(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 return Qnil;
1492}
1493
1494static void ruby_io_init(void)
1495{
1496#ifndef DYNAMIC_RUBY
1497 RUBYEXTERN VALUE rb_stdout;
1498#endif
1499
1500 rb_stdout = rb_obj_alloc(rb_cObject);
1501 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001502 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 rb_define_global_function("p", f_p, -1);
1504}
1505
1506static void ruby_vim_init(void)
1507{
1508 objtbl = rb_hash_new();
1509 rb_global_variable(&objtbl);
1510
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001511 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001512 * alias "VIM" for backwards compatibility. */
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001513 mVIM = rb_define_module("Vim");
1514 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1516 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1517 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1518 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1519 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1520 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1521 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1522 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1523 rb_define_module_function(mVIM, "message", vim_message, 1);
1524 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1525 rb_define_module_function(mVIM, "command", vim_command, 1);
1526 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
1527
1528 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1529 rb_eStandardError);
1530 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1531 rb_eStandardError);
1532
1533 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1534 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1535 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1536 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1537 rb_define_method(cBuffer, "name", buffer_name, 0);
1538 rb_define_method(cBuffer, "number", buffer_number, 0);
1539 rb_define_method(cBuffer, "count", buffer_count, 0);
1540 rb_define_method(cBuffer, "length", buffer_count, 0);
1541 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1542 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1543 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1544 rb_define_method(cBuffer, "append", buffer_append, 2);
1545
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001546 /* Added line manipulation functions
1547 * SegPhault - 03/07/05 */
1548 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1549 rb_define_method(cBuffer, "line", line_s_current, 0);
1550 rb_define_method(cBuffer, "line=", set_current_line, 1);
1551
1552
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1554 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1555 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1556 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1557 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1558 rb_define_method(cVimWindow, "height", window_height, 0);
1559 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001560 rb_define_method(cVimWindow, "width", window_width, 0);
1561 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1563 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1564
1565 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1566 rb_define_virtual_variable("$curwin", window_s_current, 0);
1567}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001568
1569void vim_ruby_init(void *stack_start)
1570{
1571 /* should get machine stack start address early in main function */
1572 ruby_stack_start = stack_start;
1573}