blob: d7df24819d25aeecbbc39aa8ee9f4fe4510e6bd6 [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
14#include <stdio.h>
15#include <string.h>
16
17#ifdef _WIN32
18# if !defined(DYNAMIC_RUBY_VER) || (DYNAMIC_RUBY_VER < 18)
19# define NT
20# endif
21# ifndef DYNAMIC_RUBY
22# define IMPORT /* For static dll usage __declspec(dllimport) */
23# define RUBYEXTERN __declspec(dllimport)
24# endif
25#endif
26#ifndef RUBYEXTERN
27# define RUBYEXTERN extern
28#endif
29
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020030#ifdef DYNAMIC_RUBY
Bram Moolenaar071d4272004-06-13 20:20:40 +000031/*
32 * This is tricky. In ruby.h there is (inline) function rb_class_of()
33 * definition. This function use these variables. But we want function to
34 * use dll_* variables.
35 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000036# define rb_cFalseClass (*dll_rb_cFalseClass)
37# define rb_cFixnum (*dll_rb_cFixnum)
38# define rb_cNilClass (*dll_rb_cNilClass)
39# define rb_cSymbol (*dll_rb_cSymbol)
40# define rb_cTrueClass (*dll_rb_cTrueClass)
41# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
42/*
Bram Moolenaar42d57f02010-03-10 12:47:00 +010043 * On ver 1.8, all Ruby functions are exported with "__declspec(dllimport)"
44 * in ruby.h. But it causes trouble for these variables, because it is
Bram Moolenaar071d4272004-06-13 20:20:40 +000045 * defined in this file. When defined this RUBY_EXPORT it modified to
46 * "extern" and be able to avoid this problem.
47 */
48# define RUBY_EXPORT
49# endif
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020050
51#if !(defined(WIN32) || defined(_WIN64))
52# include <dlfcn.h>
53# define HANDLE void*
54# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
55# define symbol_from_dll dlsym
56# define close_dll dlclose
57#else
Bram Moolenaarebbcb822010-10-23 14:02:54 +020058# define load_dll vimLoadLib
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020059# define symbol_from_dll GetProcAddress
60# define close_dll FreeLibrary
Bram Moolenaar071d4272004-06-13 20:20:40 +000061#endif
62
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020063#endif /* ifdef DYNAMIC_RUBY */
64
Bram Moolenaar0b69c732010-02-17 15:11:50 +010065/* suggested by Ariya Mizutani */
66#if (_MSC_VER == 1200)
67# undef _WIN32_WINNT
68#endif
69
Bram Moolenaar639a2552010-03-17 18:15:23 +010070#if (defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
71 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
72# define RUBY19_OR_LATER 1
73#endif
74
Bram Moolenaar42d57f02010-03-10 12:47:00 +010075#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
76/* Ruby 1.9 defines a number of static functions which use rb_num2long and
77 * rb_int2big */
78# define rb_num2long rb_num2long_stub
79# define rb_int2big rb_int2big_stub
80#endif
81
Bram Moolenaar071d4272004-06-13 20:20:40 +000082#include <ruby.h>
Bram Moolenaar639a2552010-03-17 18:15:23 +010083#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +010084# include <ruby/encoding.h>
85#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000086
87#undef EXTERN
88#undef _
89
90/* T_DATA defined both by Ruby and Mac header files, hack around it... */
Bram Moolenaarcb635362007-05-12 13:02:42 +000091#if defined(MACOS_X_UNIX) || defined(macintosh)
Bram Moolenaar071d4272004-06-13 20:20:40 +000092# define __OPENTRANSPORT__
93# define __OPENTRANSPORTPROTOCOL__
94# define __OPENTRANSPORTPROVIDERS__
95#endif
96
Bram Moolenaar165641d2010-02-17 16:23:09 +010097/*
98 * Backward compatiblity for Ruby 1.8 and earlier.
99 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
100 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
101 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
102 */
103#ifndef StringValuePtr
104# define StringValuePtr(s) STR2CSTR(s)
105#endif
106#ifndef RARRAY_LEN
107# define RARRAY_LEN(s) RARRAY(s)->len
108#endif
109#ifndef RARRAY_PTR
110# define RARRAY_PTR(s) RARRAY(s)->ptr
111#endif
112#ifndef RSTRING_LEN
113# define RSTRING_LEN(s) RSTRING(s)->len
114#endif
115#ifndef RSTRING_PTR
116# define RSTRING_PTR(s) RSTRING(s)->ptr
117#endif
118
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119#include "vim.h"
120#include "version.h"
121
122#if defined(PROTO) && !defined(FEAT_RUBY)
123/* Define these to be able to generate the function prototypes. */
124# define VALUE int
125# define RUBY_DATA_FUNC int
126#endif
127
128static int ruby_initialized = 0;
129static VALUE objtbl;
130
131static VALUE mVIM;
132static VALUE cBuffer;
133static VALUE cVimWindow;
134static VALUE eDeletedBufferError;
135static VALUE eDeletedWindowError;
136
137static int ensure_ruby_initialized(void);
138static void error_print(int);
139static void ruby_io_init(void);
140static void ruby_vim_init(void);
141
142#if defined(DYNAMIC_RUBY) || defined(PROTO)
143#ifdef PROTO
144# define HINSTANCE int /* for generating prototypes */
145#endif
146
147/*
148 * Wrapper defines
149 */
150#define rb_assoc_new dll_rb_assoc_new
151#define rb_cObject (*dll_rb_cObject)
152#define rb_check_type dll_rb_check_type
153#define rb_class_path dll_rb_class_path
154#define rb_data_object_alloc dll_rb_data_object_alloc
155#define rb_define_class_under dll_rb_define_class_under
156#define rb_define_const dll_rb_define_const
157#define rb_define_global_function dll_rb_define_global_function
158#define rb_define_method dll_rb_define_method
159#define rb_define_module dll_rb_define_module
160#define rb_define_module_function dll_rb_define_module_function
161#define rb_define_singleton_method dll_rb_define_singleton_method
162#define rb_define_virtual_variable dll_rb_define_virtual_variable
163#define rb_stdout (*dll_rb_stdout)
164#define rb_eArgError (*dll_rb_eArgError)
165#define rb_eIndexError (*dll_rb_eIndexError)
166#define rb_eRuntimeError (*dll_rb_eRuntimeError)
167#define rb_eStandardError (*dll_rb_eStandardError)
168#define rb_eval_string_protect dll_rb_eval_string_protect
169#define rb_global_variable dll_rb_global_variable
170#define rb_hash_aset dll_rb_hash_aset
171#define rb_hash_new dll_rb_hash_new
172#define rb_inspect dll_rb_inspect
173#define rb_int2inum dll_rb_int2inum
174#define rb_lastline_get dll_rb_lastline_get
175#define rb_lastline_set dll_rb_lastline_set
176#define rb_load_protect dll_rb_load_protect
177#define rb_num2long dll_rb_num2long
178#define rb_num2ulong dll_rb_num2ulong
179#define rb_obj_alloc dll_rb_obj_alloc
180#define rb_obj_as_string dll_rb_obj_as_string
181#define rb_obj_id dll_rb_obj_id
182#define rb_raise dll_rb_raise
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183#define rb_str_cat dll_rb_str_cat
184#define rb_str_concat dll_rb_str_concat
185#define rb_str_new dll_rb_str_new
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200186#ifdef rb_str_new2
187/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
188# define need_rb_str_new_cstr 1
189# define rb_str_new_cstr dll_rb_str_new_cstr
190#else
Bram Moolenaar218116c2010-05-20 21:46:00 +0200191# define rb_str_new2 dll_rb_str_new2
192#endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100193#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200194# define rb_string_value dll_rb_string_value
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100195# define rb_string_value_ptr dll_rb_string_value_ptr
196# define rb_float_new dll_rb_float_new
197# define rb_ary_new dll_rb_ary_new
198# define rb_ary_push dll_rb_ary_push
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200199#else
200# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100201#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100202#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100203# define rb_errinfo dll_rb_errinfo
204#else
205# define ruby_errinfo (*dll_ruby_errinfo)
206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#define ruby_init dll_ruby_init
208#define ruby_init_loadpath dll_ruby_init_loadpath
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100209#define NtInitialize dll_NtInitialize
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
211# define rb_w32_snprintf dll_rb_w32_snprintf
212#endif
213
Bram Moolenaar639a2552010-03-17 18:15:23 +0100214#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100215# define ruby_script dll_ruby_script
216# define rb_enc_find_index dll_rb_enc_find_index
217# define rb_enc_find dll_rb_enc_find
218# define rb_enc_str_new dll_rb_enc_str_new
219# define rb_sprintf dll_rb_sprintf
Bram Moolenaar639a2552010-03-17 18:15:23 +0100220# define ruby_init_stack dll_ruby_init_stack
Bram Moolenaar165641d2010-02-17 16:23:09 +0100221#endif
222
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223/*
224 * Pointers for dynamic link
225 */
226static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200227VALUE *dll_rb_cFalseClass;
228VALUE *dll_rb_cFixnum;
229VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230static VALUE *dll_rb_cObject;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200231VALUE *dll_rb_cSymbol;
232VALUE *dll_rb_cTrueClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233static void (*dll_rb_check_type) (VALUE,int);
234static VALUE (*dll_rb_class_path) (VALUE);
235static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
236static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
237static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
238static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
239static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
240static VALUE (*dll_rb_define_module) (const char*);
241static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
242static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
243static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
244static VALUE *dll_rb_stdout;
245static VALUE *dll_rb_eArgError;
246static VALUE *dll_rb_eIndexError;
247static VALUE *dll_rb_eRuntimeError;
248static VALUE *dll_rb_eStandardError;
249static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
250static void (*dll_rb_global_variable) (VALUE*);
251static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
252static VALUE (*dll_rb_hash_new) (void);
253static VALUE (*dll_rb_inspect) (VALUE);
254static VALUE (*dll_rb_int2inum) (long);
255static VALUE (*dll_rb_int2inum) (long);
256static VALUE (*dll_rb_lastline_get) (void);
257static void (*dll_rb_lastline_set) (VALUE);
258static void (*dll_rb_load_protect) (VALUE, int, int*);
259static long (*dll_rb_num2long) (VALUE);
260static unsigned long (*dll_rb_num2ulong) (VALUE);
261static VALUE (*dll_rb_obj_alloc) (VALUE);
262static VALUE (*dll_rb_obj_as_string) (VALUE);
263static VALUE (*dll_rb_obj_id) (VALUE);
264static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200265#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
266static VALUE (*dll_rb_string_value) (volatile VALUE*);
267#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
271static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
272static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200273#ifdef need_rb_str_new_cstr
274/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
275static VALUE (*dll_rb_str_new_cstr) (const char*);
276#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200278#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100279#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100280static VALUE (*dll_rb_errinfo) (void);
281#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282static VALUE *dll_ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +0100283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284static void (*dll_ruby_init) (void);
285static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100286static void (*dll_NtInitialize) (int*, char***);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100288static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
289static VALUE (*dll_rb_float_new) (double);
290static VALUE (*dll_rb_ary_new) (void);
291static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
292#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100293#ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100294static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
295#endif
296#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
298#endif
299
Bram Moolenaar639a2552010-03-17 18:15:23 +0100300#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100301static void (*dll_ruby_script) (const char*);
302static int (*dll_rb_enc_find_index) (const char*);
303static rb_encoding* (*dll_rb_enc_find) (const char*);
304static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
305static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar639a2552010-03-17 18:15:23 +0100306static void (*ruby_init_stack)(VALUE*);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100307#endif
308
Bram Moolenaar639a2552010-03-17 18:15:23 +0100309#ifdef RUBY19_OR_LATER
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200310SIGNED_VALUE rb_num2long_stub(VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100311{
312 return dll_rb_num2long(x);
313}
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200314VALUE rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100315{
316 return dll_rb_int2big(x);
317}
318#endif
319
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320static HINSTANCE hinstRuby = 0; /* Instance of ruby.dll */
321
322/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000323 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 */
325#define RUBY_PROC FARPROC
326static struct
327{
328 char *name;
329 RUBY_PROC *ptr;
330} ruby_funcname_table[] =
331{
332 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
333 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
334 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
335 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
336 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
337 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
338 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
339 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
340 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
341 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
342 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
343 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
344 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
345 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
346 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
347 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
348 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
349 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
350 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
351 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
352 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
353 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
354 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
355 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
356 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
357 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
358 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
359 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
360 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
361 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
362 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
363 {"rb_load_protect", (RUBY_PROC*)&dll_rb_load_protect},
364 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
365 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
366 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
367 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
368 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
369 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200370#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
371 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
372#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
376 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
377 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200378#ifdef need_rb_str_new_cstr
379 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
380#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200382#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100383#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100384 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
385#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
389 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100390 {
391#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER < 19
392 "NtInitialize",
393#else
394 "ruby_sysinit",
395#endif
396 (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
398 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
399#endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100400#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
401 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
402 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
403 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
404 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
405#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100406#ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100407 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100408 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
409 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
410 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
411 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
412 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar639a2552010-03-17 18:15:23 +0100413 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 {"", NULL},
416};
417
418/*
419 * Free ruby.dll
420 */
421 static void
422end_dynamic_ruby()
423{
424 if (hinstRuby)
425 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200426 close_dll(hinstRuby);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 hinstRuby = 0;
428 }
429}
430
431/*
432 * Load library and get all pointers.
433 * Parameter 'libname' provides name of DLL.
434 * Return OK or FAIL.
435 */
436 static int
437ruby_runtime_link_init(char *libname, int verbose)
438{
439 int i;
440
441 if (hinstRuby)
442 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200443 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 if (!hinstRuby)
445 {
446 if (verbose)
447 EMSG2(_(e_loadlib), libname);
448 return FAIL;
449 }
450
451 for (i = 0; ruby_funcname_table[i].ptr; ++i)
452 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200453 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 ruby_funcname_table[i].name)))
455 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200456 close_dll(hinstRuby);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 hinstRuby = 0;
458 if (verbose)
459 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
460 return FAIL;
461 }
462 }
463 return OK;
464}
465
466/*
467 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
468 * else FALSE.
469 */
470 int
471ruby_enabled(verbose)
472 int verbose;
473{
474 return ruby_runtime_link_init(DYNAMIC_RUBY_DLL, verbose) == OK;
475}
476#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
477
478 void
479ruby_end()
480{
481#ifdef DYNAMIC_RUBY
482 end_dynamic_ruby();
483#endif
484}
485
486void ex_ruby(exarg_T *eap)
487{
488 int state;
489 char *script = NULL;
490
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000491 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 if (!eap->skip && ensure_ruby_initialized())
493 {
494 if (script == NULL)
495 rb_eval_string_protect((char *)eap->arg, &state);
496 else
497 rb_eval_string_protect(script, &state);
498 if (state)
499 error_print(state);
500 }
501 vim_free(script);
502}
503
Bram Moolenaar165641d2010-02-17 16:23:09 +0100504/*
505 * In Ruby 1.9 or later, ruby String object has encoding.
506 * conversion buffer string of vim to ruby String object using
507 * VIM encoding option.
508 */
509 static VALUE
510vim_str2rb_enc_str(const char *s)
511{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100512#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100513 int isnum;
514 long lval;
515 char_u *sval;
516 rb_encoding *enc;
517
518 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
519 if (isnum == 0)
520 {
521 enc = rb_enc_find((char *)sval);
522 vim_free(sval);
523 if (enc) {
524 return rb_enc_str_new(s, strlen(s), enc);
525 }
526 }
527#endif
528 return rb_str_new2(s);
529}
530
531 static VALUE
532eval_enc_string_protect(const char *str, int *state)
533{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100534#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100535 int isnum;
536 long lval;
537 char_u *sval;
538 rb_encoding *enc;
539 VALUE v;
540
541 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
542 if (isnum == 0)
543 {
544 enc = rb_enc_find((char *)sval);
545 vim_free(sval);
546 if (enc)
547 {
548 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
549 return rb_eval_string_protect(StringValuePtr(v), state);
550 }
551 }
552#endif
553 return rb_eval_string_protect(str, state);
554}
555
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556void ex_rubydo(exarg_T *eap)
557{
558 int state;
559 linenr_T i;
560
561 if (ensure_ruby_initialized())
562 {
563 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
564 return;
565 for (i = eap->line1; i <= eap->line2; i++) {
566 VALUE line, oldline;
567
Bram Moolenaar165641d2010-02-17 16:23:09 +0100568 line = oldline = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100570 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 if (state) {
572 error_print(state);
573 break;
574 }
575 line = rb_lastline_get();
576 if (!NIL_P(line)) {
577 if (TYPE(line) != T_STRING) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000578 EMSG(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 return;
580 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100581 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 changed();
583#ifdef SYNTAX_HL
584 syn_changed(i); /* recompute syntax hl. for this line */
585#endif
586 }
587 }
588 check_cursor();
589 update_curbuf(NOT_VALID);
590 }
591}
592
593void ex_rubyfile(exarg_T *eap)
594{
595 int state;
596
597 if (ensure_ruby_initialized())
598 {
599 rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
600 if (state) error_print(state);
601 }
602}
603
604void ruby_buffer_free(buf_T *buf)
605{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000606 if (buf->b_ruby_ref)
607 {
608 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
609 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 }
611}
612
613void ruby_window_free(win_T *win)
614{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000615 if (win->w_ruby_ref)
616 {
617 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
618 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 }
620}
621
622static int ensure_ruby_initialized(void)
623{
624 if (!ruby_initialized)
625 {
626#ifdef DYNAMIC_RUBY
627 if (ruby_enabled(TRUE))
628 {
629#endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100630#ifdef _WIN32
631 /* suggested by Ariya Mizutani */
632 int argc = 1;
633 char *argv[] = {"gvim.exe"};
634 NtInitialize(&argc, &argv);
635#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200636 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100637#ifdef RUBY19_OR_LATER
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200638 RUBY_INIT_STACK;
Bram Moolenaar165641d2010-02-17 16:23:09 +0100639#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200640 ruby_init();
641 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100642#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100643 ruby_script("vim-ruby");
644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 ruby_init_loadpath();
646 ruby_io_init();
Bram Moolenaar639a2552010-03-17 18:15:23 +0100647#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100648 rb_enc_find_index("encdb");
649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 ruby_vim_init();
651 ruby_initialized = 1;
652#ifdef DYNAMIC_RUBY
653 }
654 else
655 {
656 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
657 return 0;
658 }
659#endif
660 }
661 return ruby_initialized;
662}
663
664static void error_print(int state)
665{
666#ifndef DYNAMIC_RUBY
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100667#if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
668 && !(defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 RUBYEXTERN VALUE ruby_errinfo;
670#endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 VALUE eclass;
673 VALUE einfo;
674 char buff[BUFSIZ];
675
676#define TAG_RETURN 0x1
677#define TAG_BREAK 0x2
678#define TAG_NEXT 0x3
679#define TAG_RETRY 0x4
680#define TAG_REDO 0x5
681#define TAG_RAISE 0x6
682#define TAG_THROW 0x7
683#define TAG_FATAL 0x8
684#define TAG_MASK 0xf
685
686 switch (state) {
687 case TAG_RETURN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000688 EMSG(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 break;
690 case TAG_NEXT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000691 EMSG(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 break;
693 case TAG_BREAK:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000694 EMSG(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 break;
696 case TAG_REDO:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000697 EMSG(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 break;
699 case TAG_RETRY:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000700 EMSG(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 break;
702 case TAG_RAISE:
703 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +0100704#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100705 eclass = CLASS_OF(rb_errinfo());
706 einfo = rb_obj_as_string(rb_errinfo());
707#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 eclass = CLASS_OF(ruby_errinfo);
709 einfo = rb_obj_as_string(ruby_errinfo);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100710#endif
711 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000712 EMSG(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 }
714 else {
715 VALUE epath;
716 char *p;
717
718 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000719 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar165641d2010-02-17 16:23:09 +0100720 RSTRING_PTR(epath), RSTRING_PTR(einfo));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 p = strchr(buff, '\n');
722 if (p) *p = '\0';
723 EMSG(buff);
724 }
725 break;
726 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000727 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 EMSG(buff);
729 break;
730 }
731}
732
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000733static VALUE vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734{
735 char *buff, *p;
736
737 str = rb_obj_as_string(str);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100738 buff = ALLOCA_N(char, RSTRING_LEN(str));
739 strcpy(buff, RSTRING_PTR(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 p = strchr(buff, '\n');
741 if (p) *p = '\0';
742 MSG(buff);
743 return Qnil;
744}
745
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000746static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100748 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 update_screen(NOT_VALID);
750 return Qnil;
751}
752
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000753static VALUE vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100755 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 return Qnil;
757}
758
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100759#ifdef FEAT_EVAL
760static VALUE vim_to_ruby(typval_T *tv)
761{
762 VALUE result = Qnil;
763
764 if (tv->v_type == VAR_STRING)
765 {
Bram Moolenaar94127e42010-03-19 23:08:48 +0100766 result = rb_str_new2(tv->vval.v_string == NULL
767 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100768 }
769 else if (tv->v_type == VAR_NUMBER)
770 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100771 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100772 }
773# ifdef FEAT_FLOAT
774 else if (tv->v_type == VAR_FLOAT)
775 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100776 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100777 }
778# endif
779 else if (tv->v_type == VAR_LIST)
780 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100781 list_T *list = tv->vval.v_list;
782 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100783
Bram Moolenaar639a2552010-03-17 18:15:23 +0100784 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100785
Bram Moolenaar639a2552010-03-17 18:15:23 +0100786 if (list != NULL)
787 {
788 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
789 {
790 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
791 }
792 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100793 }
794 else if (tv->v_type == VAR_DICT)
795 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100796 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100797
Bram Moolenaar639a2552010-03-17 18:15:23 +0100798 if (tv->vval.v_dict != NULL)
799 {
800 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
801 long_u todo = ht->ht_used;
802 hashitem_T *hi;
803 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100804
Bram Moolenaar639a2552010-03-17 18:15:23 +0100805 for (hi = ht->ht_array; todo > 0; ++hi)
806 {
807 if (!HASHITEM_EMPTY(hi))
808 {
809 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100810
Bram Moolenaar639a2552010-03-17 18:15:23 +0100811 di = dict_lookup(hi);
812 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100813 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +0100814 }
815 }
816 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100817 } /* else return Qnil; */
818
819 return result;
820}
821#endif
822
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000823static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824{
825#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100826 typval_T *tv;
827 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100829 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
830 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100832 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100834 result = vim_to_ruby(tv);
835
836 free_tv(tv);
837
838 return result;
839#else
840 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842}
843
844static VALUE buffer_new(buf_T *buf)
845{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000846 if (buf->b_ruby_ref)
847 {
848 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 }
Bram Moolenaare344bea2005-09-01 20:46:49 +0000850 else
851 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000853 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
855 return obj;
856 }
857}
858
859static buf_T *get_buf(VALUE obj)
860{
861 buf_T *buf;
862
863 Data_Get_Struct(obj, buf_T, buf);
864 if (buf == NULL)
865 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
866 return buf;
867}
868
869static VALUE buffer_s_current()
870{
871 return buffer_new(curbuf);
872}
873
874static VALUE buffer_s_count()
875{
876 buf_T *b;
877 int n = 0;
878
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000879 for (b = firstbuf; b != NULL; b = b->b_next)
880 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000881 /* Deleted buffers should not be counted
882 * SegPhault - 01/07/05 */
883 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000884 n++;
885 }
886
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 return INT2NUM(n);
888}
889
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000890static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891{
892 buf_T *b;
893 int n = NUM2INT(num);
894
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000895 for (b = firstbuf; b != NULL; b = b->b_next)
896 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000897 /* Deleted buffers should not be counted
898 * SegPhault - 01/07/05 */
899 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000900 continue;
901
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000902 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000904
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000905 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 }
907 return Qnil;
908}
909
910static VALUE buffer_name(VALUE self)
911{
912 buf_T *buf = get_buf(self);
913
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000914 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915}
916
917static VALUE buffer_number(VALUE self)
918{
919 buf_T *buf = get_buf(self);
920
921 return INT2NUM(buf->b_fnum);
922}
923
924static VALUE buffer_count(VALUE self)
925{
926 buf_T *buf = get_buf(self);
927
928 return INT2NUM(buf->b_ml.ml_line_count);
929}
930
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000931static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932{
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000933 if (n > 0 && n <= buf->b_ml.ml_line_count)
934 {
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000935 char *line = (char *)ml_get_buf(buf, n, FALSE);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100936 return line ? vim_str2rb_enc_str(line) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100938 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000939#ifndef __GNUC__
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000940 return Qnil; /* For stop warning */
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942}
943
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000944static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945{
946 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000947
948 if (buf != NULL)
949 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
950 return Qnil; /* For stop warning */
951}
952
953static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
954{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100955 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000956 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000958 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
959 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000960 /* set curwin/curbuf for "buf" and save some things */
961 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000962
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 if (u_savesub(n) == OK) {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000964 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 changed();
966#ifdef SYNTAX_HL
967 syn_changed(n); /* recompute syntax hl. for this line */
968#endif
969 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000970
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000971 /* restore curwin/curbuf and a few other things */
972 aucmd_restbuf(&aco);
973 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +0000974
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 update_curbuf(NOT_VALID);
976 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000977 else
978 {
Bram Moolenaar165641d2010-02-17 16:23:09 +0100979 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000980#ifndef __GNUC__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 return Qnil; /* For stop warning */
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 }
984 return str;
985}
986
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000987static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
988{
989 buf_T *buf = get_buf(self);
990
991 if (buf != NULL)
992 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
993 return str;
994}
995
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996static VALUE buffer_delete(VALUE self, VALUE num)
997{
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000998 buf_T *buf = get_buf(self);
999 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001000 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001002 if (n > 0 && n <= buf->b_ml.ml_line_count)
1003 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001004 /* set curwin/curbuf for "buf" and save some things */
1005 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001006
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 if (u_savedel(n, 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001009
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001010 /* Changes to non-active buffers should properly refresh
1011 * SegPhault - 01/09/05 */
1012 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001013
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 changed();
1015 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001016
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001017 /* restore curwin/curbuf and a few other things */
1018 aucmd_restbuf(&aco);
1019 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001020
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 update_curbuf(NOT_VALID);
1022 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001023 else
1024 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001025 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 }
1027 return Qnil;
1028}
1029
1030static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
1031{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001032 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001033 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001034 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001035 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036
Bram Moolenaar83bac8b2010-02-18 15:53:29 +01001037 if (line == NULL) {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001038 rb_raise(rb_eIndexError, "NULL line");
1039 }
1040 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001041 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001042 /* set curwin/curbuf for "buf" and save some things */
1043 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001044
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 if (u_inssub(n + 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001047
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001048 /* Changes to non-active buffers should properly refresh screen
1049 * SegPhault - 12/20/04 */
1050 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001051
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001052 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001054
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001055 /* restore curwin/curbuf and a few other things */
1056 aucmd_restbuf(&aco);
1057 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001058
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 update_curbuf(NOT_VALID);
1060 }
1061 else {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001062 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 }
1064 return str;
1065}
1066
1067static VALUE window_new(win_T *win)
1068{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001069 if (win->w_ruby_ref)
1070 {
1071 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001073 else
1074 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001076 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1078 return obj;
1079 }
1080}
1081
1082static win_T *get_win(VALUE obj)
1083{
1084 win_T *win;
1085
1086 Data_Get_Struct(obj, win_T, win);
1087 if (win == NULL)
1088 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1089 return win;
1090}
1091
1092static VALUE window_s_current()
1093{
1094 return window_new(curwin);
1095}
1096
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001097/*
1098 * Added line manipulation functions
1099 * SegPhault - 03/07/05
1100 */
1101static VALUE line_s_current()
1102{
1103 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1104}
1105
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001106static VALUE set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001107{
1108 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1109}
1110
1111static VALUE current_line_number()
1112{
1113 return INT2FIX((int)curwin->w_cursor.lnum);
1114}
1115
1116
1117
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118static VALUE window_s_count()
1119{
1120#ifdef FEAT_WINDOWS
1121 win_T *w;
1122 int n = 0;
1123
Bram Moolenaarf740b292006-02-16 22:11:02 +00001124 for (w = firstwin; w != NULL; w = w->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 n++;
1126 return INT2NUM(n);
1127#else
1128 return INT2NUM(1);
1129#endif
1130}
1131
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001132static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133{
1134 win_T *w;
1135 int n = NUM2INT(num);
1136
1137#ifndef FEAT_WINDOWS
1138 w = curwin;
1139#else
1140 for (w = firstwin; w != NULL; w = w->w_next, --n)
1141#endif
1142 if (n == 0)
1143 return window_new(w);
1144 return Qnil;
1145}
1146
1147static VALUE window_buffer(VALUE self)
1148{
1149 win_T *win = get_win(self);
1150
1151 return buffer_new(win->w_buffer);
1152}
1153
1154static VALUE window_height(VALUE self)
1155{
1156 win_T *win = get_win(self);
1157
1158 return INT2NUM(win->w_height);
1159}
1160
1161static VALUE window_set_height(VALUE self, VALUE height)
1162{
1163 win_T *win = get_win(self);
1164 win_T *savewin = curwin;
1165
1166 curwin = win;
1167 win_setheight(NUM2INT(height));
1168 curwin = savewin;
1169 return height;
1170}
1171
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001172static VALUE window_width(VALUE self)
1173{
1174 win_T *win = get_win(self);
1175
1176 return INT2NUM(win->w_width);
1177}
1178
1179static VALUE window_set_width(VALUE self, VALUE width)
1180{
1181 win_T *win = get_win(self);
1182 win_T *savewin = curwin;
1183
1184 curwin = win;
1185 win_setwidth(NUM2INT(width));
1186 curwin = savewin;
1187 return width;
1188}
1189
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190static VALUE window_cursor(VALUE self)
1191{
1192 win_T *win = get_win(self);
1193
1194 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1195}
1196
1197static VALUE window_set_cursor(VALUE self, VALUE pos)
1198{
1199 VALUE lnum, col;
1200 win_T *win = get_win(self);
1201
1202 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001203 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001205 lnum = RARRAY_PTR(pos)[0];
1206 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 win->w_cursor.lnum = NUM2LONG(lnum);
1208 win->w_cursor.col = NUM2UINT(col);
1209 check_cursor(); /* put cursor on an existing line */
1210 update_screen(NOT_VALID);
1211 return Qnil;
1212}
1213
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001214static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215{
1216 int i;
1217 VALUE str = rb_str_new("", 0);
1218
1219 for (i = 0; i < argc; i++) {
1220 if (i > 0) rb_str_cat(str, ", ", 2);
1221 rb_str_concat(str, rb_inspect(argv[i]));
1222 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001223 MSG(RSTRING_PTR(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 return Qnil;
1225}
1226
1227static void ruby_io_init(void)
1228{
1229#ifndef DYNAMIC_RUBY
1230 RUBYEXTERN VALUE rb_stdout;
1231#endif
1232
1233 rb_stdout = rb_obj_alloc(rb_cObject);
1234 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
1235 rb_define_global_function("p", f_p, -1);
1236}
1237
1238static void ruby_vim_init(void)
1239{
1240 objtbl = rb_hash_new();
1241 rb_global_variable(&objtbl);
1242
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001243 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
1244 * alias "VIM" for backwards compatiblity. */
1245 mVIM = rb_define_module("Vim");
1246 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1248 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1249 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1250 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1251 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1252 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1253 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1254 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1255 rb_define_module_function(mVIM, "message", vim_message, 1);
1256 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1257 rb_define_module_function(mVIM, "command", vim_command, 1);
1258 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
1259
1260 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1261 rb_eStandardError);
1262 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1263 rb_eStandardError);
1264
1265 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1266 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1267 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1268 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1269 rb_define_method(cBuffer, "name", buffer_name, 0);
1270 rb_define_method(cBuffer, "number", buffer_number, 0);
1271 rb_define_method(cBuffer, "count", buffer_count, 0);
1272 rb_define_method(cBuffer, "length", buffer_count, 0);
1273 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1274 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1275 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1276 rb_define_method(cBuffer, "append", buffer_append, 2);
1277
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001278 /* Added line manipulation functions
1279 * SegPhault - 03/07/05 */
1280 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1281 rb_define_method(cBuffer, "line", line_s_current, 0);
1282 rb_define_method(cBuffer, "line=", set_current_line, 1);
1283
1284
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1286 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1287 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1288 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1289 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1290 rb_define_method(cVimWindow, "height", window_height, 0);
1291 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001292 rb_define_method(cVimWindow, "width", window_width, 0);
1293 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1295 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1296
1297 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1298 rb_define_virtual_variable("$curwin", window_s_current, 0);
1299}