blob: d8c14c65227cc42807947fc5ea43540250a0baa9 [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 Moolenaar73b044d2014-03-27 19:08:55 +010091#if defined(DYNAMIC_RUBY_VER) && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +020092/* Ruby 2.0 defines a number of static functions which use rb_fix2int and
Bram Moolenaara2aa31a2014-02-23 22:52:40 +010093 * rb_num2int if VIM_SIZEOF_INT < VIM_SIZEOF_LONG (64bit) */
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +020094# define rb_fix2int rb_fix2int_stub
95# define rb_num2int rb_num2int_stub
96#endif
97
Bram Moolenaara1a118b2014-02-05 22:41:15 +010098# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 21
99/* Ruby 2.1 adds new GC called RGenGC and RARRAY_PTR uses
100 * rb_gc_writebarrier_unprotect_promoted if USE_RGENGC */
101# define rb_gc_writebarrier_unprotect_promoted rb_gc_writebarrier_unprotect_promoted_stub
102# endif
103
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#include <ruby.h>
Bram Moolenaar639a2552010-03-17 18:15:23 +0100105#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100106# include <ruby/encoding.h>
107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100109#undef off_t /* ruby defines off_t as _int64, Mingw uses long */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110#undef EXTERN
111#undef _
112
113/* T_DATA defined both by Ruby and Mac header files, hack around it... */
Bram Moolenaarcb635362007-05-12 13:02:42 +0000114#if defined(MACOS_X_UNIX) || defined(macintosh)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115# define __OPENTRANSPORT__
116# define __OPENTRANSPORTPROTOCOL__
117# define __OPENTRANSPORTPROVIDERS__
118#endif
119
Bram Moolenaar165641d2010-02-17 16:23:09 +0100120/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200121 * Backward compatibility for Ruby 1.8 and earlier.
Bram Moolenaar165641d2010-02-17 16:23:09 +0100122 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
123 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
124 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
125 */
126#ifndef StringValuePtr
127# define StringValuePtr(s) STR2CSTR(s)
128#endif
129#ifndef RARRAY_LEN
130# define RARRAY_LEN(s) RARRAY(s)->len
131#endif
132#ifndef RARRAY_PTR
133# define RARRAY_PTR(s) RARRAY(s)->ptr
134#endif
135#ifndef RSTRING_LEN
136# define RSTRING_LEN(s) RSTRING(s)->len
137#endif
138#ifndef RSTRING_PTR
139# define RSTRING_PTR(s) RSTRING(s)->ptr
140#endif
141
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#include "vim.h"
143#include "version.h"
144
145#if defined(PROTO) && !defined(FEAT_RUBY)
146/* Define these to be able to generate the function prototypes. */
147# define VALUE int
148# define RUBY_DATA_FUNC int
149#endif
150
151static int ruby_initialized = 0;
Bram Moolenaar99685e62013-05-11 13:56:18 +0200152static void *ruby_stack_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153static VALUE objtbl;
154
155static VALUE mVIM;
156static VALUE cBuffer;
157static VALUE cVimWindow;
158static VALUE eDeletedBufferError;
159static VALUE eDeletedWindowError;
160
161static int ensure_ruby_initialized(void);
162static void error_print(int);
163static void ruby_io_init(void);
164static void ruby_vim_init(void);
165
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200166#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
167# if defined(__ia64) && !defined(ruby_init_stack)
168# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
169# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170#endif
171
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200172#if defined(DYNAMIC_RUBY) || defined(PROTO)
173# ifdef PROTO
174# define HINSTANCE int /* for generating prototypes */
175# endif
176
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177/*
178 * Wrapper defines
179 */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200180# define rb_assoc_new dll_rb_assoc_new
181# define rb_cObject (*dll_rb_cObject)
182# define rb_check_type dll_rb_check_type
183# define rb_class_path dll_rb_class_path
184# define rb_data_object_alloc dll_rb_data_object_alloc
185# define rb_define_class_under dll_rb_define_class_under
186# define rb_define_const dll_rb_define_const
187# define rb_define_global_function dll_rb_define_global_function
188# define rb_define_method dll_rb_define_method
189# define rb_define_module dll_rb_define_module
190# define rb_define_module_function dll_rb_define_module_function
191# define rb_define_singleton_method dll_rb_define_singleton_method
192# define rb_define_virtual_variable dll_rb_define_virtual_variable
193# define rb_stdout (*dll_rb_stdout)
194# define rb_eArgError (*dll_rb_eArgError)
195# define rb_eIndexError (*dll_rb_eIndexError)
196# define rb_eRuntimeError (*dll_rb_eRuntimeError)
197# define rb_eStandardError (*dll_rb_eStandardError)
198# define rb_eval_string_protect dll_rb_eval_string_protect
199# define rb_global_variable dll_rb_global_variable
200# define rb_hash_aset dll_rb_hash_aset
201# define rb_hash_new dll_rb_hash_new
202# define rb_inspect dll_rb_inspect
203# define rb_int2inum dll_rb_int2inum
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100204# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200205# define rb_num2uint dll_rb_num2uint
206# endif
207# define rb_lastline_get dll_rb_lastline_get
208# define rb_lastline_set dll_rb_lastline_set
209# define rb_load_protect dll_rb_load_protect
210# ifndef RUBY19_OR_LATER
211# define rb_num2long dll_rb_num2long
212# endif
213# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 19
214# define rb_num2ulong dll_rb_num2ulong
215# endif
216# define rb_obj_alloc dll_rb_obj_alloc
217# define rb_obj_as_string dll_rb_obj_as_string
218# define rb_obj_id dll_rb_obj_id
219# define rb_raise dll_rb_raise
220# define rb_str_cat dll_rb_str_cat
221# define rb_str_concat dll_rb_str_concat
222# define rb_str_new dll_rb_str_new
223# ifdef rb_str_new2
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200224/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200225# define need_rb_str_new_cstr 1
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200226/* Ruby's headers #define rb_str_new_cstr to make use of GCC's
227 * __builtin_constant_p extension. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200228# undef rb_str_new_cstr
229# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200230# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200231# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200232# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200233# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200234# define rb_string_value dll_rb_string_value
235# define rb_string_value_ptr dll_rb_string_value_ptr
236# define rb_float_new dll_rb_float_new
237# define rb_ary_new dll_rb_ary_new
238# define rb_ary_push dll_rb_ary_push
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200239# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
240# ifdef __ia64
241# define rb_ia64_bsp dll_rb_ia64_bsp
242# undef ruby_init_stack
243# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
244# else
245# define ruby_init_stack dll_ruby_init_stack
246# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200247# endif
248# else
249# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200250# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200251# ifdef RUBY19_OR_LATER
252# define rb_errinfo dll_rb_errinfo
253# else
254# define ruby_errinfo (*dll_ruby_errinfo)
255# endif
256# define ruby_init dll_ruby_init
257# define ruby_init_loadpath dll_ruby_init_loadpath
258# ifdef WIN3264
259# define NtInitialize dll_NtInitialize
260# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
261# define rb_w32_snprintf dll_rb_w32_snprintf
262# endif
263# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200265# ifdef RUBY19_OR_LATER
266# define ruby_script dll_ruby_script
267# define rb_enc_find_index dll_rb_enc_find_index
268# define rb_enc_find dll_rb_enc_find
269# define rb_enc_str_new dll_rb_enc_str_new
270# define rb_sprintf dll_rb_sprintf
271# define rb_require dll_rb_require
272# define ruby_process_options dll_ruby_process_options
273# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100274
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275/*
276 * Pointers for dynamic link
277 */
278static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200279VALUE *dll_rb_cFalseClass;
280VALUE *dll_rb_cFixnum;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200281# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100282VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200283# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200284VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285static VALUE *dll_rb_cObject;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200286VALUE *dll_rb_cSymbol;
287VALUE *dll_rb_cTrueClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288static void (*dll_rb_check_type) (VALUE,int);
289static VALUE (*dll_rb_class_path) (VALUE);
290static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
291static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
292static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
293static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
294static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
295static VALUE (*dll_rb_define_module) (const char*);
296static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
297static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
298static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
299static VALUE *dll_rb_stdout;
300static VALUE *dll_rb_eArgError;
301static VALUE *dll_rb_eIndexError;
302static VALUE *dll_rb_eRuntimeError;
303static VALUE *dll_rb_eStandardError;
304static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
305static void (*dll_rb_global_variable) (VALUE*);
306static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
307static VALUE (*dll_rb_hash_new) (void);
308static VALUE (*dll_rb_inspect) (VALUE);
309static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100310# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200311static long (*dll_rb_fix2int) (VALUE);
312static long (*dll_rb_num2int) (VALUE);
313static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200314# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315static VALUE (*dll_rb_lastline_get) (void);
316static void (*dll_rb_lastline_set) (VALUE);
317static void (*dll_rb_load_protect) (VALUE, int, int*);
318static long (*dll_rb_num2long) (VALUE);
319static unsigned long (*dll_rb_num2ulong) (VALUE);
320static VALUE (*dll_rb_obj_alloc) (VALUE);
321static VALUE (*dll_rb_obj_as_string) (VALUE);
322static VALUE (*dll_rb_obj_id) (VALUE);
323static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200324# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200325static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200326# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200328# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
330static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
331static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200332# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200333/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
334static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200335# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200337# endif
338# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100339static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200340# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200342# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343static void (*dll_ruby_init) (void);
344static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200345# ifdef WIN3264
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100346static void (*dll_NtInitialize) (int*, char***);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200347# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200348static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200349# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200350# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200351# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100352static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
353static VALUE (*dll_rb_float_new) (double);
354static VALUE (*dll_rb_ary_new) (void);
355static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200356# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
357# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200358static void * (*dll_rb_ia64_bsp) (void);
359static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200360# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200361static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200362# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200363# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200364# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200365# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100366static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200367# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200369# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100370static void (*dll_ruby_script) (const char*);
371static int (*dll_rb_enc_find_index) (const char*);
372static rb_encoding* (*dll_rb_enc_find) (const char*);
373static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
374static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100375static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100376static void* (*ruby_process_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200377# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100378
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100379# if defined(USE_RGENGC) && USE_RGENGC
380static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
381# endif
382
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200383# if defined(RUBY19_OR_LATER) && !defined(PROTO)
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100384SIGNED_VALUE rb_num2long_stub(VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100385{
386 return dll_rb_num2long(x);
387}
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100388VALUE rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100389{
390 return dll_rb_int2big(x);
391}
Bram Moolenaar73b044d2014-03-27 19:08:55 +0100392# if defined(DYNAMIC_RUBY_VER) && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200393long rb_fix2int_stub(VALUE x)
394{
395 return dll_rb_fix2int(x);
396}
397long rb_num2int_stub(VALUE x)
398{
399 return dll_rb_num2int(x);
400}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200401# endif
402# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaar886ed692013-02-26 13:41:35 +0100403VALUE
404rb_float_new_in_heap(double d)
405{
406 return dll_rb_float_new(d);
407}
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100408VALUE rb_num2ulong(VALUE x)
Bram Moolenaar886ed692013-02-26 13:41:35 +0100409{
410 return (long)RSHIFT((SIGNED_VALUE)(x),1);
411}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200412# endif
413# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100414
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100415# if defined(USE_RGENGC) && USE_RGENGC
416void rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
417{
418 return dll_rb_gc_writebarrier_unprotect_promoted(obj);
419}
420# endif
421
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200422static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423
424/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000425 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427static struct
428{
429 char *name;
430 RUBY_PROC *ptr;
431} ruby_funcname_table[] =
432{
433 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
434 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
435 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200436# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100437 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200438# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
440 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
441 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
442 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
443 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
444 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
445 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
446 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
447 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
448 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
449 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
450 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
451 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
452 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
453 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
454 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
455 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
456 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
457 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
458 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
459 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
460 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
461 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
462 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
463 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
464 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100465# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200466 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
467 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
468 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200469# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
471 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
472 {"rb_load_protect", (RUBY_PROC*)&dll_rb_load_protect},
473 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
474 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
475 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
476 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
477 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
478 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200479# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200480 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200481# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200483# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
485 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
486 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200487# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200488 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200489# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200491# endif
492# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100493 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200494# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200496# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
498 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200499# ifdef WIN3264
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100500 {
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200501# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER < 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100502 "NtInitialize",
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200503# else
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100504 "ruby_sysinit",
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200505# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100506 (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200507# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200509# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200510# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200511# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100512 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200513# if DYNAMIC_RUBY_VER <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100514 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200515# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100516 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200517# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100518 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
519 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200520# endif
521# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100522 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100523 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
524 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
525 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
526 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
527 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100528 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100529 {"ruby_process_options", (RUBY_PROC*)&dll_ruby_process_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200530# endif
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200531# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
532# ifdef __ia64
533 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
534# endif
535 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
536# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100537# if defined(USE_RGENGC) && USE_RGENGC
538 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
539# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 {"", NULL},
541};
542
543/*
544 * Free ruby.dll
545 */
546 static void
547end_dynamic_ruby()
548{
549 if (hinstRuby)
550 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200551 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200552 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 }
554}
555
556/*
557 * Load library and get all pointers.
558 * Parameter 'libname' provides name of DLL.
559 * Return OK or FAIL.
560 */
561 static int
562ruby_runtime_link_init(char *libname, int verbose)
563{
564 int i;
565
566 if (hinstRuby)
567 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200568 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 if (!hinstRuby)
570 {
571 if (verbose)
572 EMSG2(_(e_loadlib), libname);
573 return FAIL;
574 }
575
576 for (i = 0; ruby_funcname_table[i].ptr; ++i)
577 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200578 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 ruby_funcname_table[i].name)))
580 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200581 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200582 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 if (verbose)
584 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
585 return FAIL;
586 }
587 }
588 return OK;
589}
590
591/*
592 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
593 * else FALSE.
594 */
595 int
596ruby_enabled(verbose)
597 int verbose;
598{
599 return ruby_runtime_link_init(DYNAMIC_RUBY_DLL, verbose) == OK;
600}
601#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
602
603 void
604ruby_end()
605{
606#ifdef DYNAMIC_RUBY
607 end_dynamic_ruby();
608#endif
609}
610
611void ex_ruby(exarg_T *eap)
612{
613 int state;
614 char *script = NULL;
615
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000616 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 if (!eap->skip && ensure_ruby_initialized())
618 {
619 if (script == NULL)
620 rb_eval_string_protect((char *)eap->arg, &state);
621 else
622 rb_eval_string_protect(script, &state);
623 if (state)
624 error_print(state);
625 }
626 vim_free(script);
627}
628
Bram Moolenaar165641d2010-02-17 16:23:09 +0100629/*
630 * In Ruby 1.9 or later, ruby String object has encoding.
631 * conversion buffer string of vim to ruby String object using
632 * VIM encoding option.
633 */
634 static VALUE
635vim_str2rb_enc_str(const char *s)
636{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100637#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100638 int isnum;
639 long lval;
640 char_u *sval;
641 rb_encoding *enc;
642
643 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
644 if (isnum == 0)
645 {
646 enc = rb_enc_find((char *)sval);
647 vim_free(sval);
648 if (enc) {
649 return rb_enc_str_new(s, strlen(s), enc);
650 }
651 }
652#endif
653 return rb_str_new2(s);
654}
655
656 static VALUE
657eval_enc_string_protect(const char *str, int *state)
658{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100659#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100660 int isnum;
661 long lval;
662 char_u *sval;
663 rb_encoding *enc;
664 VALUE v;
665
666 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
667 if (isnum == 0)
668 {
669 enc = rb_enc_find((char *)sval);
670 vim_free(sval);
671 if (enc)
672 {
673 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
674 return rb_eval_string_protect(StringValuePtr(v), state);
675 }
676 }
677#endif
678 return rb_eval_string_protect(str, state);
679}
680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681void ex_rubydo(exarg_T *eap)
682{
683 int state;
684 linenr_T i;
685
686 if (ensure_ruby_initialized())
687 {
688 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
689 return;
690 for (i = eap->line1; i <= eap->line2; i++) {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100691 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100693 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100695 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 if (state) {
697 error_print(state);
698 break;
699 }
700 line = rb_lastline_get();
701 if (!NIL_P(line)) {
702 if (TYPE(line) != T_STRING) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000703 EMSG(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 return;
705 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100706 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 changed();
708#ifdef SYNTAX_HL
709 syn_changed(i); /* recompute syntax hl. for this line */
710#endif
711 }
712 }
713 check_cursor();
714 update_curbuf(NOT_VALID);
715 }
716}
717
718void ex_rubyfile(exarg_T *eap)
719{
720 int state;
721
722 if (ensure_ruby_initialized())
723 {
724 rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
725 if (state) error_print(state);
726 }
727}
728
729void ruby_buffer_free(buf_T *buf)
730{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000731 if (buf->b_ruby_ref)
732 {
733 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
734 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 }
736}
737
738void ruby_window_free(win_T *win)
739{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000740 if (win->w_ruby_ref)
741 {
742 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
743 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 }
745}
746
747static int ensure_ruby_initialized(void)
748{
749 if (!ruby_initialized)
750 {
751#ifdef DYNAMIC_RUBY
752 if (ruby_enabled(TRUE))
753 {
754#endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100755#ifdef _WIN32
756 /* suggested by Ariya Mizutani */
757 int argc = 1;
758 char *argv[] = {"gvim.exe"};
759 NtInitialize(&argc, &argv);
760#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200761 {
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200762#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +0200763 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100764#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200765 ruby_init();
766 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100767#ifdef RUBY19_OR_LATER
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100768 {
769 int dummy_argc = 2;
770 char *dummy_argv[] = {"vim-ruby", "-e0"};
771 ruby_process_options(dummy_argc, dummy_argv);
772 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100773 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100774#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100776#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100777 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 ruby_vim_init();
779 ruby_initialized = 1;
780#ifdef DYNAMIC_RUBY
781 }
782 else
783 {
784 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
785 return 0;
786 }
787#endif
788 }
789 return ruby_initialized;
790}
791
792static void error_print(int state)
793{
794#ifndef DYNAMIC_RUBY
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100795#if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
796 && !(defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 RUBYEXTERN VALUE ruby_errinfo;
798#endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 VALUE eclass;
801 VALUE einfo;
802 char buff[BUFSIZ];
803
804#define TAG_RETURN 0x1
805#define TAG_BREAK 0x2
806#define TAG_NEXT 0x3
807#define TAG_RETRY 0x4
808#define TAG_REDO 0x5
809#define TAG_RAISE 0x6
810#define TAG_THROW 0x7
811#define TAG_FATAL 0x8
812#define TAG_MASK 0xf
813
814 switch (state) {
815 case TAG_RETURN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000816 EMSG(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 break;
818 case TAG_NEXT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000819 EMSG(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 break;
821 case TAG_BREAK:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000822 EMSG(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 break;
824 case TAG_REDO:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000825 EMSG(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 break;
827 case TAG_RETRY:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000828 EMSG(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 break;
830 case TAG_RAISE:
831 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +0100832#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100833 eclass = CLASS_OF(rb_errinfo());
834 einfo = rb_obj_as_string(rb_errinfo());
835#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 eclass = CLASS_OF(ruby_errinfo);
837 einfo = rb_obj_as_string(ruby_errinfo);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100838#endif
839 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000840 EMSG(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 }
842 else {
843 VALUE epath;
844 char *p;
845
846 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000847 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar165641d2010-02-17 16:23:09 +0100848 RSTRING_PTR(epath), RSTRING_PTR(einfo));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 p = strchr(buff, '\n');
850 if (p) *p = '\0';
851 EMSG(buff);
852 }
853 break;
854 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000855 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 EMSG(buff);
857 break;
858 }
859}
860
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000861static VALUE vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862{
863 char *buff, *p;
864
865 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +0200866 if (RSTRING_LEN(str) > 0)
867 {
868 /* Only do this when the string isn't empty, alloc(0) causes trouble. */
869 buff = ALLOCA_N(char, RSTRING_LEN(str));
870 strcpy(buff, RSTRING_PTR(str));
871 p = strchr(buff, '\n');
872 if (p) *p = '\0';
873 MSG(buff);
874 }
875 else
876 {
877 MSG("");
878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 return Qnil;
880}
881
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000882static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100884 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 update_screen(NOT_VALID);
886 return Qnil;
887}
888
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000889static VALUE vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100891 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 return Qnil;
893}
894
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100895#ifdef FEAT_EVAL
896static VALUE vim_to_ruby(typval_T *tv)
897{
898 VALUE result = Qnil;
899
900 if (tv->v_type == VAR_STRING)
901 {
Bram Moolenaar94127e42010-03-19 23:08:48 +0100902 result = rb_str_new2(tv->vval.v_string == NULL
903 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100904 }
905 else if (tv->v_type == VAR_NUMBER)
906 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100907 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100908 }
909# ifdef FEAT_FLOAT
910 else if (tv->v_type == VAR_FLOAT)
911 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100912 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100913 }
914# endif
915 else if (tv->v_type == VAR_LIST)
916 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100917 list_T *list = tv->vval.v_list;
918 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100919
Bram Moolenaar639a2552010-03-17 18:15:23 +0100920 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100921
Bram Moolenaar639a2552010-03-17 18:15:23 +0100922 if (list != NULL)
923 {
924 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
925 {
926 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
927 }
928 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100929 }
930 else if (tv->v_type == VAR_DICT)
931 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100932 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100933
Bram Moolenaar639a2552010-03-17 18:15:23 +0100934 if (tv->vval.v_dict != NULL)
935 {
936 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
937 long_u todo = ht->ht_used;
938 hashitem_T *hi;
939 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100940
Bram Moolenaar639a2552010-03-17 18:15:23 +0100941 for (hi = ht->ht_array; todo > 0; ++hi)
942 {
943 if (!HASHITEM_EMPTY(hi))
944 {
945 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100946
Bram Moolenaar639a2552010-03-17 18:15:23 +0100947 di = dict_lookup(hi);
948 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100949 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +0100950 }
951 }
952 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100953 } /* else return Qnil; */
954
955 return result;
956}
957#endif
958
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000959static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960{
961#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100962 typval_T *tv;
963 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100965 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
966 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100968 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100970 result = vim_to_ruby(tv);
971
972 free_tv(tv);
973
974 return result;
975#else
976 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978}
979
980static VALUE buffer_new(buf_T *buf)
981{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000982 if (buf->b_ruby_ref)
983 {
984 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 }
Bram Moolenaare344bea2005-09-01 20:46:49 +0000986 else
987 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000989 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
991 return obj;
992 }
993}
994
995static buf_T *get_buf(VALUE obj)
996{
997 buf_T *buf;
998
999 Data_Get_Struct(obj, buf_T, buf);
1000 if (buf == NULL)
1001 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1002 return buf;
1003}
1004
1005static VALUE buffer_s_current()
1006{
1007 return buffer_new(curbuf);
1008}
1009
1010static VALUE buffer_s_count()
1011{
1012 buf_T *b;
1013 int n = 0;
1014
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001015 for (b = firstbuf; b != NULL; b = b->b_next)
1016 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001017 /* Deleted buffers should not be counted
1018 * SegPhault - 01/07/05 */
1019 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001020 n++;
1021 }
1022
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 return INT2NUM(n);
1024}
1025
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001026static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027{
1028 buf_T *b;
1029 int n = NUM2INT(num);
1030
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001031 for (b = firstbuf; b != NULL; b = b->b_next)
1032 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001033 /* Deleted buffers should not be counted
1034 * SegPhault - 01/07/05 */
1035 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001036 continue;
1037
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001038 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001040
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001041 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 }
1043 return Qnil;
1044}
1045
1046static VALUE buffer_name(VALUE self)
1047{
1048 buf_T *buf = get_buf(self);
1049
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001050 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051}
1052
1053static VALUE buffer_number(VALUE self)
1054{
1055 buf_T *buf = get_buf(self);
1056
1057 return INT2NUM(buf->b_fnum);
1058}
1059
1060static VALUE buffer_count(VALUE self)
1061{
1062 buf_T *buf = get_buf(self);
1063
1064 return INT2NUM(buf->b_ml.ml_line_count);
1065}
1066
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001067static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001069 if (n <= 0 || n > buf->b_ml.ml_line_count)
1070 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1071 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072}
1073
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001074static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075{
1076 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001077
1078 if (buf != NULL)
1079 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
1080 return Qnil; /* For stop warning */
1081}
1082
1083static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
1084{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001085 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001086 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001088 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1089 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001090 /* set curwin/curbuf for "buf" and save some things */
1091 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001092
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 if (u_savesub(n) == OK) {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001094 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 changed();
1096#ifdef SYNTAX_HL
1097 syn_changed(n); /* recompute syntax hl. for this line */
1098#endif
1099 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001100
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001101 /* restore curwin/curbuf and a few other things */
1102 aucmd_restbuf(&aco);
1103 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001104
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 update_curbuf(NOT_VALID);
1106 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001107 else
1108 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001109 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 }
1111 return str;
1112}
1113
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001114static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
1115{
1116 buf_T *buf = get_buf(self);
1117
1118 if (buf != NULL)
1119 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1120 return str;
1121}
1122
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123static VALUE buffer_delete(VALUE self, VALUE num)
1124{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001125 buf_T *buf = get_buf(self);
1126 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001127 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001129 if (n > 0 && n <= buf->b_ml.ml_line_count)
1130 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001131 /* set curwin/curbuf for "buf" and save some things */
1132 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001133
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 if (u_savedel(n, 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001136
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001137 /* Changes to non-active buffers should properly refresh
1138 * SegPhault - 01/09/05 */
1139 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001140
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 changed();
1142 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001143
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001144 /* restore curwin/curbuf and a few other things */
1145 aucmd_restbuf(&aco);
1146 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001147
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 update_curbuf(NOT_VALID);
1149 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001150 else
1151 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001152 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 }
1154 return Qnil;
1155}
1156
1157static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
1158{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001159 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001160 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001161 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001162 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163
Bram Moolenaar3c531602010-11-16 14:46:19 +01001164 if (line == NULL)
1165 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001166 rb_raise(rb_eIndexError, "NULL line");
1167 }
1168 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001169 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001170 /* set curwin/curbuf for "buf" and save some things */
1171 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001172
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 if (u_inssub(n + 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001175
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001176 /* Changes to non-active buffers should properly refresh screen
1177 * SegPhault - 12/20/04 */
1178 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001179
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001180 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001182
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001183 /* restore curwin/curbuf and a few other things */
1184 aucmd_restbuf(&aco);
1185 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001186
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 update_curbuf(NOT_VALID);
1188 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001189 else
1190 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001191 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 }
1193 return str;
1194}
1195
1196static VALUE window_new(win_T *win)
1197{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001198 if (win->w_ruby_ref)
1199 {
1200 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001202 else
1203 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001205 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1207 return obj;
1208 }
1209}
1210
1211static win_T *get_win(VALUE obj)
1212{
1213 win_T *win;
1214
1215 Data_Get_Struct(obj, win_T, win);
1216 if (win == NULL)
1217 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1218 return win;
1219}
1220
1221static VALUE window_s_current()
1222{
1223 return window_new(curwin);
1224}
1225
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001226/*
1227 * Added line manipulation functions
1228 * SegPhault - 03/07/05
1229 */
1230static VALUE line_s_current()
1231{
1232 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1233}
1234
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001235static VALUE set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001236{
1237 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1238}
1239
1240static VALUE current_line_number()
1241{
1242 return INT2FIX((int)curwin->w_cursor.lnum);
1243}
1244
1245
1246
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247static VALUE window_s_count()
1248{
1249#ifdef FEAT_WINDOWS
1250 win_T *w;
1251 int n = 0;
1252
Bram Moolenaarf740b292006-02-16 22:11:02 +00001253 for (w = firstwin; w != NULL; w = w->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 n++;
1255 return INT2NUM(n);
1256#else
1257 return INT2NUM(1);
1258#endif
1259}
1260
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001261static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262{
1263 win_T *w;
1264 int n = NUM2INT(num);
1265
1266#ifndef FEAT_WINDOWS
1267 w = curwin;
1268#else
1269 for (w = firstwin; w != NULL; w = w->w_next, --n)
1270#endif
1271 if (n == 0)
1272 return window_new(w);
1273 return Qnil;
1274}
1275
1276static VALUE window_buffer(VALUE self)
1277{
1278 win_T *win = get_win(self);
1279
1280 return buffer_new(win->w_buffer);
1281}
1282
1283static VALUE window_height(VALUE self)
1284{
1285 win_T *win = get_win(self);
1286
1287 return INT2NUM(win->w_height);
1288}
1289
1290static VALUE window_set_height(VALUE self, VALUE height)
1291{
1292 win_T *win = get_win(self);
1293 win_T *savewin = curwin;
1294
1295 curwin = win;
1296 win_setheight(NUM2INT(height));
1297 curwin = savewin;
1298 return height;
1299}
1300
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001301static VALUE window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001302{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001303 return INT2NUM(W_WIDTH(get_win(self)));
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001304}
1305
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001306static VALUE window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001307{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001308#ifdef FEAT_VERTSPLIT
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001309 win_T *win = get_win(self);
1310 win_T *savewin = curwin;
1311
1312 curwin = win;
1313 win_setwidth(NUM2INT(width));
1314 curwin = savewin;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001315#endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001316 return width;
1317}
1318
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319static VALUE window_cursor(VALUE self)
1320{
1321 win_T *win = get_win(self);
1322
1323 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1324}
1325
1326static VALUE window_set_cursor(VALUE self, VALUE pos)
1327{
1328 VALUE lnum, col;
1329 win_T *win = get_win(self);
1330
1331 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001332 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001334 lnum = RARRAY_PTR(pos)[0];
1335 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 win->w_cursor.lnum = NUM2LONG(lnum);
1337 win->w_cursor.col = NUM2UINT(col);
1338 check_cursor(); /* put cursor on an existing line */
1339 update_screen(NOT_VALID);
1340 return Qnil;
1341}
1342
Bram Moolenaar6217cdc2012-04-25 12:28:09 +02001343static VALUE f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001344{
1345 return Qnil;
1346}
1347
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001348static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349{
1350 int i;
1351 VALUE str = rb_str_new("", 0);
1352
1353 for (i = 0; i < argc; i++) {
1354 if (i > 0) rb_str_cat(str, ", ", 2);
1355 rb_str_concat(str, rb_inspect(argv[i]));
1356 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001357 MSG(RSTRING_PTR(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 return Qnil;
1359}
1360
1361static void ruby_io_init(void)
1362{
1363#ifndef DYNAMIC_RUBY
1364 RUBYEXTERN VALUE rb_stdout;
1365#endif
1366
1367 rb_stdout = rb_obj_alloc(rb_cObject);
1368 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001369 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 rb_define_global_function("p", f_p, -1);
1371}
1372
1373static void ruby_vim_init(void)
1374{
1375 objtbl = rb_hash_new();
1376 rb_global_variable(&objtbl);
1377
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001378 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001379 * alias "VIM" for backwards compatibility. */
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001380 mVIM = rb_define_module("Vim");
1381 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1383 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1384 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1385 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1386 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1387 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1388 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1389 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1390 rb_define_module_function(mVIM, "message", vim_message, 1);
1391 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1392 rb_define_module_function(mVIM, "command", vim_command, 1);
1393 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
1394
1395 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1396 rb_eStandardError);
1397 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1398 rb_eStandardError);
1399
1400 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1401 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1402 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1403 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1404 rb_define_method(cBuffer, "name", buffer_name, 0);
1405 rb_define_method(cBuffer, "number", buffer_number, 0);
1406 rb_define_method(cBuffer, "count", buffer_count, 0);
1407 rb_define_method(cBuffer, "length", buffer_count, 0);
1408 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1409 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1410 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1411 rb_define_method(cBuffer, "append", buffer_append, 2);
1412
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001413 /* Added line manipulation functions
1414 * SegPhault - 03/07/05 */
1415 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1416 rb_define_method(cBuffer, "line", line_s_current, 0);
1417 rb_define_method(cBuffer, "line=", set_current_line, 1);
1418
1419
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1421 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1422 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1423 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1424 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1425 rb_define_method(cVimWindow, "height", window_height, 0);
1426 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001427 rb_define_method(cVimWindow, "width", window_width, 0);
1428 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1430 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1431
1432 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1433 rb_define_virtual_variable("$curwin", window_s_current, 0);
1434}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001435
1436void vim_ruby_init(void *stack_start)
1437{
1438 /* should get machine stack start address early in main function */
1439 ruby_stack_start = stack_start;
1440}