blob: 4ae2d90d122e3da99a4ed22850dc5d3496642f65 [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 Moolenaarb213da02012-10-03 18:06:59 +0200181#if SIZEOF_INT < SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200182#define rb_fix2int dll_rb_fix2int
183#define rb_num2int dll_rb_num2int
184#define rb_num2uint dll_rb_num2uint
Bram Moolenaarb213da02012-10-03 18:06:59 +0200185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186#define rb_lastline_get dll_rb_lastline_get
187#define rb_lastline_set dll_rb_lastline_set
188#define rb_load_protect dll_rb_load_protect
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200189#ifndef RUBY19_OR_LATER
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190#define rb_num2long dll_rb_num2long
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192#define rb_num2ulong dll_rb_num2ulong
193#define rb_obj_alloc dll_rb_obj_alloc
194#define rb_obj_as_string dll_rb_obj_as_string
195#define rb_obj_id dll_rb_obj_id
196#define rb_raise dll_rb_raise
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197#define rb_str_cat dll_rb_str_cat
198#define rb_str_concat dll_rb_str_concat
199#define rb_str_new dll_rb_str_new
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200200#ifdef rb_str_new2
201/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
202# define need_rb_str_new_cstr 1
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200203/* Ruby's headers #define rb_str_new_cstr to make use of GCC's
204 * __builtin_constant_p extension. */
205# undef rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200206# define rb_str_new_cstr dll_rb_str_new_cstr
207#else
Bram Moolenaar218116c2010-05-20 21:46:00 +0200208# define rb_str_new2 dll_rb_str_new2
209#endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100210#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200211# define rb_string_value dll_rb_string_value
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100212# define rb_string_value_ptr dll_rb_string_value_ptr
213# define rb_float_new dll_rb_float_new
214# define rb_ary_new dll_rb_ary_new
215# define rb_ary_push dll_rb_ary_push
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200216#else
217# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100218#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100219#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100220# define rb_errinfo dll_rb_errinfo
221#else
222# define ruby_errinfo (*dll_ruby_errinfo)
223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#define ruby_init dll_ruby_init
225#define ruby_init_loadpath dll_ruby_init_loadpath
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200226#ifdef WIN3264
227# define NtInitialize dll_NtInitialize
228# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
229# define rb_w32_snprintf dll_rb_w32_snprintf
230# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231#endif
232
Bram Moolenaar639a2552010-03-17 18:15:23 +0100233#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100234# define ruby_script dll_ruby_script
235# define rb_enc_find_index dll_rb_enc_find_index
236# define rb_enc_find dll_rb_enc_find
237# define rb_enc_str_new dll_rb_enc_str_new
238# define rb_sprintf dll_rb_sprintf
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100239# define rb_require dll_rb_require
Bram Moolenaar639a2552010-03-17 18:15:23 +0100240# define ruby_init_stack dll_ruby_init_stack
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100241# define ruby_process_options dll_ruby_process_options
Bram Moolenaar165641d2010-02-17 16:23:09 +0100242#endif
243
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244/*
245 * Pointers for dynamic link
246 */
247static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200248VALUE *dll_rb_cFalseClass;
249VALUE *dll_rb_cFixnum;
250VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251static VALUE *dll_rb_cObject;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200252VALUE *dll_rb_cSymbol;
253VALUE *dll_rb_cTrueClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254static void (*dll_rb_check_type) (VALUE,int);
255static VALUE (*dll_rb_class_path) (VALUE);
256static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
257static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
258static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
259static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
260static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
261static VALUE (*dll_rb_define_module) (const char*);
262static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
263static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
264static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
265static VALUE *dll_rb_stdout;
266static VALUE *dll_rb_eArgError;
267static VALUE *dll_rb_eIndexError;
268static VALUE *dll_rb_eRuntimeError;
269static VALUE *dll_rb_eStandardError;
270static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
271static void (*dll_rb_global_variable) (VALUE*);
272static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
273static VALUE (*dll_rb_hash_new) (void);
274static VALUE (*dll_rb_inspect) (VALUE);
275static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaarb213da02012-10-03 18:06:59 +0200276#if SIZEOF_INT < SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200277static long (*dll_rb_fix2int) (VALUE);
278static long (*dll_rb_num2int) (VALUE);
279static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaarb213da02012-10-03 18:06:59 +0200280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281static VALUE (*dll_rb_lastline_get) (void);
282static void (*dll_rb_lastline_set) (VALUE);
283static void (*dll_rb_load_protect) (VALUE, int, int*);
284static long (*dll_rb_num2long) (VALUE);
285static unsigned long (*dll_rb_num2ulong) (VALUE);
286static VALUE (*dll_rb_obj_alloc) (VALUE);
287static VALUE (*dll_rb_obj_as_string) (VALUE);
288static VALUE (*dll_rb_obj_id) (VALUE);
289static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200290#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
291static VALUE (*dll_rb_string_value) (volatile VALUE*);
292#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
296static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
297static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200298#ifdef need_rb_str_new_cstr
299/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
300static VALUE (*dll_rb_str_new_cstr) (const char*);
301#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200303#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100304#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100305static VALUE (*dll_rb_errinfo) (void);
306#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307static VALUE *dll_ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +0100308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309static void (*dll_ruby_init) (void);
310static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200311#ifdef WIN3264
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100312static void (*dll_NtInitialize) (int*, char***);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200313# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
314static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
315# endif
316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100318static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
319static VALUE (*dll_rb_float_new) (double);
320static VALUE (*dll_rb_ary_new) (void);
321static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
322#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100323#ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100324static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326
Bram Moolenaar639a2552010-03-17 18:15:23 +0100327#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100328static void (*dll_ruby_script) (const char*);
329static int (*dll_rb_enc_find_index) (const char*);
330static rb_encoding* (*dll_rb_enc_find) (const char*);
331static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
332static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100333static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar639a2552010-03-17 18:15:23 +0100334static void (*ruby_init_stack)(VALUE*);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100335static void* (*ruby_process_options)(int, char**);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100336#endif
337
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100338#if defined(RUBY19_OR_LATER) && !defined(PROTO)
339SIGNED_VALUE rb_num2long_stub(VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100340{
341 return dll_rb_num2long(x);
342}
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100343VALUE rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100344{
345 return dll_rb_int2big(x);
346}
347#endif
348
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200349static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350
351/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000352 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354static struct
355{
356 char *name;
357 RUBY_PROC *ptr;
358} ruby_funcname_table[] =
359{
360 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
361 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
362 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
363 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
364 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
365 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
366 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
367 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
368 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
369 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
370 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
371 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
372 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
373 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
374 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
375 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
376 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
377 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
378 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
379 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
380 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
381 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
382 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
383 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
384 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
385 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
386 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
387 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
388 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaarb213da02012-10-03 18:06:59 +0200389#if SIZEOF_INT < SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200390 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
391 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
392 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaarb213da02012-10-03 18:06:59 +0200393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
395 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
396 {"rb_load_protect", (RUBY_PROC*)&dll_rb_load_protect},
397 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
398 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
399 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
400 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
401 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
402 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200403#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
404 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
405#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
409 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
410 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200411#ifdef need_rb_str_new_cstr
412 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
413#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200415#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100416#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100417 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
418#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
422 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200423#ifdef WIN3264
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100424 {
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200425# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER < 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100426 "NtInitialize",
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200427# else
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100428 "ruby_sysinit",
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200429# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100430 (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200431# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200433# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434#endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100435#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
436 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
437 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
438 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
439 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
440#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100441#ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100442 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100443 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
444 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
445 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
446 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
447 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100448 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar639a2552010-03-17 18:15:23 +0100449 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100450 {"ruby_process_options", (RUBY_PROC*)&dll_ruby_process_options},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 {"", NULL},
453};
454
455/*
456 * Free ruby.dll
457 */
458 static void
459end_dynamic_ruby()
460{
461 if (hinstRuby)
462 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200463 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200464 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 }
466}
467
468/*
469 * Load library and get all pointers.
470 * Parameter 'libname' provides name of DLL.
471 * Return OK or FAIL.
472 */
473 static int
474ruby_runtime_link_init(char *libname, int verbose)
475{
476 int i;
477
478 if (hinstRuby)
479 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200480 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 if (!hinstRuby)
482 {
483 if (verbose)
484 EMSG2(_(e_loadlib), libname);
485 return FAIL;
486 }
487
488 for (i = 0; ruby_funcname_table[i].ptr; ++i)
489 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200490 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 ruby_funcname_table[i].name)))
492 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200493 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200494 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 if (verbose)
496 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
497 return FAIL;
498 }
499 }
500 return OK;
501}
502
503/*
504 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
505 * else FALSE.
506 */
507 int
508ruby_enabled(verbose)
509 int verbose;
510{
511 return ruby_runtime_link_init(DYNAMIC_RUBY_DLL, verbose) == OK;
512}
513#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
514
515 void
516ruby_end()
517{
518#ifdef DYNAMIC_RUBY
519 end_dynamic_ruby();
520#endif
521}
522
523void ex_ruby(exarg_T *eap)
524{
525 int state;
526 char *script = NULL;
527
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000528 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 if (!eap->skip && ensure_ruby_initialized())
530 {
531 if (script == NULL)
532 rb_eval_string_protect((char *)eap->arg, &state);
533 else
534 rb_eval_string_protect(script, &state);
535 if (state)
536 error_print(state);
537 }
538 vim_free(script);
539}
540
Bram Moolenaar165641d2010-02-17 16:23:09 +0100541/*
542 * In Ruby 1.9 or later, ruby String object has encoding.
543 * conversion buffer string of vim to ruby String object using
544 * VIM encoding option.
545 */
546 static VALUE
547vim_str2rb_enc_str(const char *s)
548{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100549#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100550 int isnum;
551 long lval;
552 char_u *sval;
553 rb_encoding *enc;
554
555 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
556 if (isnum == 0)
557 {
558 enc = rb_enc_find((char *)sval);
559 vim_free(sval);
560 if (enc) {
561 return rb_enc_str_new(s, strlen(s), enc);
562 }
563 }
564#endif
565 return rb_str_new2(s);
566}
567
568 static VALUE
569eval_enc_string_protect(const char *str, int *state)
570{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100571#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100572 int isnum;
573 long lval;
574 char_u *sval;
575 rb_encoding *enc;
576 VALUE v;
577
578 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
579 if (isnum == 0)
580 {
581 enc = rb_enc_find((char *)sval);
582 vim_free(sval);
583 if (enc)
584 {
585 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
586 return rb_eval_string_protect(StringValuePtr(v), state);
587 }
588 }
589#endif
590 return rb_eval_string_protect(str, state);
591}
592
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593void ex_rubydo(exarg_T *eap)
594{
595 int state;
596 linenr_T i;
597
598 if (ensure_ruby_initialized())
599 {
600 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
601 return;
602 for (i = eap->line1; i <= eap->line2; i++) {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100603 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100605 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100607 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 if (state) {
609 error_print(state);
610 break;
611 }
612 line = rb_lastline_get();
613 if (!NIL_P(line)) {
614 if (TYPE(line) != T_STRING) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000615 EMSG(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 return;
617 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100618 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 changed();
620#ifdef SYNTAX_HL
621 syn_changed(i); /* recompute syntax hl. for this line */
622#endif
623 }
624 }
625 check_cursor();
626 update_curbuf(NOT_VALID);
627 }
628}
629
630void ex_rubyfile(exarg_T *eap)
631{
632 int state;
633
634 if (ensure_ruby_initialized())
635 {
636 rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
637 if (state) error_print(state);
638 }
639}
640
641void ruby_buffer_free(buf_T *buf)
642{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000643 if (buf->b_ruby_ref)
644 {
645 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
646 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 }
648}
649
650void ruby_window_free(win_T *win)
651{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000652 if (win->w_ruby_ref)
653 {
654 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
655 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 }
657}
658
659static int ensure_ruby_initialized(void)
660{
661 if (!ruby_initialized)
662 {
663#ifdef DYNAMIC_RUBY
664 if (ruby_enabled(TRUE))
665 {
666#endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100667#ifdef _WIN32
668 /* suggested by Ariya Mizutani */
669 int argc = 1;
670 char *argv[] = {"gvim.exe"};
671 NtInitialize(&argc, &argv);
672#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200673 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100674#ifdef RUBY19_OR_LATER
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200675 RUBY_INIT_STACK;
Bram Moolenaar165641d2010-02-17 16:23:09 +0100676#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200677 ruby_init();
678 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100679#ifdef RUBY19_OR_LATER
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100680 {
681 int dummy_argc = 2;
682 char *dummy_argv[] = {"vim-ruby", "-e0"};
683 ruby_process_options(dummy_argc, dummy_argv);
684 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100685 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100686#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100688#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100689 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 ruby_vim_init();
691 ruby_initialized = 1;
692#ifdef DYNAMIC_RUBY
693 }
694 else
695 {
696 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
697 return 0;
698 }
699#endif
700 }
701 return ruby_initialized;
702}
703
704static void error_print(int state)
705{
706#ifndef DYNAMIC_RUBY
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100707#if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
708 && !(defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 RUBYEXTERN VALUE ruby_errinfo;
710#endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 VALUE eclass;
713 VALUE einfo;
714 char buff[BUFSIZ];
715
716#define TAG_RETURN 0x1
717#define TAG_BREAK 0x2
718#define TAG_NEXT 0x3
719#define TAG_RETRY 0x4
720#define TAG_REDO 0x5
721#define TAG_RAISE 0x6
722#define TAG_THROW 0x7
723#define TAG_FATAL 0x8
724#define TAG_MASK 0xf
725
726 switch (state) {
727 case TAG_RETURN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000728 EMSG(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 break;
730 case TAG_NEXT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000731 EMSG(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 break;
733 case TAG_BREAK:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000734 EMSG(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 break;
736 case TAG_REDO:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000737 EMSG(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 break;
739 case TAG_RETRY:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000740 EMSG(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 break;
742 case TAG_RAISE:
743 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +0100744#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100745 eclass = CLASS_OF(rb_errinfo());
746 einfo = rb_obj_as_string(rb_errinfo());
747#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 eclass = CLASS_OF(ruby_errinfo);
749 einfo = rb_obj_as_string(ruby_errinfo);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100750#endif
751 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000752 EMSG(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 }
754 else {
755 VALUE epath;
756 char *p;
757
758 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000759 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar165641d2010-02-17 16:23:09 +0100760 RSTRING_PTR(epath), RSTRING_PTR(einfo));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 p = strchr(buff, '\n');
762 if (p) *p = '\0';
763 EMSG(buff);
764 }
765 break;
766 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000767 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 EMSG(buff);
769 break;
770 }
771}
772
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000773static VALUE vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774{
775 char *buff, *p;
776
777 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +0200778 if (RSTRING_LEN(str) > 0)
779 {
780 /* Only do this when the string isn't empty, alloc(0) causes trouble. */
781 buff = ALLOCA_N(char, RSTRING_LEN(str));
782 strcpy(buff, RSTRING_PTR(str));
783 p = strchr(buff, '\n');
784 if (p) *p = '\0';
785 MSG(buff);
786 }
787 else
788 {
789 MSG("");
790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 return Qnil;
792}
793
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000794static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100796 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 update_screen(NOT_VALID);
798 return Qnil;
799}
800
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000801static VALUE vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100803 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 return Qnil;
805}
806
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100807#ifdef FEAT_EVAL
808static VALUE vim_to_ruby(typval_T *tv)
809{
810 VALUE result = Qnil;
811
812 if (tv->v_type == VAR_STRING)
813 {
Bram Moolenaar94127e42010-03-19 23:08:48 +0100814 result = rb_str_new2(tv->vval.v_string == NULL
815 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100816 }
817 else if (tv->v_type == VAR_NUMBER)
818 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100819 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100820 }
821# ifdef FEAT_FLOAT
822 else if (tv->v_type == VAR_FLOAT)
823 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100824 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100825 }
826# endif
827 else if (tv->v_type == VAR_LIST)
828 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100829 list_T *list = tv->vval.v_list;
830 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100831
Bram Moolenaar639a2552010-03-17 18:15:23 +0100832 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100833
Bram Moolenaar639a2552010-03-17 18:15:23 +0100834 if (list != NULL)
835 {
836 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
837 {
838 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
839 }
840 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100841 }
842 else if (tv->v_type == VAR_DICT)
843 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100844 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100845
Bram Moolenaar639a2552010-03-17 18:15:23 +0100846 if (tv->vval.v_dict != NULL)
847 {
848 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
849 long_u todo = ht->ht_used;
850 hashitem_T *hi;
851 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100852
Bram Moolenaar639a2552010-03-17 18:15:23 +0100853 for (hi = ht->ht_array; todo > 0; ++hi)
854 {
855 if (!HASHITEM_EMPTY(hi))
856 {
857 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100858
Bram Moolenaar639a2552010-03-17 18:15:23 +0100859 di = dict_lookup(hi);
860 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100861 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +0100862 }
863 }
864 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100865 } /* else return Qnil; */
866
867 return result;
868}
869#endif
870
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000871static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872{
873#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100874 typval_T *tv;
875 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100877 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
878 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100880 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100882 result = vim_to_ruby(tv);
883
884 free_tv(tv);
885
886 return result;
887#else
888 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890}
891
892static VALUE buffer_new(buf_T *buf)
893{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000894 if (buf->b_ruby_ref)
895 {
896 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 }
Bram Moolenaare344bea2005-09-01 20:46:49 +0000898 else
899 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000901 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
903 return obj;
904 }
905}
906
907static buf_T *get_buf(VALUE obj)
908{
909 buf_T *buf;
910
911 Data_Get_Struct(obj, buf_T, buf);
912 if (buf == NULL)
913 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
914 return buf;
915}
916
917static VALUE buffer_s_current()
918{
919 return buffer_new(curbuf);
920}
921
922static VALUE buffer_s_count()
923{
924 buf_T *b;
925 int n = 0;
926
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000927 for (b = firstbuf; b != NULL; b = b->b_next)
928 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000929 /* Deleted buffers should not be counted
930 * SegPhault - 01/07/05 */
931 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000932 n++;
933 }
934
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 return INT2NUM(n);
936}
937
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000938static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939{
940 buf_T *b;
941 int n = NUM2INT(num);
942
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000943 for (b = firstbuf; b != NULL; b = b->b_next)
944 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000945 /* Deleted buffers should not be counted
946 * SegPhault - 01/07/05 */
947 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000948 continue;
949
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000950 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000952
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000953 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 }
955 return Qnil;
956}
957
958static VALUE buffer_name(VALUE self)
959{
960 buf_T *buf = get_buf(self);
961
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000962 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963}
964
965static VALUE buffer_number(VALUE self)
966{
967 buf_T *buf = get_buf(self);
968
969 return INT2NUM(buf->b_fnum);
970}
971
972static VALUE buffer_count(VALUE self)
973{
974 buf_T *buf = get_buf(self);
975
976 return INT2NUM(buf->b_ml.ml_line_count);
977}
978
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000979static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980{
Bram Moolenaar3c531602010-11-16 14:46:19 +0100981 if (n <= 0 || n > buf->b_ml.ml_line_count)
982 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
983 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984}
985
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000986static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987{
988 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000989
990 if (buf != NULL)
991 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
992 return Qnil; /* For stop warning */
993}
994
995static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
996{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100997 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000998 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001000 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1001 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001002 /* set curwin/curbuf for "buf" and save some things */
1003 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001004
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 if (u_savesub(n) == OK) {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001006 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 changed();
1008#ifdef SYNTAX_HL
1009 syn_changed(n); /* recompute syntax hl. for this line */
1010#endif
1011 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001012
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001013 /* restore curwin/curbuf and a few other things */
1014 aucmd_restbuf(&aco);
1015 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001016
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 update_curbuf(NOT_VALID);
1018 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001019 else
1020 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001021 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 }
1023 return str;
1024}
1025
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001026static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
1027{
1028 buf_T *buf = get_buf(self);
1029
1030 if (buf != NULL)
1031 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1032 return str;
1033}
1034
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035static VALUE buffer_delete(VALUE self, VALUE num)
1036{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001037 buf_T *buf = get_buf(self);
1038 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001039 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001041 if (n > 0 && n <= buf->b_ml.ml_line_count)
1042 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001043 /* set curwin/curbuf for "buf" and save some things */
1044 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001045
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 if (u_savedel(n, 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001048
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001049 /* Changes to non-active buffers should properly refresh
1050 * SegPhault - 01/09/05 */
1051 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001052
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 changed();
1054 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001055
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001056 /* restore curwin/curbuf and a few other things */
1057 aucmd_restbuf(&aco);
1058 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001059
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 update_curbuf(NOT_VALID);
1061 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001062 else
1063 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001064 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 }
1066 return Qnil;
1067}
1068
1069static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
1070{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001071 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001072 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001073 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001074 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075
Bram Moolenaar3c531602010-11-16 14:46:19 +01001076 if (line == NULL)
1077 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001078 rb_raise(rb_eIndexError, "NULL line");
1079 }
1080 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001081 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001082 /* set curwin/curbuf for "buf" and save some things */
1083 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001084
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 if (u_inssub(n + 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001087
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001088 /* Changes to non-active buffers should properly refresh screen
1089 * SegPhault - 12/20/04 */
1090 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001091
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001092 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001094
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001095 /* restore curwin/curbuf and a few other things */
1096 aucmd_restbuf(&aco);
1097 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001098
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 update_curbuf(NOT_VALID);
1100 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001101 else
1102 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001103 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 }
1105 return str;
1106}
1107
1108static VALUE window_new(win_T *win)
1109{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001110 if (win->w_ruby_ref)
1111 {
1112 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001114 else
1115 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001117 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1119 return obj;
1120 }
1121}
1122
1123static win_T *get_win(VALUE obj)
1124{
1125 win_T *win;
1126
1127 Data_Get_Struct(obj, win_T, win);
1128 if (win == NULL)
1129 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1130 return win;
1131}
1132
1133static VALUE window_s_current()
1134{
1135 return window_new(curwin);
1136}
1137
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001138/*
1139 * Added line manipulation functions
1140 * SegPhault - 03/07/05
1141 */
1142static VALUE line_s_current()
1143{
1144 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1145}
1146
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001147static VALUE set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001148{
1149 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1150}
1151
1152static VALUE current_line_number()
1153{
1154 return INT2FIX((int)curwin->w_cursor.lnum);
1155}
1156
1157
1158
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159static VALUE window_s_count()
1160{
1161#ifdef FEAT_WINDOWS
1162 win_T *w;
1163 int n = 0;
1164
Bram Moolenaarf740b292006-02-16 22:11:02 +00001165 for (w = firstwin; w != NULL; w = w->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 n++;
1167 return INT2NUM(n);
1168#else
1169 return INT2NUM(1);
1170#endif
1171}
1172
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001173static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174{
1175 win_T *w;
1176 int n = NUM2INT(num);
1177
1178#ifndef FEAT_WINDOWS
1179 w = curwin;
1180#else
1181 for (w = firstwin; w != NULL; w = w->w_next, --n)
1182#endif
1183 if (n == 0)
1184 return window_new(w);
1185 return Qnil;
1186}
1187
1188static VALUE window_buffer(VALUE self)
1189{
1190 win_T *win = get_win(self);
1191
1192 return buffer_new(win->w_buffer);
1193}
1194
1195static VALUE window_height(VALUE self)
1196{
1197 win_T *win = get_win(self);
1198
1199 return INT2NUM(win->w_height);
1200}
1201
1202static VALUE window_set_height(VALUE self, VALUE height)
1203{
1204 win_T *win = get_win(self);
1205 win_T *savewin = curwin;
1206
1207 curwin = win;
1208 win_setheight(NUM2INT(height));
1209 curwin = savewin;
1210 return height;
1211}
1212
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001213static VALUE window_width(VALUE self)
1214{
1215 win_T *win = get_win(self);
1216
1217 return INT2NUM(win->w_width);
1218}
1219
1220static VALUE window_set_width(VALUE self, VALUE width)
1221{
1222 win_T *win = get_win(self);
1223 win_T *savewin = curwin;
1224
1225 curwin = win;
1226 win_setwidth(NUM2INT(width));
1227 curwin = savewin;
1228 return width;
1229}
1230
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231static VALUE window_cursor(VALUE self)
1232{
1233 win_T *win = get_win(self);
1234
1235 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1236}
1237
1238static VALUE window_set_cursor(VALUE self, VALUE pos)
1239{
1240 VALUE lnum, col;
1241 win_T *win = get_win(self);
1242
1243 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001244 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001246 lnum = RARRAY_PTR(pos)[0];
1247 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 win->w_cursor.lnum = NUM2LONG(lnum);
1249 win->w_cursor.col = NUM2UINT(col);
1250 check_cursor(); /* put cursor on an existing line */
1251 update_screen(NOT_VALID);
1252 return Qnil;
1253}
1254
Bram Moolenaar6217cdc2012-04-25 12:28:09 +02001255static VALUE f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001256{
1257 return Qnil;
1258}
1259
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001260static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261{
1262 int i;
1263 VALUE str = rb_str_new("", 0);
1264
1265 for (i = 0; i < argc; i++) {
1266 if (i > 0) rb_str_cat(str, ", ", 2);
1267 rb_str_concat(str, rb_inspect(argv[i]));
1268 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001269 MSG(RSTRING_PTR(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 return Qnil;
1271}
1272
1273static void ruby_io_init(void)
1274{
1275#ifndef DYNAMIC_RUBY
1276 RUBYEXTERN VALUE rb_stdout;
1277#endif
1278
1279 rb_stdout = rb_obj_alloc(rb_cObject);
1280 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001281 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 rb_define_global_function("p", f_p, -1);
1283}
1284
1285static void ruby_vim_init(void)
1286{
1287 objtbl = rb_hash_new();
1288 rb_global_variable(&objtbl);
1289
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001290 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
1291 * alias "VIM" for backwards compatiblity. */
1292 mVIM = rb_define_module("Vim");
1293 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1295 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1296 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1297 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1298 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1299 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1300 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1301 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1302 rb_define_module_function(mVIM, "message", vim_message, 1);
1303 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1304 rb_define_module_function(mVIM, "command", vim_command, 1);
1305 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
1306
1307 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1308 rb_eStandardError);
1309 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1310 rb_eStandardError);
1311
1312 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1313 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1314 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1315 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1316 rb_define_method(cBuffer, "name", buffer_name, 0);
1317 rb_define_method(cBuffer, "number", buffer_number, 0);
1318 rb_define_method(cBuffer, "count", buffer_count, 0);
1319 rb_define_method(cBuffer, "length", buffer_count, 0);
1320 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1321 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1322 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1323 rb_define_method(cBuffer, "append", buffer_append, 2);
1324
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001325 /* Added line manipulation functions
1326 * SegPhault - 03/07/05 */
1327 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1328 rb_define_method(cBuffer, "line", line_s_current, 0);
1329 rb_define_method(cBuffer, "line=", set_current_line, 1);
1330
1331
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1333 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1334 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1335 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1336 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1337 rb_define_method(cVimWindow, "height", window_height, 0);
1338 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001339 rb_define_method(cVimWindow, "width", window_width, 0);
1340 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1342 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1343
1344 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1345 rb_define_virtual_variable("$curwin", window_s_current, 0);
1346}