blob: 5d3089d1aa1c7f1f152a991cca6cc8c74da3db79 [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
106# define rb_check_type rb_check_type_stub
107#endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100108
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109#include <ruby.h>
Bram Moolenaar639a2552010-03-17 18:15:23 +0100110#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100111# include <ruby/encoding.h>
112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100114#undef off_t /* ruby defines off_t as _int64, Mingw uses long */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#undef EXTERN
116#undef _
117
118/* T_DATA defined both by Ruby and Mac header files, hack around it... */
Bram Moolenaarcb635362007-05-12 13:02:42 +0000119#if defined(MACOS_X_UNIX) || defined(macintosh)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120# define __OPENTRANSPORT__
121# define __OPENTRANSPORTPROTOCOL__
122# define __OPENTRANSPORTPROVIDERS__
123#endif
124
Bram Moolenaar165641d2010-02-17 16:23:09 +0100125/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200126 * Backward compatibility for Ruby 1.8 and earlier.
Bram Moolenaar165641d2010-02-17 16:23:09 +0100127 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
128 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
129 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
130 */
131#ifndef StringValuePtr
132# define StringValuePtr(s) STR2CSTR(s)
133#endif
134#ifndef RARRAY_LEN
135# define RARRAY_LEN(s) RARRAY(s)->len
136#endif
137#ifndef RARRAY_PTR
138# define RARRAY_PTR(s) RARRAY(s)->ptr
139#endif
140#ifndef RSTRING_LEN
141# define RSTRING_LEN(s) RSTRING(s)->len
142#endif
143#ifndef RSTRING_PTR
144# define RSTRING_PTR(s) RSTRING(s)->ptr
145#endif
146
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147#include "vim.h"
148#include "version.h"
149
150#if defined(PROTO) && !defined(FEAT_RUBY)
151/* Define these to be able to generate the function prototypes. */
152# define VALUE int
153# define RUBY_DATA_FUNC int
154#endif
155
156static int ruby_initialized = 0;
Bram Moolenaar99685e62013-05-11 13:56:18 +0200157static void *ruby_stack_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158static VALUE objtbl;
159
160static VALUE mVIM;
161static VALUE cBuffer;
162static VALUE cVimWindow;
163static VALUE eDeletedBufferError;
164static VALUE eDeletedWindowError;
165
166static int ensure_ruby_initialized(void);
167static void error_print(int);
168static void ruby_io_init(void);
169static void ruby_vim_init(void);
170
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200171#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
172# if defined(__ia64) && !defined(ruby_init_stack)
173# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
174# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
176
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200177#if defined(DYNAMIC_RUBY) || defined(PROTO)
178# ifdef PROTO
179# define HINSTANCE int /* for generating prototypes */
180# endif
181
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182/*
183 * Wrapper defines
184 */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200185# define rb_assoc_new dll_rb_assoc_new
186# define rb_cObject (*dll_rb_cObject)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100187# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER < 22
188# define rb_check_type dll_rb_check_type
189# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200190# define rb_class_path dll_rb_class_path
191# define rb_data_object_alloc dll_rb_data_object_alloc
192# define rb_define_class_under dll_rb_define_class_under
193# define rb_define_const dll_rb_define_const
194# define rb_define_global_function dll_rb_define_global_function
195# define rb_define_method dll_rb_define_method
196# define rb_define_module dll_rb_define_module
197# define rb_define_module_function dll_rb_define_module_function
198# define rb_define_singleton_method dll_rb_define_singleton_method
199# define rb_define_virtual_variable dll_rb_define_virtual_variable
200# define rb_stdout (*dll_rb_stdout)
201# define rb_eArgError (*dll_rb_eArgError)
202# define rb_eIndexError (*dll_rb_eIndexError)
203# define rb_eRuntimeError (*dll_rb_eRuntimeError)
204# define rb_eStandardError (*dll_rb_eStandardError)
205# define rb_eval_string_protect dll_rb_eval_string_protect
206# define rb_global_variable dll_rb_global_variable
207# define rb_hash_aset dll_rb_hash_aset
208# define rb_hash_new dll_rb_hash_new
209# define rb_inspect dll_rb_inspect
210# define rb_int2inum dll_rb_int2inum
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100211# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar498af702014-03-28 21:58:21 +0100212# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 18
213# define rb_fix2int dll_rb_fix2int
214# define rb_num2int dll_rb_num2int
215# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200216# define rb_num2uint dll_rb_num2uint
217# endif
218# define rb_lastline_get dll_rb_lastline_get
219# define rb_lastline_set dll_rb_lastline_set
220# define rb_load_protect dll_rb_load_protect
221# ifndef RUBY19_OR_LATER
222# define rb_num2long dll_rb_num2long
223# endif
224# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 19
225# define rb_num2ulong dll_rb_num2ulong
226# endif
227# define rb_obj_alloc dll_rb_obj_alloc
228# define rb_obj_as_string dll_rb_obj_as_string
229# define rb_obj_id dll_rb_obj_id
230# define rb_raise dll_rb_raise
231# define rb_str_cat dll_rb_str_cat
232# define rb_str_concat dll_rb_str_concat
233# define rb_str_new dll_rb_str_new
234# ifdef rb_str_new2
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200235/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200236# define need_rb_str_new_cstr 1
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200237/* Ruby's headers #define rb_str_new_cstr to make use of GCC's
238 * __builtin_constant_p extension. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200239# undef rb_str_new_cstr
240# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200241# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200242# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200243# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200244# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200245# define rb_string_value dll_rb_string_value
246# define rb_string_value_ptr dll_rb_string_value_ptr
247# define rb_float_new dll_rb_float_new
248# define rb_ary_new dll_rb_ary_new
249# define rb_ary_push dll_rb_ary_push
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200250# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
251# ifdef __ia64
252# define rb_ia64_bsp dll_rb_ia64_bsp
253# undef ruby_init_stack
254# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
255# else
256# define ruby_init_stack dll_ruby_init_stack
257# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200258# endif
259# else
260# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200261# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200262# ifdef RUBY19_OR_LATER
263# define rb_errinfo dll_rb_errinfo
264# else
265# define ruby_errinfo (*dll_ruby_errinfo)
266# endif
267# define ruby_init dll_ruby_init
268# define ruby_init_loadpath dll_ruby_init_loadpath
269# ifdef WIN3264
270# define NtInitialize dll_NtInitialize
271# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
272# define rb_w32_snprintf dll_rb_w32_snprintf
273# endif
274# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200276# ifdef RUBY19_OR_LATER
277# define ruby_script dll_ruby_script
278# define rb_enc_find_index dll_rb_enc_find_index
279# define rb_enc_find dll_rb_enc_find
280# define rb_enc_str_new dll_rb_enc_str_new
281# define rb_sprintf dll_rb_sprintf
282# define rb_require dll_rb_require
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100283# define ruby_options dll_ruby_options
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200284# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100285
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286/*
287 * Pointers for dynamic link
288 */
289static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200290VALUE *dll_rb_cFalseClass;
291VALUE *dll_rb_cFixnum;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200292# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100293VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200294# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200295VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296static VALUE *dll_rb_cObject;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200297VALUE *dll_rb_cSymbol;
298VALUE *dll_rb_cTrueClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299static void (*dll_rb_check_type) (VALUE,int);
300static VALUE (*dll_rb_class_path) (VALUE);
301static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
302static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
303static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
304static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
305static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
306static VALUE (*dll_rb_define_module) (const char*);
307static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
308static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
309static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
310static VALUE *dll_rb_stdout;
311static VALUE *dll_rb_eArgError;
312static VALUE *dll_rb_eIndexError;
313static VALUE *dll_rb_eRuntimeError;
314static VALUE *dll_rb_eStandardError;
315static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
316static void (*dll_rb_global_variable) (VALUE*);
317static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
318static VALUE (*dll_rb_hash_new) (void);
319static VALUE (*dll_rb_inspect) (VALUE);
320static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100321# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200322static long (*dll_rb_fix2int) (VALUE);
323static long (*dll_rb_num2int) (VALUE);
324static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200325# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326static VALUE (*dll_rb_lastline_get) (void);
327static void (*dll_rb_lastline_set) (VALUE);
328static void (*dll_rb_load_protect) (VALUE, int, int*);
329static long (*dll_rb_num2long) (VALUE);
330static unsigned long (*dll_rb_num2ulong) (VALUE);
331static VALUE (*dll_rb_obj_alloc) (VALUE);
332static VALUE (*dll_rb_obj_as_string) (VALUE);
333static VALUE (*dll_rb_obj_id) (VALUE);
334static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200335# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200336static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200337# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200339# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
341static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
342static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200343# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200344/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
345static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200346# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200348# endif
349# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100350static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200351# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200353# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354static void (*dll_ruby_init) (void);
355static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200356# ifdef WIN3264
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100357static void (*dll_NtInitialize) (int*, char***);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200358# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200359static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200360# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200361# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200362# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100363static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
364static VALUE (*dll_rb_float_new) (double);
365static VALUE (*dll_rb_ary_new) (void);
366static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200367# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
368# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200369static void * (*dll_rb_ia64_bsp) (void);
370static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200371# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200372static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200373# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200374# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200375# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200376# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100377static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200378# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200380# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100381static void (*dll_ruby_script) (const char*);
382static int (*dll_rb_enc_find_index) (const char*);
383static rb_encoding* (*dll_rb_enc_find) (const char*);
384static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
385static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100386static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100387static void* (*ruby_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200388# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100389
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100390# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100391# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100392static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100393# else
394static void (*dll_rb_gc_writebarrier_unprotect)(VALUE obj);
395# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100396# endif
397
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200398# if defined(RUBY19_OR_LATER) && !defined(PROTO)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200399# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
400long rb_num2long_stub(VALUE x)
401# else
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100402SIGNED_VALUE rb_num2long_stub(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200403# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100404{
405 return dll_rb_num2long(x);
406}
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100407VALUE rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100408{
409 return dll_rb_int2big(x);
410}
Bram Moolenaar498af702014-03-28 21:58:21 +0100411# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \
412 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200413long rb_fix2int_stub(VALUE x)
414{
415 return dll_rb_fix2int(x);
416}
417long rb_num2int_stub(VALUE x)
418{
419 return dll_rb_num2int(x);
420}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200421# endif
422# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaar886ed692013-02-26 13:41:35 +0100423VALUE
424rb_float_new_in_heap(double d)
425{
426 return dll_rb_float_new(d);
427}
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200428# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
429unsigned long rb_num2ulong(VALUE x)
430# else
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100431VALUE rb_num2ulong(VALUE x)
Bram Moolenaarbbc1a592015-04-21 15:25:31 +0200432# endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100433{
434 return (long)RSHIFT((SIGNED_VALUE)(x),1);
435}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200436# endif
437# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100438
Bram Moolenaar3e9a1612014-11-12 16:05:04 +0100439 /* Do not generate a prototype here, VALUE isn't always defined. */
440# if defined(USE_RGENGC) && USE_RGENGC && !defined(PROTO)
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100441# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100442void rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
443{
Bram Moolenaar90140742014-11-27 17:44:08 +0100444 dll_rb_gc_writebarrier_unprotect_promoted(obj);
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100445}
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100446# else
447void rb_gc_writebarrier_unprotect_stub(VALUE obj)
448{
449 dll_rb_gc_writebarrier_unprotect(obj);
450}
451# endif
452# endif
453
454# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 22
455void rb_check_type_stub(VALUE v, int i)
456{
457 dll_rb_check_type(v, i);
458}
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100459# endif
460
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200461static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462
463/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000464 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466static struct
467{
468 char *name;
469 RUBY_PROC *ptr;
470} ruby_funcname_table[] =
471{
472 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
473 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
474 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200475# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100476 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200477# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
479 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
480 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
481 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
482 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
483 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
484 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
485 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
486 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
487 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
488 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
489 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
490 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
491 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
492 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
493 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
494 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
495 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
496 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
497 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
498 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
499 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
500 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
501 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
502 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
503 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100504# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200505 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
506 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
507 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200508# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
510 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
511 {"rb_load_protect", (RUBY_PROC*)&dll_rb_load_protect},
512 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
513 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
514 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
515 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
516 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
517 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200518# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200519 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200520# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200522# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
524 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
525 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200526# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200527 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200528# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200530# endif
531# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100532 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200533# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200535# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
537 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200538# ifdef WIN3264
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100539 {
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200540# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER < 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100541 "NtInitialize",
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200542# else
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100543 "ruby_sysinit",
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200544# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100545 (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200546# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200548# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200549# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200550# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100551 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200552# if DYNAMIC_RUBY_VER <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100553 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200554# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100555 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200556# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100557 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
558 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200559# endif
560# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100561 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100562 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
563 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
564 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
565 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
566 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100567 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100568 {"ruby_options", (RUBY_PROC*)&dll_ruby_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200569# endif
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200570# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
571# ifdef __ia64
572 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
573# endif
574 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
575# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100576# if defined(USE_RGENGC) && USE_RGENGC
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100577# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER == 21
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100578 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
Bram Moolenaar0c7485f2015-01-14 14:04:10 +0100579# else
580 {"rb_gc_writebarrier_unprotect", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect},
581# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100582# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 {"", NULL},
584};
585
586/*
587 * Free ruby.dll
588 */
589 static void
590end_dynamic_ruby()
591{
592 if (hinstRuby)
593 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200594 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200595 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 }
597}
598
599/*
600 * Load library and get all pointers.
601 * Parameter 'libname' provides name of DLL.
602 * Return OK or FAIL.
603 */
604 static int
605ruby_runtime_link_init(char *libname, int verbose)
606{
607 int i;
608
609 if (hinstRuby)
610 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200611 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 if (!hinstRuby)
613 {
614 if (verbose)
615 EMSG2(_(e_loadlib), libname);
616 return FAIL;
617 }
618
619 for (i = 0; ruby_funcname_table[i].ptr; ++i)
620 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200621 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 ruby_funcname_table[i].name)))
623 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200624 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200625 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 if (verbose)
627 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
628 return FAIL;
629 }
630 }
631 return OK;
632}
633
634/*
635 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
636 * else FALSE.
637 */
638 int
639ruby_enabled(verbose)
640 int verbose;
641{
Bram Moolenaard94464e2015-11-02 15:28:18 +0100642#ifdef WIN3264
643 char *dll = DYNAMIC_RUBY_DLL;
644#else
645 char *dll = *p_rubydll ? (char *)p_rubydll : DYNAMIC_RUBY_DLL;
646#endif
647 return ruby_runtime_link_init(dll, verbose) == OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648}
649#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
650
651 void
652ruby_end()
653{
654#ifdef DYNAMIC_RUBY
655 end_dynamic_ruby();
656#endif
657}
658
659void ex_ruby(exarg_T *eap)
660{
661 int state;
662 char *script = NULL;
663
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000664 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 if (!eap->skip && ensure_ruby_initialized())
666 {
667 if (script == NULL)
668 rb_eval_string_protect((char *)eap->arg, &state);
669 else
670 rb_eval_string_protect(script, &state);
671 if (state)
672 error_print(state);
673 }
674 vim_free(script);
675}
676
Bram Moolenaar165641d2010-02-17 16:23:09 +0100677/*
678 * In Ruby 1.9 or later, ruby String object has encoding.
679 * conversion buffer string of vim to ruby String object using
680 * VIM encoding option.
681 */
682 static VALUE
683vim_str2rb_enc_str(const char *s)
684{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100685#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100686 int isnum;
687 long lval;
688 char_u *sval;
689 rb_encoding *enc;
690
691 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
692 if (isnum == 0)
693 {
694 enc = rb_enc_find((char *)sval);
695 vim_free(sval);
696 if (enc) {
697 return rb_enc_str_new(s, strlen(s), enc);
698 }
699 }
700#endif
701 return rb_str_new2(s);
702}
703
704 static VALUE
705eval_enc_string_protect(const char *str, int *state)
706{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100707#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100708 int isnum;
709 long lval;
710 char_u *sval;
711 rb_encoding *enc;
712 VALUE v;
713
714 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
715 if (isnum == 0)
716 {
717 enc = rb_enc_find((char *)sval);
718 vim_free(sval);
719 if (enc)
720 {
721 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
722 return rb_eval_string_protect(StringValuePtr(v), state);
723 }
724 }
725#endif
726 return rb_eval_string_protect(str, state);
727}
728
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729void ex_rubydo(exarg_T *eap)
730{
731 int state;
732 linenr_T i;
733
734 if (ensure_ruby_initialized())
735 {
736 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
737 return;
738 for (i = eap->line1; i <= eap->line2; i++) {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100739 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100741 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100743 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 if (state) {
745 error_print(state);
746 break;
747 }
748 line = rb_lastline_get();
749 if (!NIL_P(line)) {
750 if (TYPE(line) != T_STRING) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000751 EMSG(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 return;
753 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100754 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 changed();
756#ifdef SYNTAX_HL
757 syn_changed(i); /* recompute syntax hl. for this line */
758#endif
759 }
760 }
761 check_cursor();
762 update_curbuf(NOT_VALID);
763 }
764}
765
766void ex_rubyfile(exarg_T *eap)
767{
768 int state;
769
770 if (ensure_ruby_initialized())
771 {
772 rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
773 if (state) error_print(state);
774 }
775}
776
777void ruby_buffer_free(buf_T *buf)
778{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000779 if (buf->b_ruby_ref)
780 {
781 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
782 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 }
784}
785
786void ruby_window_free(win_T *win)
787{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000788 if (win->w_ruby_ref)
789 {
790 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
791 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 }
793}
794
795static int ensure_ruby_initialized(void)
796{
797 if (!ruby_initialized)
798 {
799#ifdef DYNAMIC_RUBY
800 if (ruby_enabled(TRUE))
801 {
802#endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100803#ifdef _WIN32
804 /* suggested by Ariya Mizutani */
805 int argc = 1;
806 char *argv[] = {"gvim.exe"};
Bram Moolenaar90140742014-11-27 17:44:08 +0100807 char **argvp = argv;
808 NtInitialize(&argc, &argvp);
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100809#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200810 {
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200811#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +0200812 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100813#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200814 ruby_init();
815 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100816#ifdef RUBY19_OR_LATER
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100817 {
818 int dummy_argc = 2;
819 char *dummy_argv[] = {"vim-ruby", "-e0"};
Bram Moolenaar9b1067e2015-11-19 19:33:15 +0100820 ruby_options(dummy_argc, dummy_argv);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100821 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100822 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100823#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100825#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100826 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 ruby_vim_init();
828 ruby_initialized = 1;
829#ifdef DYNAMIC_RUBY
830 }
831 else
832 {
833 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
834 return 0;
835 }
836#endif
837 }
838 return ruby_initialized;
839}
840
841static void error_print(int state)
842{
843#ifndef DYNAMIC_RUBY
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100844#if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
845 && !(defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 RUBYEXTERN VALUE ruby_errinfo;
847#endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 VALUE eclass;
850 VALUE einfo;
851 char buff[BUFSIZ];
852
853#define TAG_RETURN 0x1
854#define TAG_BREAK 0x2
855#define TAG_NEXT 0x3
856#define TAG_RETRY 0x4
857#define TAG_REDO 0x5
858#define TAG_RAISE 0x6
859#define TAG_THROW 0x7
860#define TAG_FATAL 0x8
861#define TAG_MASK 0xf
862
863 switch (state) {
864 case TAG_RETURN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000865 EMSG(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 break;
867 case TAG_NEXT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000868 EMSG(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 break;
870 case TAG_BREAK:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000871 EMSG(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 break;
873 case TAG_REDO:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000874 EMSG(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 break;
876 case TAG_RETRY:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000877 EMSG(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 break;
879 case TAG_RAISE:
880 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +0100881#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100882 eclass = CLASS_OF(rb_errinfo());
883 einfo = rb_obj_as_string(rb_errinfo());
884#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 eclass = CLASS_OF(ruby_errinfo);
886 einfo = rb_obj_as_string(ruby_errinfo);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100887#endif
888 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000889 EMSG(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 }
891 else {
892 VALUE epath;
893 char *p;
894
895 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000896 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar165641d2010-02-17 16:23:09 +0100897 RSTRING_PTR(epath), RSTRING_PTR(einfo));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 p = strchr(buff, '\n');
899 if (p) *p = '\0';
900 EMSG(buff);
901 }
902 break;
903 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000904 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 EMSG(buff);
906 break;
907 }
908}
909
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000910static VALUE vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911{
912 char *buff, *p;
913
914 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +0200915 if (RSTRING_LEN(str) > 0)
916 {
917 /* Only do this when the string isn't empty, alloc(0) causes trouble. */
918 buff = ALLOCA_N(char, RSTRING_LEN(str));
919 strcpy(buff, RSTRING_PTR(str));
920 p = strchr(buff, '\n');
921 if (p) *p = '\0';
922 MSG(buff);
923 }
924 else
925 {
926 MSG("");
927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 return Qnil;
929}
930
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000931static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100933 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 update_screen(NOT_VALID);
935 return Qnil;
936}
937
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000938static VALUE vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100940 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 return Qnil;
942}
943
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100944#ifdef FEAT_EVAL
945static VALUE vim_to_ruby(typval_T *tv)
946{
947 VALUE result = Qnil;
948
949 if (tv->v_type == VAR_STRING)
950 {
Bram Moolenaar94127e42010-03-19 23:08:48 +0100951 result = rb_str_new2(tv->vval.v_string == NULL
952 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100953 }
954 else if (tv->v_type == VAR_NUMBER)
955 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100956 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100957 }
958# ifdef FEAT_FLOAT
959 else if (tv->v_type == VAR_FLOAT)
960 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100961 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100962 }
963# endif
964 else if (tv->v_type == VAR_LIST)
965 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100966 list_T *list = tv->vval.v_list;
967 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100968
Bram Moolenaar639a2552010-03-17 18:15:23 +0100969 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100970
Bram Moolenaar639a2552010-03-17 18:15:23 +0100971 if (list != NULL)
972 {
973 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
974 {
975 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
976 }
977 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100978 }
979 else if (tv->v_type == VAR_DICT)
980 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100981 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100982
Bram Moolenaar639a2552010-03-17 18:15:23 +0100983 if (tv->vval.v_dict != NULL)
984 {
985 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
986 long_u todo = ht->ht_used;
987 hashitem_T *hi;
988 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100989
Bram Moolenaar639a2552010-03-17 18:15:23 +0100990 for (hi = ht->ht_array; todo > 0; ++hi)
991 {
992 if (!HASHITEM_EMPTY(hi))
993 {
994 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100995
Bram Moolenaar639a2552010-03-17 18:15:23 +0100996 di = dict_lookup(hi);
997 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100998 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +0100999 }
1000 }
1001 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001002 } /* else return Qnil; */
1003
1004 return result;
1005}
1006#endif
1007
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001008static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009{
1010#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001011 typval_T *tv;
1012 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001014 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
1015 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 {
Bram Moolenaar639a2552010-03-17 18:15:23 +01001017 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01001019 result = vim_to_ruby(tv);
1020
1021 free_tv(tv);
1022
1023 return result;
1024#else
1025 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027}
1028
1029static VALUE buffer_new(buf_T *buf)
1030{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001031 if (buf->b_ruby_ref)
1032 {
1033 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001035 else
1036 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001038 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1040 return obj;
1041 }
1042}
1043
1044static buf_T *get_buf(VALUE obj)
1045{
1046 buf_T *buf;
1047
1048 Data_Get_Struct(obj, buf_T, buf);
1049 if (buf == NULL)
1050 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1051 return buf;
1052}
1053
1054static VALUE buffer_s_current()
1055{
1056 return buffer_new(curbuf);
1057}
1058
1059static VALUE buffer_s_count()
1060{
1061 buf_T *b;
1062 int n = 0;
1063
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001064 for (b = firstbuf; b != NULL; b = b->b_next)
1065 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001066 /* Deleted buffers should not be counted
1067 * SegPhault - 01/07/05 */
1068 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001069 n++;
1070 }
1071
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 return INT2NUM(n);
1073}
1074
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001075static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076{
1077 buf_T *b;
1078 int n = NUM2INT(num);
1079
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001080 for (b = firstbuf; b != NULL; b = b->b_next)
1081 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001082 /* Deleted buffers should not be counted
1083 * SegPhault - 01/07/05 */
1084 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001085 continue;
1086
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001087 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001089
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001090 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 }
1092 return Qnil;
1093}
1094
1095static VALUE buffer_name(VALUE self)
1096{
1097 buf_T *buf = get_buf(self);
1098
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001099 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100}
1101
1102static VALUE buffer_number(VALUE self)
1103{
1104 buf_T *buf = get_buf(self);
1105
1106 return INT2NUM(buf->b_fnum);
1107}
1108
1109static VALUE buffer_count(VALUE self)
1110{
1111 buf_T *buf = get_buf(self);
1112
1113 return INT2NUM(buf->b_ml.ml_line_count);
1114}
1115
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001116static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001118 if (n <= 0 || n > buf->b_ml.ml_line_count)
1119 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1120 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121}
1122
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001123static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124{
1125 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001126
1127 if (buf != NULL)
1128 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
1129 return Qnil; /* For stop warning */
1130}
1131
1132static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
1133{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001134 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001135 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001137 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1138 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001139 /* set curwin/curbuf for "buf" and save some things */
1140 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001141
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 if (u_savesub(n) == OK) {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001143 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 changed();
1145#ifdef SYNTAX_HL
1146 syn_changed(n); /* recompute syntax hl. for this line */
1147#endif
1148 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001149
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001150 /* restore curwin/curbuf and a few other things */
1151 aucmd_restbuf(&aco);
1152 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001153
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 update_curbuf(NOT_VALID);
1155 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001156 else
1157 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001158 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 }
1160 return str;
1161}
1162
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001163static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
1164{
1165 buf_T *buf = get_buf(self);
1166
1167 if (buf != NULL)
1168 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1169 return str;
1170}
1171
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172static VALUE buffer_delete(VALUE self, VALUE num)
1173{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001174 buf_T *buf = get_buf(self);
1175 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001176 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001178 if (n > 0 && n <= buf->b_ml.ml_line_count)
1179 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001180 /* set curwin/curbuf for "buf" and save some things */
1181 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001182
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 if (u_savedel(n, 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001185
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001186 /* Changes to non-active buffers should properly refresh
1187 * SegPhault - 01/09/05 */
1188 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001189
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 changed();
1191 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001192
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001193 /* restore curwin/curbuf and a few other things */
1194 aucmd_restbuf(&aco);
1195 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001196
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 update_curbuf(NOT_VALID);
1198 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001199 else
1200 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001201 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 }
1203 return Qnil;
1204}
1205
1206static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
1207{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001208 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001209 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001210 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001211 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212
Bram Moolenaar3c531602010-11-16 14:46:19 +01001213 if (line == NULL)
1214 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001215 rb_raise(rb_eIndexError, "NULL line");
1216 }
1217 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001218 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001219 /* set curwin/curbuf for "buf" and save some things */
1220 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001221
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 if (u_inssub(n + 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001224
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001225 /* Changes to non-active buffers should properly refresh screen
1226 * SegPhault - 12/20/04 */
1227 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001228
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001229 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001231
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001232 /* restore curwin/curbuf and a few other things */
1233 aucmd_restbuf(&aco);
1234 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001235
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 update_curbuf(NOT_VALID);
1237 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001238 else
1239 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001240 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 }
1242 return str;
1243}
1244
1245static VALUE window_new(win_T *win)
1246{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001247 if (win->w_ruby_ref)
1248 {
1249 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001251 else
1252 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001254 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1256 return obj;
1257 }
1258}
1259
1260static win_T *get_win(VALUE obj)
1261{
1262 win_T *win;
1263
1264 Data_Get_Struct(obj, win_T, win);
1265 if (win == NULL)
1266 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1267 return win;
1268}
1269
1270static VALUE window_s_current()
1271{
1272 return window_new(curwin);
1273}
1274
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001275/*
1276 * Added line manipulation functions
1277 * SegPhault - 03/07/05
1278 */
1279static VALUE line_s_current()
1280{
1281 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1282}
1283
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001284static VALUE set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001285{
1286 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1287}
1288
1289static VALUE current_line_number()
1290{
1291 return INT2FIX((int)curwin->w_cursor.lnum);
1292}
1293
1294
1295
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296static VALUE window_s_count()
1297{
1298#ifdef FEAT_WINDOWS
1299 win_T *w;
1300 int n = 0;
1301
Bram Moolenaarf740b292006-02-16 22:11:02 +00001302 for (w = firstwin; w != NULL; w = w->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 n++;
1304 return INT2NUM(n);
1305#else
1306 return INT2NUM(1);
1307#endif
1308}
1309
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001310static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311{
1312 win_T *w;
1313 int n = NUM2INT(num);
1314
1315#ifndef FEAT_WINDOWS
1316 w = curwin;
1317#else
1318 for (w = firstwin; w != NULL; w = w->w_next, --n)
1319#endif
1320 if (n == 0)
1321 return window_new(w);
1322 return Qnil;
1323}
1324
1325static VALUE window_buffer(VALUE self)
1326{
1327 win_T *win = get_win(self);
1328
1329 return buffer_new(win->w_buffer);
1330}
1331
1332static VALUE window_height(VALUE self)
1333{
1334 win_T *win = get_win(self);
1335
1336 return INT2NUM(win->w_height);
1337}
1338
1339static VALUE window_set_height(VALUE self, VALUE height)
1340{
1341 win_T *win = get_win(self);
1342 win_T *savewin = curwin;
1343
1344 curwin = win;
1345 win_setheight(NUM2INT(height));
1346 curwin = savewin;
1347 return height;
1348}
1349
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001350static VALUE window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001351{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001352 return INT2NUM(W_WIDTH(get_win(self)));
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001353}
1354
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001355static VALUE window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001356{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001357#ifdef FEAT_VERTSPLIT
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001358 win_T *win = get_win(self);
1359 win_T *savewin = curwin;
1360
1361 curwin = win;
1362 win_setwidth(NUM2INT(width));
1363 curwin = savewin;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001364#endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001365 return width;
1366}
1367
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368static VALUE window_cursor(VALUE self)
1369{
1370 win_T *win = get_win(self);
1371
1372 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1373}
1374
1375static VALUE window_set_cursor(VALUE self, VALUE pos)
1376{
1377 VALUE lnum, col;
1378 win_T *win = get_win(self);
1379
1380 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001381 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001383 lnum = RARRAY_PTR(pos)[0];
1384 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 win->w_cursor.lnum = NUM2LONG(lnum);
1386 win->w_cursor.col = NUM2UINT(col);
1387 check_cursor(); /* put cursor on an existing line */
1388 update_screen(NOT_VALID);
1389 return Qnil;
1390}
1391
Bram Moolenaar6217cdc2012-04-25 12:28:09 +02001392static VALUE f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001393{
1394 return Qnil;
1395}
1396
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001397static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398{
1399 int i;
1400 VALUE str = rb_str_new("", 0);
1401
1402 for (i = 0; i < argc; i++) {
1403 if (i > 0) rb_str_cat(str, ", ", 2);
1404 rb_str_concat(str, rb_inspect(argv[i]));
1405 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001406 MSG(RSTRING_PTR(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 return Qnil;
1408}
1409
1410static void ruby_io_init(void)
1411{
1412#ifndef DYNAMIC_RUBY
1413 RUBYEXTERN VALUE rb_stdout;
1414#endif
1415
1416 rb_stdout = rb_obj_alloc(rb_cObject);
1417 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001418 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 rb_define_global_function("p", f_p, -1);
1420}
1421
1422static void ruby_vim_init(void)
1423{
1424 objtbl = rb_hash_new();
1425 rb_global_variable(&objtbl);
1426
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001427 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001428 * alias "VIM" for backwards compatibility. */
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001429 mVIM = rb_define_module("Vim");
1430 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1432 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1433 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1434 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1435 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1436 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1437 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1438 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1439 rb_define_module_function(mVIM, "message", vim_message, 1);
1440 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1441 rb_define_module_function(mVIM, "command", vim_command, 1);
1442 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
1443
1444 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1445 rb_eStandardError);
1446 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1447 rb_eStandardError);
1448
1449 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1450 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1451 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1452 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1453 rb_define_method(cBuffer, "name", buffer_name, 0);
1454 rb_define_method(cBuffer, "number", buffer_number, 0);
1455 rb_define_method(cBuffer, "count", buffer_count, 0);
1456 rb_define_method(cBuffer, "length", buffer_count, 0);
1457 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1458 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1459 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1460 rb_define_method(cBuffer, "append", buffer_append, 2);
1461
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001462 /* Added line manipulation functions
1463 * SegPhault - 03/07/05 */
1464 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1465 rb_define_method(cBuffer, "line", line_s_current, 0);
1466 rb_define_method(cBuffer, "line=", set_current_line, 1);
1467
1468
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1470 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1471 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1472 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1473 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1474 rb_define_method(cVimWindow, "height", window_height, 0);
1475 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001476 rb_define_method(cVimWindow, "width", window_width, 0);
1477 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1479 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1480
1481 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1482 rb_define_virtual_variable("$curwin", window_s_current, 0);
1483}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001484
1485void vim_ruby_init(void *stack_start)
1486{
1487 /* should get machine stack start address early in main function */
1488 ruby_stack_start = stack_start;
1489}