blob: dd1ba47009d5cd54f83a13a53ad9dc9ddfb8511a [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)
42# define rb_cNilClass (*dll_rb_cNilClass)
43# define rb_cSymbol (*dll_rb_cSymbol)
44# define rb_cTrueClass (*dll_rb_cTrueClass)
45# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
46/*
Bram Moolenaar42d57f02010-03-10 12:47:00 +010047 * On ver 1.8, all Ruby functions are exported with "__declspec(dllimport)"
48 * in ruby.h. But it causes trouble for these variables, because it is
Bram Moolenaar071d4272004-06-13 20:20:40 +000049 * defined in this file. When defined this RUBY_EXPORT it modified to
50 * "extern" and be able to avoid this problem.
51 */
52# define RUBY_EXPORT
53# endif
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020054
Bram Moolenaar2d73ff42010-10-27 17:40:59 +020055#if !(defined(WIN32) || defined(_WIN64))
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020056# include <dlfcn.h>
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020057# define HINSTANCE void*
58# define RUBY_PROC void*
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020059# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
60# define symbol_from_dll dlsym
61# define close_dll dlclose
62#else
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020063# define RUBY_PROC FARPROC
Bram Moolenaarebbcb822010-10-23 14:02:54 +020064# define load_dll vimLoadLib
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020065# define symbol_from_dll GetProcAddress
66# define close_dll FreeLibrary
Bram Moolenaar071d4272004-06-13 20:20:40 +000067#endif
68
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020069#endif /* ifdef DYNAMIC_RUBY */
70
Bram Moolenaar0b69c732010-02-17 15:11:50 +010071/* suggested by Ariya Mizutani */
72#if (_MSC_VER == 1200)
73# undef _WIN32_WINNT
74#endif
75
Bram Moolenaar639a2552010-03-17 18:15:23 +010076#if (defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
77 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
78# define RUBY19_OR_LATER 1
79#endif
80
Bram Moolenaar42d57f02010-03-10 12:47:00 +010081#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
82/* Ruby 1.9 defines a number of static functions which use rb_num2long and
83 * rb_int2big */
84# define rb_num2long rb_num2long_stub
85# define rb_int2big rb_int2big_stub
86#endif
87
Bram Moolenaar071d4272004-06-13 20:20:40 +000088#include <ruby.h>
Bram Moolenaar639a2552010-03-17 18:15:23 +010089#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +010090# include <ruby/encoding.h>
91#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000092
Bram Moolenaar7a8ef142010-12-24 13:39:35 +010093#undef off_t /* ruby defines off_t as _int64, Mingw uses long */
Bram Moolenaar071d4272004-06-13 20:20:40 +000094#undef EXTERN
95#undef _
96
97/* T_DATA defined both by Ruby and Mac header files, hack around it... */
Bram Moolenaarcb635362007-05-12 13:02:42 +000098#if defined(MACOS_X_UNIX) || defined(macintosh)
Bram Moolenaar071d4272004-06-13 20:20:40 +000099# define __OPENTRANSPORT__
100# define __OPENTRANSPORTPROTOCOL__
101# define __OPENTRANSPORTPROVIDERS__
102#endif
103
Bram Moolenaar165641d2010-02-17 16:23:09 +0100104/*
105 * Backward compatiblity for Ruby 1.8 and earlier.
106 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
107 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
108 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
109 */
110#ifndef StringValuePtr
111# define StringValuePtr(s) STR2CSTR(s)
112#endif
113#ifndef RARRAY_LEN
114# define RARRAY_LEN(s) RARRAY(s)->len
115#endif
116#ifndef RARRAY_PTR
117# define RARRAY_PTR(s) RARRAY(s)->ptr
118#endif
119#ifndef RSTRING_LEN
120# define RSTRING_LEN(s) RSTRING(s)->len
121#endif
122#ifndef RSTRING_PTR
123# define RSTRING_PTR(s) RSTRING(s)->ptr
124#endif
125
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#include "vim.h"
127#include "version.h"
128
129#if defined(PROTO) && !defined(FEAT_RUBY)
130/* Define these to be able to generate the function prototypes. */
131# define VALUE int
132# define RUBY_DATA_FUNC int
133#endif
134
135static int ruby_initialized = 0;
136static VALUE objtbl;
137
138static VALUE mVIM;
139static VALUE cBuffer;
140static VALUE cVimWindow;
141static VALUE eDeletedBufferError;
142static VALUE eDeletedWindowError;
143
144static int ensure_ruby_initialized(void);
145static void error_print(int);
146static void ruby_io_init(void);
147static void ruby_vim_init(void);
148
149#if defined(DYNAMIC_RUBY) || defined(PROTO)
150#ifdef PROTO
151# define HINSTANCE int /* for generating prototypes */
152#endif
153
154/*
155 * Wrapper defines
156 */
157#define rb_assoc_new dll_rb_assoc_new
158#define rb_cObject (*dll_rb_cObject)
159#define rb_check_type dll_rb_check_type
160#define rb_class_path dll_rb_class_path
161#define rb_data_object_alloc dll_rb_data_object_alloc
162#define rb_define_class_under dll_rb_define_class_under
163#define rb_define_const dll_rb_define_const
164#define rb_define_global_function dll_rb_define_global_function
165#define rb_define_method dll_rb_define_method
166#define rb_define_module dll_rb_define_module
167#define rb_define_module_function dll_rb_define_module_function
168#define rb_define_singleton_method dll_rb_define_singleton_method
169#define rb_define_virtual_variable dll_rb_define_virtual_variable
170#define rb_stdout (*dll_rb_stdout)
171#define rb_eArgError (*dll_rb_eArgError)
172#define rb_eIndexError (*dll_rb_eIndexError)
173#define rb_eRuntimeError (*dll_rb_eRuntimeError)
174#define rb_eStandardError (*dll_rb_eStandardError)
175#define rb_eval_string_protect dll_rb_eval_string_protect
176#define rb_global_variable dll_rb_global_variable
177#define rb_hash_aset dll_rb_hash_aset
178#define rb_hash_new dll_rb_hash_new
179#define rb_inspect dll_rb_inspect
180#define rb_int2inum dll_rb_int2inum
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200181#define rb_fix2int dll_rb_fix2int
182#define rb_num2int dll_rb_num2int
183#define rb_num2uint dll_rb_num2uint
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184#define rb_lastline_get dll_rb_lastline_get
185#define rb_lastline_set dll_rb_lastline_set
186#define rb_load_protect dll_rb_load_protect
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200187#ifndef RUBY19_OR_LATER
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188#define rb_num2long dll_rb_num2long
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190#define rb_num2ulong dll_rb_num2ulong
191#define rb_obj_alloc dll_rb_obj_alloc
192#define rb_obj_as_string dll_rb_obj_as_string
193#define rb_obj_id dll_rb_obj_id
194#define rb_raise dll_rb_raise
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195#define rb_str_cat dll_rb_str_cat
196#define rb_str_concat dll_rb_str_concat
197#define rb_str_new dll_rb_str_new
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200198#ifdef rb_str_new2
199/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
200# define need_rb_str_new_cstr 1
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200201/* Ruby's headers #define rb_str_new_cstr to make use of GCC's
202 * __builtin_constant_p extension. */
203# undef rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200204# define rb_str_new_cstr dll_rb_str_new_cstr
205#else
Bram Moolenaar218116c2010-05-20 21:46:00 +0200206# define rb_str_new2 dll_rb_str_new2
207#endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100208#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200209# define rb_string_value dll_rb_string_value
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100210# define rb_string_value_ptr dll_rb_string_value_ptr
211# define rb_float_new dll_rb_float_new
212# define rb_ary_new dll_rb_ary_new
213# define rb_ary_push dll_rb_ary_push
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200214#else
215# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100216#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100217#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100218# define rb_errinfo dll_rb_errinfo
219#else
220# define ruby_errinfo (*dll_ruby_errinfo)
221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222#define ruby_init dll_ruby_init
223#define ruby_init_loadpath dll_ruby_init_loadpath
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200224#ifdef WIN3264
225# define NtInitialize dll_NtInitialize
226# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
227# define rb_w32_snprintf dll_rb_w32_snprintf
228# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229#endif
230
Bram Moolenaar639a2552010-03-17 18:15:23 +0100231#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100232# define ruby_script dll_ruby_script
233# define rb_enc_find_index dll_rb_enc_find_index
234# define rb_enc_find dll_rb_enc_find
235# define rb_enc_str_new dll_rb_enc_str_new
236# define rb_sprintf dll_rb_sprintf
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100237# define rb_require dll_rb_require
Bram Moolenaar639a2552010-03-17 18:15:23 +0100238# define ruby_init_stack dll_ruby_init_stack
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100239# define ruby_process_options dll_ruby_process_options
Bram Moolenaar165641d2010-02-17 16:23:09 +0100240#endif
241
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242/*
243 * Pointers for dynamic link
244 */
245static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200246VALUE *dll_rb_cFalseClass;
247VALUE *dll_rb_cFixnum;
248VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249static VALUE *dll_rb_cObject;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200250VALUE *dll_rb_cSymbol;
251VALUE *dll_rb_cTrueClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252static void (*dll_rb_check_type) (VALUE,int);
253static VALUE (*dll_rb_class_path) (VALUE);
254static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
255static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
256static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
257static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
258static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
259static VALUE (*dll_rb_define_module) (const char*);
260static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
261static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
262static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
263static VALUE *dll_rb_stdout;
264static VALUE *dll_rb_eArgError;
265static VALUE *dll_rb_eIndexError;
266static VALUE *dll_rb_eRuntimeError;
267static VALUE *dll_rb_eStandardError;
268static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
269static void (*dll_rb_global_variable) (VALUE*);
270static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
271static VALUE (*dll_rb_hash_new) (void);
272static VALUE (*dll_rb_inspect) (VALUE);
273static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200274static long (*dll_rb_fix2int) (VALUE);
275static long (*dll_rb_num2int) (VALUE);
276static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277static VALUE (*dll_rb_lastline_get) (void);
278static void (*dll_rb_lastline_set) (VALUE);
279static void (*dll_rb_load_protect) (VALUE, int, int*);
280static long (*dll_rb_num2long) (VALUE);
281static unsigned long (*dll_rb_num2ulong) (VALUE);
282static VALUE (*dll_rb_obj_alloc) (VALUE);
283static VALUE (*dll_rb_obj_as_string) (VALUE);
284static VALUE (*dll_rb_obj_id) (VALUE);
285static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200286#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
287static VALUE (*dll_rb_string_value) (volatile VALUE*);
288#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
292static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
293static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200294#ifdef need_rb_str_new_cstr
295/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
296static VALUE (*dll_rb_str_new_cstr) (const char*);
297#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200299#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100300#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100301static VALUE (*dll_rb_errinfo) (void);
302#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303static VALUE *dll_ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +0100304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305static void (*dll_ruby_init) (void);
306static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200307#ifdef WIN3264
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100308static void (*dll_NtInitialize) (int*, char***);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200309# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
310static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
311# endif
312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100314static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
315static VALUE (*dll_rb_float_new) (double);
316static VALUE (*dll_rb_ary_new) (void);
317static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
318#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100319#ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100320static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322
Bram Moolenaar639a2552010-03-17 18:15:23 +0100323#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100324static void (*dll_ruby_script) (const char*);
325static int (*dll_rb_enc_find_index) (const char*);
326static rb_encoding* (*dll_rb_enc_find) (const char*);
327static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
328static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100329static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar639a2552010-03-17 18:15:23 +0100330static void (*ruby_init_stack)(VALUE*);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100331static void* (*ruby_process_options)(int, char**);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100332#endif
333
Bram Moolenaar639a2552010-03-17 18:15:23 +0100334#ifdef RUBY19_OR_LATER
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200335SIGNED_VALUE rb_num2long_stub(VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100336{
337 return dll_rb_num2long(x);
338}
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200339VALUE rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100340{
341 return dll_rb_int2big(x);
342}
343#endif
344
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200345static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346
347/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000348 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350static struct
351{
352 char *name;
353 RUBY_PROC *ptr;
354} ruby_funcname_table[] =
355{
356 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
357 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
358 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
359 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
360 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
361 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
362 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
363 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
364 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
365 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
366 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
367 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
368 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
369 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
370 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
371 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
372 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
373 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
374 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
375 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
376 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
377 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
378 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
379 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
380 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
381 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
382 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
383 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
384 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200385 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
386 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
387 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
389 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
390 {"rb_load_protect", (RUBY_PROC*)&dll_rb_load_protect},
391 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
392 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
393 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
394 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
395 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
396 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200397#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
398 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
399#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
403 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
404 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200405#ifdef need_rb_str_new_cstr
406 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
407#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200409#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100410#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100411 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
412#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
416 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200417#ifdef WIN3264
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100418 {
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200419# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER < 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100420 "NtInitialize",
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200421# else
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100422 "ruby_sysinit",
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200423# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100424 (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200425# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200427# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428#endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100429#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
430 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
431 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
432 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
433 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
434#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100435#ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100436 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100437 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
438 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
439 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
440 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
441 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100442 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar639a2552010-03-17 18:15:23 +0100443 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100444 {"ruby_process_options", (RUBY_PROC*)&dll_ruby_process_options},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 {"", NULL},
447};
448
449/*
450 * Free ruby.dll
451 */
452 static void
453end_dynamic_ruby()
454{
455 if (hinstRuby)
456 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200457 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200458 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 }
460}
461
462/*
463 * Load library and get all pointers.
464 * Parameter 'libname' provides name of DLL.
465 * Return OK or FAIL.
466 */
467 static int
468ruby_runtime_link_init(char *libname, int verbose)
469{
470 int i;
471
472 if (hinstRuby)
473 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200474 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 if (!hinstRuby)
476 {
477 if (verbose)
478 EMSG2(_(e_loadlib), libname);
479 return FAIL;
480 }
481
482 for (i = 0; ruby_funcname_table[i].ptr; ++i)
483 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200484 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 ruby_funcname_table[i].name)))
486 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200487 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200488 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 if (verbose)
490 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
491 return FAIL;
492 }
493 }
494 return OK;
495}
496
497/*
498 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
499 * else FALSE.
500 */
501 int
502ruby_enabled(verbose)
503 int verbose;
504{
505 return ruby_runtime_link_init(DYNAMIC_RUBY_DLL, verbose) == OK;
506}
507#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
508
509 void
510ruby_end()
511{
512#ifdef DYNAMIC_RUBY
513 end_dynamic_ruby();
514#endif
515}
516
517void ex_ruby(exarg_T *eap)
518{
519 int state;
520 char *script = NULL;
521
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000522 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 if (!eap->skip && ensure_ruby_initialized())
524 {
525 if (script == NULL)
526 rb_eval_string_protect((char *)eap->arg, &state);
527 else
528 rb_eval_string_protect(script, &state);
529 if (state)
530 error_print(state);
531 }
532 vim_free(script);
533}
534
Bram Moolenaar165641d2010-02-17 16:23:09 +0100535/*
536 * In Ruby 1.9 or later, ruby String object has encoding.
537 * conversion buffer string of vim to ruby String object using
538 * VIM encoding option.
539 */
540 static VALUE
541vim_str2rb_enc_str(const char *s)
542{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100543#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100544 int isnum;
545 long lval;
546 char_u *sval;
547 rb_encoding *enc;
548
549 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
550 if (isnum == 0)
551 {
552 enc = rb_enc_find((char *)sval);
553 vim_free(sval);
554 if (enc) {
555 return rb_enc_str_new(s, strlen(s), enc);
556 }
557 }
558#endif
559 return rb_str_new2(s);
560}
561
562 static VALUE
563eval_enc_string_protect(const char *str, int *state)
564{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100565#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100566 int isnum;
567 long lval;
568 char_u *sval;
569 rb_encoding *enc;
570 VALUE v;
571
572 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
573 if (isnum == 0)
574 {
575 enc = rb_enc_find((char *)sval);
576 vim_free(sval);
577 if (enc)
578 {
579 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
580 return rb_eval_string_protect(StringValuePtr(v), state);
581 }
582 }
583#endif
584 return rb_eval_string_protect(str, state);
585}
586
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587void ex_rubydo(exarg_T *eap)
588{
589 int state;
590 linenr_T i;
591
592 if (ensure_ruby_initialized())
593 {
594 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
595 return;
596 for (i = eap->line1; i <= eap->line2; i++) {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100597 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100599 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100601 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 if (state) {
603 error_print(state);
604 break;
605 }
606 line = rb_lastline_get();
607 if (!NIL_P(line)) {
608 if (TYPE(line) != T_STRING) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000609 EMSG(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 return;
611 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100612 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 changed();
614#ifdef SYNTAX_HL
615 syn_changed(i); /* recompute syntax hl. for this line */
616#endif
617 }
618 }
619 check_cursor();
620 update_curbuf(NOT_VALID);
621 }
622}
623
624void ex_rubyfile(exarg_T *eap)
625{
626 int state;
627
628 if (ensure_ruby_initialized())
629 {
630 rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
631 if (state) error_print(state);
632 }
633}
634
635void ruby_buffer_free(buf_T *buf)
636{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000637 if (buf->b_ruby_ref)
638 {
639 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
640 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 }
642}
643
644void ruby_window_free(win_T *win)
645{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000646 if (win->w_ruby_ref)
647 {
648 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
649 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 }
651}
652
653static int ensure_ruby_initialized(void)
654{
655 if (!ruby_initialized)
656 {
657#ifdef DYNAMIC_RUBY
658 if (ruby_enabled(TRUE))
659 {
660#endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100661#ifdef _WIN32
662 /* suggested by Ariya Mizutani */
663 int argc = 1;
664 char *argv[] = {"gvim.exe"};
665 NtInitialize(&argc, &argv);
666#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200667 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100668#ifdef RUBY19_OR_LATER
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200669 RUBY_INIT_STACK;
Bram Moolenaar165641d2010-02-17 16:23:09 +0100670#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200671 ruby_init();
672 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100673#ifdef RUBY19_OR_LATER
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100674 {
675 int dummy_argc = 2;
676 char *dummy_argv[] = {"vim-ruby", "-e0"};
677 ruby_process_options(dummy_argc, dummy_argv);
678 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100679 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100680#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100682#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100683 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 ruby_vim_init();
685 ruby_initialized = 1;
686#ifdef DYNAMIC_RUBY
687 }
688 else
689 {
690 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
691 return 0;
692 }
693#endif
694 }
695 return ruby_initialized;
696}
697
698static void error_print(int state)
699{
700#ifndef DYNAMIC_RUBY
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100701#if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
702 && !(defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 RUBYEXTERN VALUE ruby_errinfo;
704#endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 VALUE eclass;
707 VALUE einfo;
708 char buff[BUFSIZ];
709
710#define TAG_RETURN 0x1
711#define TAG_BREAK 0x2
712#define TAG_NEXT 0x3
713#define TAG_RETRY 0x4
714#define TAG_REDO 0x5
715#define TAG_RAISE 0x6
716#define TAG_THROW 0x7
717#define TAG_FATAL 0x8
718#define TAG_MASK 0xf
719
720 switch (state) {
721 case TAG_RETURN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000722 EMSG(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 break;
724 case TAG_NEXT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000725 EMSG(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 break;
727 case TAG_BREAK:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000728 EMSG(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 break;
730 case TAG_REDO:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000731 EMSG(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 break;
733 case TAG_RETRY:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000734 EMSG(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 break;
736 case TAG_RAISE:
737 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +0100738#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100739 eclass = CLASS_OF(rb_errinfo());
740 einfo = rb_obj_as_string(rb_errinfo());
741#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 eclass = CLASS_OF(ruby_errinfo);
743 einfo = rb_obj_as_string(ruby_errinfo);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100744#endif
745 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000746 EMSG(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 }
748 else {
749 VALUE epath;
750 char *p;
751
752 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000753 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar165641d2010-02-17 16:23:09 +0100754 RSTRING_PTR(epath), RSTRING_PTR(einfo));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 p = strchr(buff, '\n');
756 if (p) *p = '\0';
757 EMSG(buff);
758 }
759 break;
760 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000761 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 EMSG(buff);
763 break;
764 }
765}
766
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000767static VALUE vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768{
769 char *buff, *p;
770
771 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +0200772 if (RSTRING_LEN(str) > 0)
773 {
774 /* Only do this when the string isn't empty, alloc(0) causes trouble. */
775 buff = ALLOCA_N(char, RSTRING_LEN(str));
776 strcpy(buff, RSTRING_PTR(str));
777 p = strchr(buff, '\n');
778 if (p) *p = '\0';
779 MSG(buff);
780 }
781 else
782 {
783 MSG("");
784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 return Qnil;
786}
787
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000788static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100790 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 update_screen(NOT_VALID);
792 return Qnil;
793}
794
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000795static VALUE vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100797 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 return Qnil;
799}
800
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100801#ifdef FEAT_EVAL
802static VALUE vim_to_ruby(typval_T *tv)
803{
804 VALUE result = Qnil;
805
806 if (tv->v_type == VAR_STRING)
807 {
Bram Moolenaar94127e42010-03-19 23:08:48 +0100808 result = rb_str_new2(tv->vval.v_string == NULL
809 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100810 }
811 else if (tv->v_type == VAR_NUMBER)
812 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100813 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100814 }
815# ifdef FEAT_FLOAT
816 else if (tv->v_type == VAR_FLOAT)
817 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100818 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100819 }
820# endif
821 else if (tv->v_type == VAR_LIST)
822 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100823 list_T *list = tv->vval.v_list;
824 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100825
Bram Moolenaar639a2552010-03-17 18:15:23 +0100826 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100827
Bram Moolenaar639a2552010-03-17 18:15:23 +0100828 if (list != NULL)
829 {
830 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
831 {
832 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
833 }
834 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100835 }
836 else if (tv->v_type == VAR_DICT)
837 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100838 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100839
Bram Moolenaar639a2552010-03-17 18:15:23 +0100840 if (tv->vval.v_dict != NULL)
841 {
842 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
843 long_u todo = ht->ht_used;
844 hashitem_T *hi;
845 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100846
Bram Moolenaar639a2552010-03-17 18:15:23 +0100847 for (hi = ht->ht_array; todo > 0; ++hi)
848 {
849 if (!HASHITEM_EMPTY(hi))
850 {
851 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100852
Bram Moolenaar639a2552010-03-17 18:15:23 +0100853 di = dict_lookup(hi);
854 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100855 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +0100856 }
857 }
858 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100859 } /* else return Qnil; */
860
861 return result;
862}
863#endif
864
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000865static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866{
867#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100868 typval_T *tv;
869 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100871 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
872 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100874 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100876 result = vim_to_ruby(tv);
877
878 free_tv(tv);
879
880 return result;
881#else
882 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884}
885
886static VALUE buffer_new(buf_T *buf)
887{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000888 if (buf->b_ruby_ref)
889 {
890 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 }
Bram Moolenaare344bea2005-09-01 20:46:49 +0000892 else
893 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000895 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
897 return obj;
898 }
899}
900
901static buf_T *get_buf(VALUE obj)
902{
903 buf_T *buf;
904
905 Data_Get_Struct(obj, buf_T, buf);
906 if (buf == NULL)
907 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
908 return buf;
909}
910
911static VALUE buffer_s_current()
912{
913 return buffer_new(curbuf);
914}
915
916static VALUE buffer_s_count()
917{
918 buf_T *b;
919 int n = 0;
920
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000921 for (b = firstbuf; b != NULL; b = b->b_next)
922 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000923 /* Deleted buffers should not be counted
924 * SegPhault - 01/07/05 */
925 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000926 n++;
927 }
928
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 return INT2NUM(n);
930}
931
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000932static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933{
934 buf_T *b;
935 int n = NUM2INT(num);
936
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000937 for (b = firstbuf; b != NULL; b = b->b_next)
938 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000939 /* Deleted buffers should not be counted
940 * SegPhault - 01/07/05 */
941 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000942 continue;
943
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000944 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000946
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000947 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 }
949 return Qnil;
950}
951
952static VALUE buffer_name(VALUE self)
953{
954 buf_T *buf = get_buf(self);
955
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000956 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957}
958
959static VALUE buffer_number(VALUE self)
960{
961 buf_T *buf = get_buf(self);
962
963 return INT2NUM(buf->b_fnum);
964}
965
966static VALUE buffer_count(VALUE self)
967{
968 buf_T *buf = get_buf(self);
969
970 return INT2NUM(buf->b_ml.ml_line_count);
971}
972
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000973static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974{
Bram Moolenaar3c531602010-11-16 14:46:19 +0100975 if (n <= 0 || n > buf->b_ml.ml_line_count)
976 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
977 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978}
979
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000980static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981{
982 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000983
984 if (buf != NULL)
985 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
986 return Qnil; /* For stop warning */
987}
988
989static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
990{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100991 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000992 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000994 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
995 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000996 /* set curwin/curbuf for "buf" and save some things */
997 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000998
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 if (u_savesub(n) == OK) {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001000 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 changed();
1002#ifdef SYNTAX_HL
1003 syn_changed(n); /* recompute syntax hl. for this line */
1004#endif
1005 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001006
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001007 /* restore curwin/curbuf and a few other things */
1008 aucmd_restbuf(&aco);
1009 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001010
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 update_curbuf(NOT_VALID);
1012 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001013 else
1014 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001015 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 }
1017 return str;
1018}
1019
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001020static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
1021{
1022 buf_T *buf = get_buf(self);
1023
1024 if (buf != NULL)
1025 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1026 return str;
1027}
1028
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029static VALUE buffer_delete(VALUE self, VALUE num)
1030{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001031 buf_T *buf = get_buf(self);
1032 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001033 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001035 if (n > 0 && n <= buf->b_ml.ml_line_count)
1036 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001037 /* set curwin/curbuf for "buf" and save some things */
1038 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001039
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 if (u_savedel(n, 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001042
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001043 /* Changes to non-active buffers should properly refresh
1044 * SegPhault - 01/09/05 */
1045 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001046
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 changed();
1048 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001049
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001050 /* restore curwin/curbuf and a few other things */
1051 aucmd_restbuf(&aco);
1052 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001053
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 update_curbuf(NOT_VALID);
1055 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001056 else
1057 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001058 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 }
1060 return Qnil;
1061}
1062
1063static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
1064{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001065 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001066 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001067 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001068 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069
Bram Moolenaar3c531602010-11-16 14:46:19 +01001070 if (line == NULL)
1071 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001072 rb_raise(rb_eIndexError, "NULL line");
1073 }
1074 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001075 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001076 /* set curwin/curbuf for "buf" and save some things */
1077 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001078
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 if (u_inssub(n + 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001081
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001082 /* Changes to non-active buffers should properly refresh screen
1083 * SegPhault - 12/20/04 */
1084 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001085
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001086 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001088
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001089 /* restore curwin/curbuf and a few other things */
1090 aucmd_restbuf(&aco);
1091 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001092
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 update_curbuf(NOT_VALID);
1094 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001095 else
1096 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001097 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 }
1099 return str;
1100}
1101
1102static VALUE window_new(win_T *win)
1103{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001104 if (win->w_ruby_ref)
1105 {
1106 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001108 else
1109 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001111 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1113 return obj;
1114 }
1115}
1116
1117static win_T *get_win(VALUE obj)
1118{
1119 win_T *win;
1120
1121 Data_Get_Struct(obj, win_T, win);
1122 if (win == NULL)
1123 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1124 return win;
1125}
1126
1127static VALUE window_s_current()
1128{
1129 return window_new(curwin);
1130}
1131
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001132/*
1133 * Added line manipulation functions
1134 * SegPhault - 03/07/05
1135 */
1136static VALUE line_s_current()
1137{
1138 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1139}
1140
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001141static VALUE set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001142{
1143 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1144}
1145
1146static VALUE current_line_number()
1147{
1148 return INT2FIX((int)curwin->w_cursor.lnum);
1149}
1150
1151
1152
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153static VALUE window_s_count()
1154{
1155#ifdef FEAT_WINDOWS
1156 win_T *w;
1157 int n = 0;
1158
Bram Moolenaarf740b292006-02-16 22:11:02 +00001159 for (w = firstwin; w != NULL; w = w->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 n++;
1161 return INT2NUM(n);
1162#else
1163 return INT2NUM(1);
1164#endif
1165}
1166
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001167static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168{
1169 win_T *w;
1170 int n = NUM2INT(num);
1171
1172#ifndef FEAT_WINDOWS
1173 w = curwin;
1174#else
1175 for (w = firstwin; w != NULL; w = w->w_next, --n)
1176#endif
1177 if (n == 0)
1178 return window_new(w);
1179 return Qnil;
1180}
1181
1182static VALUE window_buffer(VALUE self)
1183{
1184 win_T *win = get_win(self);
1185
1186 return buffer_new(win->w_buffer);
1187}
1188
1189static VALUE window_height(VALUE self)
1190{
1191 win_T *win = get_win(self);
1192
1193 return INT2NUM(win->w_height);
1194}
1195
1196static VALUE window_set_height(VALUE self, VALUE height)
1197{
1198 win_T *win = get_win(self);
1199 win_T *savewin = curwin;
1200
1201 curwin = win;
1202 win_setheight(NUM2INT(height));
1203 curwin = savewin;
1204 return height;
1205}
1206
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001207static VALUE window_width(VALUE self)
1208{
1209 win_T *win = get_win(self);
1210
1211 return INT2NUM(win->w_width);
1212}
1213
1214static VALUE window_set_width(VALUE self, VALUE width)
1215{
1216 win_T *win = get_win(self);
1217 win_T *savewin = curwin;
1218
1219 curwin = win;
1220 win_setwidth(NUM2INT(width));
1221 curwin = savewin;
1222 return width;
1223}
1224
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225static VALUE window_cursor(VALUE self)
1226{
1227 win_T *win = get_win(self);
1228
1229 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1230}
1231
1232static VALUE window_set_cursor(VALUE self, VALUE pos)
1233{
1234 VALUE lnum, col;
1235 win_T *win = get_win(self);
1236
1237 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001238 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001240 lnum = RARRAY_PTR(pos)[0];
1241 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 win->w_cursor.lnum = NUM2LONG(lnum);
1243 win->w_cursor.col = NUM2UINT(col);
1244 check_cursor(); /* put cursor on an existing line */
1245 update_screen(NOT_VALID);
1246 return Qnil;
1247}
1248
Bram Moolenaar6217cdc2012-04-25 12:28:09 +02001249static VALUE f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001250{
1251 return Qnil;
1252}
1253
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001254static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255{
1256 int i;
1257 VALUE str = rb_str_new("", 0);
1258
1259 for (i = 0; i < argc; i++) {
1260 if (i > 0) rb_str_cat(str, ", ", 2);
1261 rb_str_concat(str, rb_inspect(argv[i]));
1262 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001263 MSG(RSTRING_PTR(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 return Qnil;
1265}
1266
1267static void ruby_io_init(void)
1268{
1269#ifndef DYNAMIC_RUBY
1270 RUBYEXTERN VALUE rb_stdout;
1271#endif
1272
1273 rb_stdout = rb_obj_alloc(rb_cObject);
1274 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001275 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 rb_define_global_function("p", f_p, -1);
1277}
1278
1279static void ruby_vim_init(void)
1280{
1281 objtbl = rb_hash_new();
1282 rb_global_variable(&objtbl);
1283
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001284 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
1285 * alias "VIM" for backwards compatiblity. */
1286 mVIM = rb_define_module("Vim");
1287 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1289 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1290 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1291 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1292 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1293 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1294 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1295 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1296 rb_define_module_function(mVIM, "message", vim_message, 1);
1297 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1298 rb_define_module_function(mVIM, "command", vim_command, 1);
1299 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
1300
1301 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1302 rb_eStandardError);
1303 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1304 rb_eStandardError);
1305
1306 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1307 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1308 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1309 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1310 rb_define_method(cBuffer, "name", buffer_name, 0);
1311 rb_define_method(cBuffer, "number", buffer_number, 0);
1312 rb_define_method(cBuffer, "count", buffer_count, 0);
1313 rb_define_method(cBuffer, "length", buffer_count, 0);
1314 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1315 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1316 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1317 rb_define_method(cBuffer, "append", buffer_append, 2);
1318
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001319 /* Added line manipulation functions
1320 * SegPhault - 03/07/05 */
1321 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1322 rb_define_method(cBuffer, "line", line_s_current, 0);
1323 rb_define_method(cBuffer, "line=", set_current_line, 1);
1324
1325
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1327 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1328 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1329 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1330 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1331 rb_define_method(cVimWindow, "height", window_height, 0);
1332 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001333 rb_define_method(cVimWindow, "width", window_width, 0);
1334 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1336 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1337
1338 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1339 rb_define_virtual_variable("$curwin", window_s_current, 0);
1340}