blob: 9301b72927140d73706f91bf97574f2c599b3ab3 [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 Moolenaara1a118b2014-02-05 22:41:15 +010099# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 21
100/* Ruby 2.1 adds new GC called RGenGC and RARRAY_PTR uses
101 * rb_gc_writebarrier_unprotect_promoted if USE_RGENGC */
102# define rb_gc_writebarrier_unprotect_promoted rb_gc_writebarrier_unprotect_promoted_stub
103# endif
104
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105#include <ruby.h>
Bram Moolenaar639a2552010-03-17 18:15:23 +0100106#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100107# include <ruby/encoding.h>
108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100110#undef off_t /* ruby defines off_t as _int64, Mingw uses long */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111#undef EXTERN
112#undef _
113
114/* T_DATA defined both by Ruby and Mac header files, hack around it... */
Bram Moolenaarcb635362007-05-12 13:02:42 +0000115#if defined(MACOS_X_UNIX) || defined(macintosh)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116# define __OPENTRANSPORT__
117# define __OPENTRANSPORTPROTOCOL__
118# define __OPENTRANSPORTPROVIDERS__
119#endif
120
Bram Moolenaar165641d2010-02-17 16:23:09 +0100121/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200122 * Backward compatibility for Ruby 1.8 and earlier.
Bram Moolenaar165641d2010-02-17 16:23:09 +0100123 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
124 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
125 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
126 */
127#ifndef StringValuePtr
128# define StringValuePtr(s) STR2CSTR(s)
129#endif
130#ifndef RARRAY_LEN
131# define RARRAY_LEN(s) RARRAY(s)->len
132#endif
133#ifndef RARRAY_PTR
134# define RARRAY_PTR(s) RARRAY(s)->ptr
135#endif
136#ifndef RSTRING_LEN
137# define RSTRING_LEN(s) RSTRING(s)->len
138#endif
139#ifndef RSTRING_PTR
140# define RSTRING_PTR(s) RSTRING(s)->ptr
141#endif
142
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143#include "vim.h"
144#include "version.h"
145
146#if defined(PROTO) && !defined(FEAT_RUBY)
147/* Define these to be able to generate the function prototypes. */
148# define VALUE int
149# define RUBY_DATA_FUNC int
150#endif
151
152static int ruby_initialized = 0;
Bram Moolenaar99685e62013-05-11 13:56:18 +0200153static void *ruby_stack_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154static VALUE objtbl;
155
156static VALUE mVIM;
157static VALUE cBuffer;
158static VALUE cVimWindow;
159static VALUE eDeletedBufferError;
160static VALUE eDeletedWindowError;
161
162static int ensure_ruby_initialized(void);
163static void error_print(int);
164static void ruby_io_init(void);
165static void ruby_vim_init(void);
166
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200167#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
168# if defined(__ia64) && !defined(ruby_init_stack)
169# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
170# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171#endif
172
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200173#if defined(DYNAMIC_RUBY) || defined(PROTO)
174# ifdef PROTO
175# define HINSTANCE int /* for generating prototypes */
176# endif
177
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178/*
179 * Wrapper defines
180 */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200181# define rb_assoc_new dll_rb_assoc_new
182# define rb_cObject (*dll_rb_cObject)
183# define rb_check_type dll_rb_check_type
184# define rb_class_path dll_rb_class_path
185# define rb_data_object_alloc dll_rb_data_object_alloc
186# define rb_define_class_under dll_rb_define_class_under
187# define rb_define_const dll_rb_define_const
188# define rb_define_global_function dll_rb_define_global_function
189# define rb_define_method dll_rb_define_method
190# define rb_define_module dll_rb_define_module
191# define rb_define_module_function dll_rb_define_module_function
192# define rb_define_singleton_method dll_rb_define_singleton_method
193# define rb_define_virtual_variable dll_rb_define_virtual_variable
194# define rb_stdout (*dll_rb_stdout)
195# define rb_eArgError (*dll_rb_eArgError)
196# define rb_eIndexError (*dll_rb_eIndexError)
197# define rb_eRuntimeError (*dll_rb_eRuntimeError)
198# define rb_eStandardError (*dll_rb_eStandardError)
199# define rb_eval_string_protect dll_rb_eval_string_protect
200# define rb_global_variable dll_rb_global_variable
201# define rb_hash_aset dll_rb_hash_aset
202# define rb_hash_new dll_rb_hash_new
203# define rb_inspect dll_rb_inspect
204# define rb_int2inum dll_rb_int2inum
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100205# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar498af702014-03-28 21:58:21 +0100206# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 18
207# define rb_fix2int dll_rb_fix2int
208# define rb_num2int dll_rb_num2int
209# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200210# define rb_num2uint dll_rb_num2uint
211# endif
212# define rb_lastline_get dll_rb_lastline_get
213# define rb_lastline_set dll_rb_lastline_set
214# define rb_load_protect dll_rb_load_protect
215# ifndef RUBY19_OR_LATER
216# define rb_num2long dll_rb_num2long
217# endif
218# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 19
219# define rb_num2ulong dll_rb_num2ulong
220# endif
221# define rb_obj_alloc dll_rb_obj_alloc
222# define rb_obj_as_string dll_rb_obj_as_string
223# define rb_obj_id dll_rb_obj_id
224# define rb_raise dll_rb_raise
225# define rb_str_cat dll_rb_str_cat
226# define rb_str_concat dll_rb_str_concat
227# define rb_str_new dll_rb_str_new
228# ifdef rb_str_new2
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200229/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200230# define need_rb_str_new_cstr 1
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200231/* Ruby's headers #define rb_str_new_cstr to make use of GCC's
232 * __builtin_constant_p extension. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200233# undef rb_str_new_cstr
234# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200235# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200236# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200237# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200238# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200239# define rb_string_value dll_rb_string_value
240# define rb_string_value_ptr dll_rb_string_value_ptr
241# define rb_float_new dll_rb_float_new
242# define rb_ary_new dll_rb_ary_new
243# define rb_ary_push dll_rb_ary_push
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200244# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
245# ifdef __ia64
246# define rb_ia64_bsp dll_rb_ia64_bsp
247# undef ruby_init_stack
248# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
249# else
250# define ruby_init_stack dll_ruby_init_stack
251# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200252# endif
253# else
254# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200255# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200256# ifdef RUBY19_OR_LATER
257# define rb_errinfo dll_rb_errinfo
258# else
259# define ruby_errinfo (*dll_ruby_errinfo)
260# endif
261# define ruby_init dll_ruby_init
262# define ruby_init_loadpath dll_ruby_init_loadpath
263# ifdef WIN3264
264# define NtInitialize dll_NtInitialize
265# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
266# define rb_w32_snprintf dll_rb_w32_snprintf
267# endif
268# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200270# ifdef RUBY19_OR_LATER
271# define ruby_script dll_ruby_script
272# define rb_enc_find_index dll_rb_enc_find_index
273# define rb_enc_find dll_rb_enc_find
274# define rb_enc_str_new dll_rb_enc_str_new
275# define rb_sprintf dll_rb_sprintf
276# define rb_require dll_rb_require
277# define ruby_process_options dll_ruby_process_options
278# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100279
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280/*
281 * Pointers for dynamic link
282 */
283static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200284VALUE *dll_rb_cFalseClass;
285VALUE *dll_rb_cFixnum;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200286# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100287VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200288# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200289VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290static VALUE *dll_rb_cObject;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200291VALUE *dll_rb_cSymbol;
292VALUE *dll_rb_cTrueClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293static void (*dll_rb_check_type) (VALUE,int);
294static VALUE (*dll_rb_class_path) (VALUE);
295static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
296static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
297static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
298static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
299static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
300static VALUE (*dll_rb_define_module) (const char*);
301static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
302static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
303static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
304static VALUE *dll_rb_stdout;
305static VALUE *dll_rb_eArgError;
306static VALUE *dll_rb_eIndexError;
307static VALUE *dll_rb_eRuntimeError;
308static VALUE *dll_rb_eStandardError;
309static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
310static void (*dll_rb_global_variable) (VALUE*);
311static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
312static VALUE (*dll_rb_hash_new) (void);
313static VALUE (*dll_rb_inspect) (VALUE);
314static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100315# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200316static long (*dll_rb_fix2int) (VALUE);
317static long (*dll_rb_num2int) (VALUE);
318static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200319# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320static VALUE (*dll_rb_lastline_get) (void);
321static void (*dll_rb_lastline_set) (VALUE);
322static void (*dll_rb_load_protect) (VALUE, int, int*);
323static long (*dll_rb_num2long) (VALUE);
324static unsigned long (*dll_rb_num2ulong) (VALUE);
325static VALUE (*dll_rb_obj_alloc) (VALUE);
326static VALUE (*dll_rb_obj_as_string) (VALUE);
327static VALUE (*dll_rb_obj_id) (VALUE);
328static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200329# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200330static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200331# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200333# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
335static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
336static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200337# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200338/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
339static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200340# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200342# endif
343# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100344static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200345# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200347# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348static void (*dll_ruby_init) (void);
349static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200350# ifdef WIN3264
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100351static void (*dll_NtInitialize) (int*, char***);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200352# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200353static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200354# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200355# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200356# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100357static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
358static VALUE (*dll_rb_float_new) (double);
359static VALUE (*dll_rb_ary_new) (void);
360static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200361# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
362# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200363static void * (*dll_rb_ia64_bsp) (void);
364static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200365# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200366static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200367# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200368# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200369# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200370# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100371static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200372# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200374# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100375static void (*dll_ruby_script) (const char*);
376static int (*dll_rb_enc_find_index) (const char*);
377static rb_encoding* (*dll_rb_enc_find) (const char*);
378static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
379static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100380static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100381static void* (*ruby_process_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200382# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100383
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100384# if defined(USE_RGENGC) && USE_RGENGC
385static void (*dll_rb_gc_writebarrier_unprotect_promoted)(VALUE);
386# endif
387
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200388# if defined(RUBY19_OR_LATER) && !defined(PROTO)
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100389SIGNED_VALUE rb_num2long_stub(VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100390{
391 return dll_rb_num2long(x);
392}
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100393VALUE rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100394{
395 return dll_rb_int2big(x);
396}
Bram Moolenaar498af702014-03-28 21:58:21 +0100397# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19 \
398 && VIM_SIZEOF_INT < VIM_SIZEOF_LONG
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200399long rb_fix2int_stub(VALUE x)
400{
401 return dll_rb_fix2int(x);
402}
403long rb_num2int_stub(VALUE x)
404{
405 return dll_rb_num2int(x);
406}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200407# endif
408# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaar886ed692013-02-26 13:41:35 +0100409VALUE
410rb_float_new_in_heap(double d)
411{
412 return dll_rb_float_new(d);
413}
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100414VALUE rb_num2ulong(VALUE x)
Bram Moolenaar886ed692013-02-26 13:41:35 +0100415{
416 return (long)RSHIFT((SIGNED_VALUE)(x),1);
417}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200418# endif
419# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100420
Bram Moolenaar3e9a1612014-11-12 16:05:04 +0100421 /* Do not generate a prototype here, VALUE isn't always defined. */
422# if defined(USE_RGENGC) && USE_RGENGC && !defined(PROTO)
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100423void rb_gc_writebarrier_unprotect_promoted_stub(VALUE obj)
424{
425 return dll_rb_gc_writebarrier_unprotect_promoted(obj);
426}
427# endif
428
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200429static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430
431/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000432 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434static struct
435{
436 char *name;
437 RUBY_PROC *ptr;
438} ruby_funcname_table[] =
439{
440 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
441 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
442 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200443# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100444 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200445# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
447 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
448 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
449 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
450 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
451 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
452 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
453 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
454 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
455 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
456 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
457 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
458 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
459 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
460 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
461 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
462 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
463 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
464 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
465 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
466 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
467 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
468 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
469 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
470 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
471 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100472# if VIM_SIZEOF_INT < VIM_SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200473 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
474 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
475 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200476# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
478 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
479 {"rb_load_protect", (RUBY_PROC*)&dll_rb_load_protect},
480 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
481 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
482 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
483 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
484 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
485 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200486# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200487 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200488# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200490# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
492 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
493 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200494# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200495 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200496# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200498# endif
499# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100500 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200501# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200503# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
505 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200506# ifdef WIN3264
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100507 {
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200508# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER < 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100509 "NtInitialize",
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200510# else
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100511 "ruby_sysinit",
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200512# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100513 (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200514# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200516# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200517# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200518# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100519 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200520# if DYNAMIC_RUBY_VER <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100521 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200522# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100523 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200524# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100525 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
526 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200527# endif
528# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100529 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100530 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
531 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
532 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
533 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
534 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100535 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100536 {"ruby_process_options", (RUBY_PROC*)&dll_ruby_process_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200537# endif
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200538# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
539# ifdef __ia64
540 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
541# endif
542 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
543# endif
Bram Moolenaara1a118b2014-02-05 22:41:15 +0100544# if defined(USE_RGENGC) && USE_RGENGC
545 {"rb_gc_writebarrier_unprotect_promoted", (RUBY_PROC*)&dll_rb_gc_writebarrier_unprotect_promoted},
546# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 {"", NULL},
548};
549
550/*
551 * Free ruby.dll
552 */
553 static void
554end_dynamic_ruby()
555{
556 if (hinstRuby)
557 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200558 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200559 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 }
561}
562
563/*
564 * Load library and get all pointers.
565 * Parameter 'libname' provides name of DLL.
566 * Return OK or FAIL.
567 */
568 static int
569ruby_runtime_link_init(char *libname, int verbose)
570{
571 int i;
572
573 if (hinstRuby)
574 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200575 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 if (!hinstRuby)
577 {
578 if (verbose)
579 EMSG2(_(e_loadlib), libname);
580 return FAIL;
581 }
582
583 for (i = 0; ruby_funcname_table[i].ptr; ++i)
584 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200585 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 ruby_funcname_table[i].name)))
587 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200588 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200589 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 if (verbose)
591 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
592 return FAIL;
593 }
594 }
595 return OK;
596}
597
598/*
599 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
600 * else FALSE.
601 */
602 int
603ruby_enabled(verbose)
604 int verbose;
605{
606 return ruby_runtime_link_init(DYNAMIC_RUBY_DLL, verbose) == OK;
607}
608#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
609
610 void
611ruby_end()
612{
613#ifdef DYNAMIC_RUBY
614 end_dynamic_ruby();
615#endif
616}
617
618void ex_ruby(exarg_T *eap)
619{
620 int state;
621 char *script = NULL;
622
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000623 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 if (!eap->skip && ensure_ruby_initialized())
625 {
626 if (script == NULL)
627 rb_eval_string_protect((char *)eap->arg, &state);
628 else
629 rb_eval_string_protect(script, &state);
630 if (state)
631 error_print(state);
632 }
633 vim_free(script);
634}
635
Bram Moolenaar165641d2010-02-17 16:23:09 +0100636/*
637 * In Ruby 1.9 or later, ruby String object has encoding.
638 * conversion buffer string of vim to ruby String object using
639 * VIM encoding option.
640 */
641 static VALUE
642vim_str2rb_enc_str(const char *s)
643{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100644#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100645 int isnum;
646 long lval;
647 char_u *sval;
648 rb_encoding *enc;
649
650 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
651 if (isnum == 0)
652 {
653 enc = rb_enc_find((char *)sval);
654 vim_free(sval);
655 if (enc) {
656 return rb_enc_str_new(s, strlen(s), enc);
657 }
658 }
659#endif
660 return rb_str_new2(s);
661}
662
663 static VALUE
664eval_enc_string_protect(const char *str, int *state)
665{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100666#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100667 int isnum;
668 long lval;
669 char_u *sval;
670 rb_encoding *enc;
671 VALUE v;
672
673 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
674 if (isnum == 0)
675 {
676 enc = rb_enc_find((char *)sval);
677 vim_free(sval);
678 if (enc)
679 {
680 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
681 return rb_eval_string_protect(StringValuePtr(v), state);
682 }
683 }
684#endif
685 return rb_eval_string_protect(str, state);
686}
687
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688void ex_rubydo(exarg_T *eap)
689{
690 int state;
691 linenr_T i;
692
693 if (ensure_ruby_initialized())
694 {
695 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
696 return;
697 for (i = eap->line1; i <= eap->line2; i++) {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100698 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100700 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100702 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 if (state) {
704 error_print(state);
705 break;
706 }
707 line = rb_lastline_get();
708 if (!NIL_P(line)) {
709 if (TYPE(line) != T_STRING) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000710 EMSG(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 return;
712 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100713 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 changed();
715#ifdef SYNTAX_HL
716 syn_changed(i); /* recompute syntax hl. for this line */
717#endif
718 }
719 }
720 check_cursor();
721 update_curbuf(NOT_VALID);
722 }
723}
724
725void ex_rubyfile(exarg_T *eap)
726{
727 int state;
728
729 if (ensure_ruby_initialized())
730 {
731 rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
732 if (state) error_print(state);
733 }
734}
735
736void ruby_buffer_free(buf_T *buf)
737{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000738 if (buf->b_ruby_ref)
739 {
740 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
741 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 }
743}
744
745void ruby_window_free(win_T *win)
746{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000747 if (win->w_ruby_ref)
748 {
749 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
750 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 }
752}
753
754static int ensure_ruby_initialized(void)
755{
756 if (!ruby_initialized)
757 {
758#ifdef DYNAMIC_RUBY
759 if (ruby_enabled(TRUE))
760 {
761#endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100762#ifdef _WIN32
763 /* suggested by Ariya Mizutani */
764 int argc = 1;
765 char *argv[] = {"gvim.exe"};
766 NtInitialize(&argc, &argv);
767#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200768 {
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200769#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +0200770 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100771#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200772 ruby_init();
773 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100774#ifdef RUBY19_OR_LATER
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100775 {
776 int dummy_argc = 2;
777 char *dummy_argv[] = {"vim-ruby", "-e0"};
778 ruby_process_options(dummy_argc, dummy_argv);
779 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100780 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100781#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100783#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100784 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 ruby_vim_init();
786 ruby_initialized = 1;
787#ifdef DYNAMIC_RUBY
788 }
789 else
790 {
791 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
792 return 0;
793 }
794#endif
795 }
796 return ruby_initialized;
797}
798
799static void error_print(int state)
800{
801#ifndef DYNAMIC_RUBY
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100802#if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
803 && !(defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 RUBYEXTERN VALUE ruby_errinfo;
805#endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 VALUE eclass;
808 VALUE einfo;
809 char buff[BUFSIZ];
810
811#define TAG_RETURN 0x1
812#define TAG_BREAK 0x2
813#define TAG_NEXT 0x3
814#define TAG_RETRY 0x4
815#define TAG_REDO 0x5
816#define TAG_RAISE 0x6
817#define TAG_THROW 0x7
818#define TAG_FATAL 0x8
819#define TAG_MASK 0xf
820
821 switch (state) {
822 case TAG_RETURN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000823 EMSG(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 break;
825 case TAG_NEXT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000826 EMSG(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 break;
828 case TAG_BREAK:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000829 EMSG(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 break;
831 case TAG_REDO:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000832 EMSG(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 break;
834 case TAG_RETRY:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000835 EMSG(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 break;
837 case TAG_RAISE:
838 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +0100839#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100840 eclass = CLASS_OF(rb_errinfo());
841 einfo = rb_obj_as_string(rb_errinfo());
842#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 eclass = CLASS_OF(ruby_errinfo);
844 einfo = rb_obj_as_string(ruby_errinfo);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100845#endif
846 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000847 EMSG(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 }
849 else {
850 VALUE epath;
851 char *p;
852
853 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000854 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar165641d2010-02-17 16:23:09 +0100855 RSTRING_PTR(epath), RSTRING_PTR(einfo));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 p = strchr(buff, '\n');
857 if (p) *p = '\0';
858 EMSG(buff);
859 }
860 break;
861 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000862 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 EMSG(buff);
864 break;
865 }
866}
867
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000868static VALUE vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869{
870 char *buff, *p;
871
872 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +0200873 if (RSTRING_LEN(str) > 0)
874 {
875 /* Only do this when the string isn't empty, alloc(0) causes trouble. */
876 buff = ALLOCA_N(char, RSTRING_LEN(str));
877 strcpy(buff, RSTRING_PTR(str));
878 p = strchr(buff, '\n');
879 if (p) *p = '\0';
880 MSG(buff);
881 }
882 else
883 {
884 MSG("");
885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 return Qnil;
887}
888
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000889static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100891 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 update_screen(NOT_VALID);
893 return Qnil;
894}
895
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000896static VALUE vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100898 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 return Qnil;
900}
901
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100902#ifdef FEAT_EVAL
903static VALUE vim_to_ruby(typval_T *tv)
904{
905 VALUE result = Qnil;
906
907 if (tv->v_type == VAR_STRING)
908 {
Bram Moolenaar94127e42010-03-19 23:08:48 +0100909 result = rb_str_new2(tv->vval.v_string == NULL
910 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100911 }
912 else if (tv->v_type == VAR_NUMBER)
913 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100914 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100915 }
916# ifdef FEAT_FLOAT
917 else if (tv->v_type == VAR_FLOAT)
918 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100919 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100920 }
921# endif
922 else if (tv->v_type == VAR_LIST)
923 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100924 list_T *list = tv->vval.v_list;
925 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100926
Bram Moolenaar639a2552010-03-17 18:15:23 +0100927 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100928
Bram Moolenaar639a2552010-03-17 18:15:23 +0100929 if (list != NULL)
930 {
931 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
932 {
933 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
934 }
935 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100936 }
937 else if (tv->v_type == VAR_DICT)
938 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100939 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100940
Bram Moolenaar639a2552010-03-17 18:15:23 +0100941 if (tv->vval.v_dict != NULL)
942 {
943 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
944 long_u todo = ht->ht_used;
945 hashitem_T *hi;
946 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100947
Bram Moolenaar639a2552010-03-17 18:15:23 +0100948 for (hi = ht->ht_array; todo > 0; ++hi)
949 {
950 if (!HASHITEM_EMPTY(hi))
951 {
952 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100953
Bram Moolenaar639a2552010-03-17 18:15:23 +0100954 di = dict_lookup(hi);
955 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100956 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +0100957 }
958 }
959 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100960 } /* else return Qnil; */
961
962 return result;
963}
964#endif
965
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000966static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967{
968#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100969 typval_T *tv;
970 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100972 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
973 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100975 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100977 result = vim_to_ruby(tv);
978
979 free_tv(tv);
980
981 return result;
982#else
983 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985}
986
987static VALUE buffer_new(buf_T *buf)
988{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000989 if (buf->b_ruby_ref)
990 {
991 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 }
Bram Moolenaare344bea2005-09-01 20:46:49 +0000993 else
994 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000996 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
998 return obj;
999 }
1000}
1001
1002static buf_T *get_buf(VALUE obj)
1003{
1004 buf_T *buf;
1005
1006 Data_Get_Struct(obj, buf_T, buf);
1007 if (buf == NULL)
1008 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
1009 return buf;
1010}
1011
1012static VALUE buffer_s_current()
1013{
1014 return buffer_new(curbuf);
1015}
1016
1017static VALUE buffer_s_count()
1018{
1019 buf_T *b;
1020 int n = 0;
1021
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001022 for (b = firstbuf; b != NULL; b = b->b_next)
1023 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001024 /* Deleted buffers should not be counted
1025 * SegPhault - 01/07/05 */
1026 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001027 n++;
1028 }
1029
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 return INT2NUM(n);
1031}
1032
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001033static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034{
1035 buf_T *b;
1036 int n = NUM2INT(num);
1037
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001038 for (b = firstbuf; b != NULL; b = b->b_next)
1039 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001040 /* Deleted buffers should not be counted
1041 * SegPhault - 01/07/05 */
1042 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001043 continue;
1044
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001045 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001047
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001048 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 }
1050 return Qnil;
1051}
1052
1053static VALUE buffer_name(VALUE self)
1054{
1055 buf_T *buf = get_buf(self);
1056
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001057 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058}
1059
1060static VALUE buffer_number(VALUE self)
1061{
1062 buf_T *buf = get_buf(self);
1063
1064 return INT2NUM(buf->b_fnum);
1065}
1066
1067static VALUE buffer_count(VALUE self)
1068{
1069 buf_T *buf = get_buf(self);
1070
1071 return INT2NUM(buf->b_ml.ml_line_count);
1072}
1073
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001074static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001076 if (n <= 0 || n > buf->b_ml.ml_line_count)
1077 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1078 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079}
1080
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001081static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082{
1083 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001084
1085 if (buf != NULL)
1086 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
1087 return Qnil; /* For stop warning */
1088}
1089
1090static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
1091{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001092 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001093 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001095 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1096 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001097 /* set curwin/curbuf for "buf" and save some things */
1098 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001099
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 if (u_savesub(n) == OK) {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001101 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 changed();
1103#ifdef SYNTAX_HL
1104 syn_changed(n); /* recompute syntax hl. for this line */
1105#endif
1106 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001107
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001108 /* restore curwin/curbuf and a few other things */
1109 aucmd_restbuf(&aco);
1110 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001111
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 update_curbuf(NOT_VALID);
1113 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001114 else
1115 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001116 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 }
1118 return str;
1119}
1120
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001121static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
1122{
1123 buf_T *buf = get_buf(self);
1124
1125 if (buf != NULL)
1126 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1127 return str;
1128}
1129
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130static VALUE buffer_delete(VALUE self, VALUE num)
1131{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001132 buf_T *buf = get_buf(self);
1133 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001134 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001136 if (n > 0 && n <= buf->b_ml.ml_line_count)
1137 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001138 /* set curwin/curbuf for "buf" and save some things */
1139 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001140
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 if (u_savedel(n, 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001143
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001144 /* Changes to non-active buffers should properly refresh
1145 * SegPhault - 01/09/05 */
1146 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001147
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 changed();
1149 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001150
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001151 /* restore curwin/curbuf and a few other things */
1152 aucmd_restbuf(&aco);
1153 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001154
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 update_curbuf(NOT_VALID);
1156 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001157 else
1158 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001159 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 }
1161 return Qnil;
1162}
1163
1164static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
1165{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001166 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001167 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001168 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001169 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170
Bram Moolenaar3c531602010-11-16 14:46:19 +01001171 if (line == NULL)
1172 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001173 rb_raise(rb_eIndexError, "NULL line");
1174 }
1175 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001176 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001177 /* set curwin/curbuf for "buf" and save some things */
1178 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001179
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 if (u_inssub(n + 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001182
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001183 /* Changes to non-active buffers should properly refresh screen
1184 * SegPhault - 12/20/04 */
1185 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001186
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001187 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001189
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001190 /* restore curwin/curbuf and a few other things */
1191 aucmd_restbuf(&aco);
1192 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001193
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 update_curbuf(NOT_VALID);
1195 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001196 else
1197 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001198 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 }
1200 return str;
1201}
1202
1203static VALUE window_new(win_T *win)
1204{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001205 if (win->w_ruby_ref)
1206 {
1207 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001209 else
1210 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001212 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1214 return obj;
1215 }
1216}
1217
1218static win_T *get_win(VALUE obj)
1219{
1220 win_T *win;
1221
1222 Data_Get_Struct(obj, win_T, win);
1223 if (win == NULL)
1224 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1225 return win;
1226}
1227
1228static VALUE window_s_current()
1229{
1230 return window_new(curwin);
1231}
1232
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001233/*
1234 * Added line manipulation functions
1235 * SegPhault - 03/07/05
1236 */
1237static VALUE line_s_current()
1238{
1239 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1240}
1241
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001242static VALUE set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001243{
1244 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1245}
1246
1247static VALUE current_line_number()
1248{
1249 return INT2FIX((int)curwin->w_cursor.lnum);
1250}
1251
1252
1253
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254static VALUE window_s_count()
1255{
1256#ifdef FEAT_WINDOWS
1257 win_T *w;
1258 int n = 0;
1259
Bram Moolenaarf740b292006-02-16 22:11:02 +00001260 for (w = firstwin; w != NULL; w = w->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 n++;
1262 return INT2NUM(n);
1263#else
1264 return INT2NUM(1);
1265#endif
1266}
1267
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001268static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269{
1270 win_T *w;
1271 int n = NUM2INT(num);
1272
1273#ifndef FEAT_WINDOWS
1274 w = curwin;
1275#else
1276 for (w = firstwin; w != NULL; w = w->w_next, --n)
1277#endif
1278 if (n == 0)
1279 return window_new(w);
1280 return Qnil;
1281}
1282
1283static VALUE window_buffer(VALUE self)
1284{
1285 win_T *win = get_win(self);
1286
1287 return buffer_new(win->w_buffer);
1288}
1289
1290static VALUE window_height(VALUE self)
1291{
1292 win_T *win = get_win(self);
1293
1294 return INT2NUM(win->w_height);
1295}
1296
1297static VALUE window_set_height(VALUE self, VALUE height)
1298{
1299 win_T *win = get_win(self);
1300 win_T *savewin = curwin;
1301
1302 curwin = win;
1303 win_setheight(NUM2INT(height));
1304 curwin = savewin;
1305 return height;
1306}
1307
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001308static VALUE window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001309{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001310 return INT2NUM(W_WIDTH(get_win(self)));
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001311}
1312
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001313static VALUE window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001314{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001315#ifdef FEAT_VERTSPLIT
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001316 win_T *win = get_win(self);
1317 win_T *savewin = curwin;
1318
1319 curwin = win;
1320 win_setwidth(NUM2INT(width));
1321 curwin = savewin;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001322#endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001323 return width;
1324}
1325
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326static VALUE window_cursor(VALUE self)
1327{
1328 win_T *win = get_win(self);
1329
1330 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1331}
1332
1333static VALUE window_set_cursor(VALUE self, VALUE pos)
1334{
1335 VALUE lnum, col;
1336 win_T *win = get_win(self);
1337
1338 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001339 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001341 lnum = RARRAY_PTR(pos)[0];
1342 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 win->w_cursor.lnum = NUM2LONG(lnum);
1344 win->w_cursor.col = NUM2UINT(col);
1345 check_cursor(); /* put cursor on an existing line */
1346 update_screen(NOT_VALID);
1347 return Qnil;
1348}
1349
Bram Moolenaar6217cdc2012-04-25 12:28:09 +02001350static VALUE f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001351{
1352 return Qnil;
1353}
1354
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001355static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356{
1357 int i;
1358 VALUE str = rb_str_new("", 0);
1359
1360 for (i = 0; i < argc; i++) {
1361 if (i > 0) rb_str_cat(str, ", ", 2);
1362 rb_str_concat(str, rb_inspect(argv[i]));
1363 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001364 MSG(RSTRING_PTR(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 return Qnil;
1366}
1367
1368static void ruby_io_init(void)
1369{
1370#ifndef DYNAMIC_RUBY
1371 RUBYEXTERN VALUE rb_stdout;
1372#endif
1373
1374 rb_stdout = rb_obj_alloc(rb_cObject);
1375 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001376 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 rb_define_global_function("p", f_p, -1);
1378}
1379
1380static void ruby_vim_init(void)
1381{
1382 objtbl = rb_hash_new();
1383 rb_global_variable(&objtbl);
1384
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001385 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001386 * alias "VIM" for backwards compatibility. */
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001387 mVIM = rb_define_module("Vim");
1388 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1390 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1391 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1392 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1393 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1394 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1395 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1396 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1397 rb_define_module_function(mVIM, "message", vim_message, 1);
1398 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1399 rb_define_module_function(mVIM, "command", vim_command, 1);
1400 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
1401
1402 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1403 rb_eStandardError);
1404 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1405 rb_eStandardError);
1406
1407 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1408 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1409 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1410 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1411 rb_define_method(cBuffer, "name", buffer_name, 0);
1412 rb_define_method(cBuffer, "number", buffer_number, 0);
1413 rb_define_method(cBuffer, "count", buffer_count, 0);
1414 rb_define_method(cBuffer, "length", buffer_count, 0);
1415 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1416 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1417 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1418 rb_define_method(cBuffer, "append", buffer_append, 2);
1419
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001420 /* Added line manipulation functions
1421 * SegPhault - 03/07/05 */
1422 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1423 rb_define_method(cBuffer, "line", line_s_current, 0);
1424 rb_define_method(cBuffer, "line=", set_current_line, 1);
1425
1426
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1428 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1429 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1430 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1431 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1432 rb_define_method(cVimWindow, "height", window_height, 0);
1433 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001434 rb_define_method(cVimWindow, "width", window_width, 0);
1435 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1437 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1438
1439 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1440 rb_define_virtual_variable("$curwin", window_s_current, 0);
1441}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001442
1443void vim_ruby_init(void *stack_start)
1444{
1445 /* should get machine stack start address early in main function */
1446 ruby_stack_start = stack_start;
1447}