blob: 818aacbfc773845f2f925cea6c56a05962c2536b [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 Moolenaar886ed692013-02-26 13:41:35 +0100192#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 19
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193#define rb_num2ulong dll_rb_num2ulong
Bram Moolenaar886ed692013-02-26 13:41:35 +0100194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195#define rb_obj_alloc dll_rb_obj_alloc
196#define rb_obj_as_string dll_rb_obj_as_string
197#define rb_obj_id dll_rb_obj_id
198#define rb_raise dll_rb_raise
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199#define rb_str_cat dll_rb_str_cat
200#define rb_str_concat dll_rb_str_concat
201#define rb_str_new dll_rb_str_new
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200202#ifdef rb_str_new2
203/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
204# define need_rb_str_new_cstr 1
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200205/* Ruby's headers #define rb_str_new_cstr to make use of GCC's
206 * __builtin_constant_p extension. */
207# undef rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200208# define rb_str_new_cstr dll_rb_str_new_cstr
209#else
Bram Moolenaar218116c2010-05-20 21:46:00 +0200210# define rb_str_new2 dll_rb_str_new2
211#endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100212#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200213# define rb_string_value dll_rb_string_value
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100214# define rb_string_value_ptr dll_rb_string_value_ptr
215# define rb_float_new dll_rb_float_new
216# define rb_ary_new dll_rb_ary_new
217# define rb_ary_push dll_rb_ary_push
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200218#else
219# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100220#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100221#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100222# define rb_errinfo dll_rb_errinfo
223#else
224# define ruby_errinfo (*dll_ruby_errinfo)
225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#define ruby_init dll_ruby_init
227#define ruby_init_loadpath dll_ruby_init_loadpath
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200228#ifdef WIN3264
229# define NtInitialize dll_NtInitialize
230# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
231# define rb_w32_snprintf dll_rb_w32_snprintf
232# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233#endif
234
Bram Moolenaar639a2552010-03-17 18:15:23 +0100235#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100236# define ruby_script dll_ruby_script
237# define rb_enc_find_index dll_rb_enc_find_index
238# define rb_enc_find dll_rb_enc_find
239# define rb_enc_str_new dll_rb_enc_str_new
240# define rb_sprintf dll_rb_sprintf
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100241# define rb_require dll_rb_require
Bram Moolenaar639a2552010-03-17 18:15:23 +0100242# define ruby_init_stack dll_ruby_init_stack
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100243# define ruby_process_options dll_ruby_process_options
Bram Moolenaar165641d2010-02-17 16:23:09 +0100244#endif
245
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246/*
247 * Pointers for dynamic link
248 */
249static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200250VALUE *dll_rb_cFalseClass;
251VALUE *dll_rb_cFixnum;
252VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253static VALUE *dll_rb_cObject;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200254VALUE *dll_rb_cSymbol;
255VALUE *dll_rb_cTrueClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256static void (*dll_rb_check_type) (VALUE,int);
257static VALUE (*dll_rb_class_path) (VALUE);
258static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
259static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
260static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
261static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
262static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
263static VALUE (*dll_rb_define_module) (const char*);
264static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
265static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
266static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
267static VALUE *dll_rb_stdout;
268static VALUE *dll_rb_eArgError;
269static VALUE *dll_rb_eIndexError;
270static VALUE *dll_rb_eRuntimeError;
271static VALUE *dll_rb_eStandardError;
272static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
273static void (*dll_rb_global_variable) (VALUE*);
274static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
275static VALUE (*dll_rb_hash_new) (void);
276static VALUE (*dll_rb_inspect) (VALUE);
277static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaarb213da02012-10-03 18:06:59 +0200278#if SIZEOF_INT < SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200279static long (*dll_rb_fix2int) (VALUE);
280static long (*dll_rb_num2int) (VALUE);
281static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaarb213da02012-10-03 18:06:59 +0200282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283static VALUE (*dll_rb_lastline_get) (void);
284static void (*dll_rb_lastline_set) (VALUE);
285static void (*dll_rb_load_protect) (VALUE, int, int*);
286static long (*dll_rb_num2long) (VALUE);
287static unsigned long (*dll_rb_num2ulong) (VALUE);
288static VALUE (*dll_rb_obj_alloc) (VALUE);
289static VALUE (*dll_rb_obj_as_string) (VALUE);
290static VALUE (*dll_rb_obj_id) (VALUE);
291static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200292#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
293static VALUE (*dll_rb_string_value) (volatile VALUE*);
294#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
298static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
299static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200300#ifdef need_rb_str_new_cstr
301/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
302static VALUE (*dll_rb_str_new_cstr) (const char*);
303#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200305#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100306#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100307static VALUE (*dll_rb_errinfo) (void);
308#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309static VALUE *dll_ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +0100310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311static void (*dll_ruby_init) (void);
312static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200313#ifdef WIN3264
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100314static void (*dll_NtInitialize) (int*, char***);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200315# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
316static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
317# endif
318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100320static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
321static VALUE (*dll_rb_float_new) (double);
322static VALUE (*dll_rb_ary_new) (void);
323static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
324#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100325#ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100326static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328
Bram Moolenaar639a2552010-03-17 18:15:23 +0100329#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100330static void (*dll_ruby_script) (const char*);
331static int (*dll_rb_enc_find_index) (const char*);
332static rb_encoding* (*dll_rb_enc_find) (const char*);
333static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
334static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100335static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar639a2552010-03-17 18:15:23 +0100336static void (*ruby_init_stack)(VALUE*);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100337static void* (*ruby_process_options)(int, char**);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100338#endif
339
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100340#if defined(RUBY19_OR_LATER) && !defined(PROTO)
341SIGNED_VALUE rb_num2long_stub(VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100342{
343 return dll_rb_num2long(x);
344}
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100345VALUE rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100346{
347 return dll_rb_int2big(x);
348}
Bram Moolenaar886ed692013-02-26 13:41:35 +0100349#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
350VALUE
351rb_float_new_in_heap(double d)
352{
353 return dll_rb_float_new(d);
354}
355unsigned long rb_num2ulong(VALUE x)
356{
357 return (long)RSHIFT((SIGNED_VALUE)(x),1);
358}
359#endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100360#endif
361
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200362static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363
364/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000365 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367static struct
368{
369 char *name;
370 RUBY_PROC *ptr;
371} ruby_funcname_table[] =
372{
373 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
374 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
375 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
376 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
377 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
378 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
379 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
380 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
381 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
382 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
383 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
384 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
385 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
386 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
387 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
388 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
389 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
390 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
391 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
392 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
393 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
394 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
395 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
396 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
397 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
398 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
399 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
400 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
401 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaarb213da02012-10-03 18:06:59 +0200402#if SIZEOF_INT < SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200403 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
404 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
405 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaarb213da02012-10-03 18:06:59 +0200406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
408 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
409 {"rb_load_protect", (RUBY_PROC*)&dll_rb_load_protect},
410 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
411 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
412 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
413 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
414 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
415 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200416#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
417 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
418#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
422 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
423 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200424#ifdef need_rb_str_new_cstr
425 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
426#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200428#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100429#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100430 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
431#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
435 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200436#ifdef WIN3264
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100437 {
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200438# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER < 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100439 "NtInitialize",
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200440# else
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100441 "ruby_sysinit",
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200442# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100443 (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200444# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200446# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447#endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100448#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
449 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar886ed692013-02-26 13:41:35 +0100450# if DYNAMIC_RUBY_VER <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100451 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar886ed692013-02-26 13:41:35 +0100452# else
453 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
454# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100455 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
456 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
457#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100458#ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100459 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100460 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
461 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
462 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
463 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
464 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100465 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar639a2552010-03-17 18:15:23 +0100466 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100467 {"ruby_process_options", (RUBY_PROC*)&dll_ruby_process_options},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 {"", NULL},
470};
471
472/*
473 * Free ruby.dll
474 */
475 static void
476end_dynamic_ruby()
477{
478 if (hinstRuby)
479 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200480 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200481 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 }
483}
484
485/*
486 * Load library and get all pointers.
487 * Parameter 'libname' provides name of DLL.
488 * Return OK or FAIL.
489 */
490 static int
491ruby_runtime_link_init(char *libname, int verbose)
492{
493 int i;
494
495 if (hinstRuby)
496 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200497 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 if (!hinstRuby)
499 {
500 if (verbose)
501 EMSG2(_(e_loadlib), libname);
502 return FAIL;
503 }
504
505 for (i = 0; ruby_funcname_table[i].ptr; ++i)
506 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200507 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 ruby_funcname_table[i].name)))
509 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200510 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200511 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 if (verbose)
513 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
514 return FAIL;
515 }
516 }
517 return OK;
518}
519
520/*
521 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
522 * else FALSE.
523 */
524 int
525ruby_enabled(verbose)
526 int verbose;
527{
528 return ruby_runtime_link_init(DYNAMIC_RUBY_DLL, verbose) == OK;
529}
530#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
531
532 void
533ruby_end()
534{
535#ifdef DYNAMIC_RUBY
536 end_dynamic_ruby();
537#endif
538}
539
540void ex_ruby(exarg_T *eap)
541{
542 int state;
543 char *script = NULL;
544
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000545 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 if (!eap->skip && ensure_ruby_initialized())
547 {
548 if (script == NULL)
549 rb_eval_string_protect((char *)eap->arg, &state);
550 else
551 rb_eval_string_protect(script, &state);
552 if (state)
553 error_print(state);
554 }
555 vim_free(script);
556}
557
Bram Moolenaar165641d2010-02-17 16:23:09 +0100558/*
559 * In Ruby 1.9 or later, ruby String object has encoding.
560 * conversion buffer string of vim to ruby String object using
561 * VIM encoding option.
562 */
563 static VALUE
564vim_str2rb_enc_str(const char *s)
565{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100566#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100567 int isnum;
568 long lval;
569 char_u *sval;
570 rb_encoding *enc;
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 return rb_enc_str_new(s, strlen(s), enc);
579 }
580 }
581#endif
582 return rb_str_new2(s);
583}
584
585 static VALUE
586eval_enc_string_protect(const char *str, int *state)
587{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100588#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100589 int isnum;
590 long lval;
591 char_u *sval;
592 rb_encoding *enc;
593 VALUE v;
594
595 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
596 if (isnum == 0)
597 {
598 enc = rb_enc_find((char *)sval);
599 vim_free(sval);
600 if (enc)
601 {
602 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
603 return rb_eval_string_protect(StringValuePtr(v), state);
604 }
605 }
606#endif
607 return rb_eval_string_protect(str, state);
608}
609
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610void ex_rubydo(exarg_T *eap)
611{
612 int state;
613 linenr_T i;
614
615 if (ensure_ruby_initialized())
616 {
617 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
618 return;
619 for (i = eap->line1; i <= eap->line2; i++) {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100620 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100622 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100624 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 if (state) {
626 error_print(state);
627 break;
628 }
629 line = rb_lastline_get();
630 if (!NIL_P(line)) {
631 if (TYPE(line) != T_STRING) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000632 EMSG(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 return;
634 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100635 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 changed();
637#ifdef SYNTAX_HL
638 syn_changed(i); /* recompute syntax hl. for this line */
639#endif
640 }
641 }
642 check_cursor();
643 update_curbuf(NOT_VALID);
644 }
645}
646
647void ex_rubyfile(exarg_T *eap)
648{
649 int state;
650
651 if (ensure_ruby_initialized())
652 {
653 rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
654 if (state) error_print(state);
655 }
656}
657
658void ruby_buffer_free(buf_T *buf)
659{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000660 if (buf->b_ruby_ref)
661 {
662 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
663 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 }
665}
666
667void ruby_window_free(win_T *win)
668{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000669 if (win->w_ruby_ref)
670 {
671 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
672 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 }
674}
675
676static int ensure_ruby_initialized(void)
677{
678 if (!ruby_initialized)
679 {
680#ifdef DYNAMIC_RUBY
681 if (ruby_enabled(TRUE))
682 {
683#endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100684#ifdef _WIN32
685 /* suggested by Ariya Mizutani */
686 int argc = 1;
687 char *argv[] = {"gvim.exe"};
688 NtInitialize(&argc, &argv);
689#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200690 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100691#ifdef RUBY19_OR_LATER
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200692 RUBY_INIT_STACK;
Bram Moolenaar165641d2010-02-17 16:23:09 +0100693#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200694 ruby_init();
695 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100696#ifdef RUBY19_OR_LATER
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100697 {
698 int dummy_argc = 2;
699 char *dummy_argv[] = {"vim-ruby", "-e0"};
700 ruby_process_options(dummy_argc, dummy_argv);
701 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100702 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100703#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100705#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100706 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 ruby_vim_init();
708 ruby_initialized = 1;
709#ifdef DYNAMIC_RUBY
710 }
711 else
712 {
713 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
714 return 0;
715 }
716#endif
717 }
718 return ruby_initialized;
719}
720
721static void error_print(int state)
722{
723#ifndef DYNAMIC_RUBY
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100724#if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
725 && !(defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 RUBYEXTERN VALUE ruby_errinfo;
727#endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 VALUE eclass;
730 VALUE einfo;
731 char buff[BUFSIZ];
732
733#define TAG_RETURN 0x1
734#define TAG_BREAK 0x2
735#define TAG_NEXT 0x3
736#define TAG_RETRY 0x4
737#define TAG_REDO 0x5
738#define TAG_RAISE 0x6
739#define TAG_THROW 0x7
740#define TAG_FATAL 0x8
741#define TAG_MASK 0xf
742
743 switch (state) {
744 case TAG_RETURN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000745 EMSG(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 break;
747 case TAG_NEXT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000748 EMSG(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 break;
750 case TAG_BREAK:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000751 EMSG(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 break;
753 case TAG_REDO:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000754 EMSG(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 break;
756 case TAG_RETRY:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000757 EMSG(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 break;
759 case TAG_RAISE:
760 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +0100761#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100762 eclass = CLASS_OF(rb_errinfo());
763 einfo = rb_obj_as_string(rb_errinfo());
764#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 eclass = CLASS_OF(ruby_errinfo);
766 einfo = rb_obj_as_string(ruby_errinfo);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100767#endif
768 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000769 EMSG(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 }
771 else {
772 VALUE epath;
773 char *p;
774
775 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000776 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar165641d2010-02-17 16:23:09 +0100777 RSTRING_PTR(epath), RSTRING_PTR(einfo));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 p = strchr(buff, '\n');
779 if (p) *p = '\0';
780 EMSG(buff);
781 }
782 break;
783 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000784 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 EMSG(buff);
786 break;
787 }
788}
789
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000790static VALUE vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791{
792 char *buff, *p;
793
794 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +0200795 if (RSTRING_LEN(str) > 0)
796 {
797 /* Only do this when the string isn't empty, alloc(0) causes trouble. */
798 buff = ALLOCA_N(char, RSTRING_LEN(str));
799 strcpy(buff, RSTRING_PTR(str));
800 p = strchr(buff, '\n');
801 if (p) *p = '\0';
802 MSG(buff);
803 }
804 else
805 {
806 MSG("");
807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 return Qnil;
809}
810
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000811static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100813 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 update_screen(NOT_VALID);
815 return Qnil;
816}
817
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000818static VALUE vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100820 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 return Qnil;
822}
823
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100824#ifdef FEAT_EVAL
825static VALUE vim_to_ruby(typval_T *tv)
826{
827 VALUE result = Qnil;
828
829 if (tv->v_type == VAR_STRING)
830 {
Bram Moolenaar94127e42010-03-19 23:08:48 +0100831 result = rb_str_new2(tv->vval.v_string == NULL
832 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100833 }
834 else if (tv->v_type == VAR_NUMBER)
835 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100836 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100837 }
838# ifdef FEAT_FLOAT
839 else if (tv->v_type == VAR_FLOAT)
840 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100841 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100842 }
843# endif
844 else if (tv->v_type == VAR_LIST)
845 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100846 list_T *list = tv->vval.v_list;
847 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100848
Bram Moolenaar639a2552010-03-17 18:15:23 +0100849 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100850
Bram Moolenaar639a2552010-03-17 18:15:23 +0100851 if (list != NULL)
852 {
853 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
854 {
855 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
856 }
857 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100858 }
859 else if (tv->v_type == VAR_DICT)
860 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100861 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100862
Bram Moolenaar639a2552010-03-17 18:15:23 +0100863 if (tv->vval.v_dict != NULL)
864 {
865 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
866 long_u todo = ht->ht_used;
867 hashitem_T *hi;
868 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100869
Bram Moolenaar639a2552010-03-17 18:15:23 +0100870 for (hi = ht->ht_array; todo > 0; ++hi)
871 {
872 if (!HASHITEM_EMPTY(hi))
873 {
874 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100875
Bram Moolenaar639a2552010-03-17 18:15:23 +0100876 di = dict_lookup(hi);
877 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100878 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +0100879 }
880 }
881 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100882 } /* else return Qnil; */
883
884 return result;
885}
886#endif
887
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000888static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889{
890#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100891 typval_T *tv;
892 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100894 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
895 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100897 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100899 result = vim_to_ruby(tv);
900
901 free_tv(tv);
902
903 return result;
904#else
905 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907}
908
909static VALUE buffer_new(buf_T *buf)
910{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000911 if (buf->b_ruby_ref)
912 {
913 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 }
Bram Moolenaare344bea2005-09-01 20:46:49 +0000915 else
916 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000918 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
920 return obj;
921 }
922}
923
924static buf_T *get_buf(VALUE obj)
925{
926 buf_T *buf;
927
928 Data_Get_Struct(obj, buf_T, buf);
929 if (buf == NULL)
930 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
931 return buf;
932}
933
934static VALUE buffer_s_current()
935{
936 return buffer_new(curbuf);
937}
938
939static VALUE buffer_s_count()
940{
941 buf_T *b;
942 int n = 0;
943
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000944 for (b = firstbuf; b != NULL; b = b->b_next)
945 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000946 /* Deleted buffers should not be counted
947 * SegPhault - 01/07/05 */
948 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000949 n++;
950 }
951
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 return INT2NUM(n);
953}
954
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000955static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956{
957 buf_T *b;
958 int n = NUM2INT(num);
959
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000960 for (b = firstbuf; b != NULL; b = b->b_next)
961 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000962 /* Deleted buffers should not be counted
963 * SegPhault - 01/07/05 */
964 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000965 continue;
966
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000967 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000969
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000970 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 }
972 return Qnil;
973}
974
975static VALUE buffer_name(VALUE self)
976{
977 buf_T *buf = get_buf(self);
978
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000979 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980}
981
982static VALUE buffer_number(VALUE self)
983{
984 buf_T *buf = get_buf(self);
985
986 return INT2NUM(buf->b_fnum);
987}
988
989static VALUE buffer_count(VALUE self)
990{
991 buf_T *buf = get_buf(self);
992
993 return INT2NUM(buf->b_ml.ml_line_count);
994}
995
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000996static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997{
Bram Moolenaar3c531602010-11-16 14:46:19 +0100998 if (n <= 0 || n > buf->b_ml.ml_line_count)
999 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1000 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001}
1002
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001003static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004{
1005 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001006
1007 if (buf != NULL)
1008 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
1009 return Qnil; /* For stop warning */
1010}
1011
1012static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
1013{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001014 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001015 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001017 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1018 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001019 /* set curwin/curbuf for "buf" and save some things */
1020 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001021
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 if (u_savesub(n) == OK) {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001023 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 changed();
1025#ifdef SYNTAX_HL
1026 syn_changed(n); /* recompute syntax hl. for this line */
1027#endif
1028 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001029
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001030 /* restore curwin/curbuf and a few other things */
1031 aucmd_restbuf(&aco);
1032 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001033
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 update_curbuf(NOT_VALID);
1035 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001036 else
1037 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001038 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 }
1040 return str;
1041}
1042
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001043static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
1044{
1045 buf_T *buf = get_buf(self);
1046
1047 if (buf != NULL)
1048 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1049 return str;
1050}
1051
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052static VALUE buffer_delete(VALUE self, VALUE num)
1053{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001054 buf_T *buf = get_buf(self);
1055 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001056 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001058 if (n > 0 && n <= buf->b_ml.ml_line_count)
1059 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001060 /* set curwin/curbuf for "buf" and save some things */
1061 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001062
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 if (u_savedel(n, 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001065
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001066 /* Changes to non-active buffers should properly refresh
1067 * SegPhault - 01/09/05 */
1068 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001069
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 changed();
1071 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001072
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001073 /* restore curwin/curbuf and a few other things */
1074 aucmd_restbuf(&aco);
1075 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001076
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 update_curbuf(NOT_VALID);
1078 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001079 else
1080 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001081 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 }
1083 return Qnil;
1084}
1085
1086static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
1087{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001088 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001089 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001090 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001091 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092
Bram Moolenaar3c531602010-11-16 14:46:19 +01001093 if (line == NULL)
1094 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001095 rb_raise(rb_eIndexError, "NULL line");
1096 }
1097 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001098 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001099 /* set curwin/curbuf for "buf" and save some things */
1100 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001101
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 if (u_inssub(n + 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001104
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001105 /* Changes to non-active buffers should properly refresh screen
1106 * SegPhault - 12/20/04 */
1107 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001108
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001109 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001111
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001112 /* restore curwin/curbuf and a few other things */
1113 aucmd_restbuf(&aco);
1114 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001115
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 update_curbuf(NOT_VALID);
1117 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001118 else
1119 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001120 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 }
1122 return str;
1123}
1124
1125static VALUE window_new(win_T *win)
1126{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001127 if (win->w_ruby_ref)
1128 {
1129 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001131 else
1132 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001134 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1136 return obj;
1137 }
1138}
1139
1140static win_T *get_win(VALUE obj)
1141{
1142 win_T *win;
1143
1144 Data_Get_Struct(obj, win_T, win);
1145 if (win == NULL)
1146 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1147 return win;
1148}
1149
1150static VALUE window_s_current()
1151{
1152 return window_new(curwin);
1153}
1154
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001155/*
1156 * Added line manipulation functions
1157 * SegPhault - 03/07/05
1158 */
1159static VALUE line_s_current()
1160{
1161 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1162}
1163
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001164static VALUE set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001165{
1166 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1167}
1168
1169static VALUE current_line_number()
1170{
1171 return INT2FIX((int)curwin->w_cursor.lnum);
1172}
1173
1174
1175
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176static VALUE window_s_count()
1177{
1178#ifdef FEAT_WINDOWS
1179 win_T *w;
1180 int n = 0;
1181
Bram Moolenaarf740b292006-02-16 22:11:02 +00001182 for (w = firstwin; w != NULL; w = w->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 n++;
1184 return INT2NUM(n);
1185#else
1186 return INT2NUM(1);
1187#endif
1188}
1189
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001190static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191{
1192 win_T *w;
1193 int n = NUM2INT(num);
1194
1195#ifndef FEAT_WINDOWS
1196 w = curwin;
1197#else
1198 for (w = firstwin; w != NULL; w = w->w_next, --n)
1199#endif
1200 if (n == 0)
1201 return window_new(w);
1202 return Qnil;
1203}
1204
1205static VALUE window_buffer(VALUE self)
1206{
1207 win_T *win = get_win(self);
1208
1209 return buffer_new(win->w_buffer);
1210}
1211
1212static VALUE window_height(VALUE self)
1213{
1214 win_T *win = get_win(self);
1215
1216 return INT2NUM(win->w_height);
1217}
1218
1219static VALUE window_set_height(VALUE self, VALUE height)
1220{
1221 win_T *win = get_win(self);
1222 win_T *savewin = curwin;
1223
1224 curwin = win;
1225 win_setheight(NUM2INT(height));
1226 curwin = savewin;
1227 return height;
1228}
1229
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001230static VALUE window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001231{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001232 return INT2NUM(W_WIDTH(get_win(self)));
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001233}
1234
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001235static VALUE window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001236{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001237#ifdef FEAT_VERTSPLIT
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001238 win_T *win = get_win(self);
1239 win_T *savewin = curwin;
1240
1241 curwin = win;
1242 win_setwidth(NUM2INT(width));
1243 curwin = savewin;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001244#endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001245 return width;
1246}
1247
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248static VALUE window_cursor(VALUE self)
1249{
1250 win_T *win = get_win(self);
1251
1252 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1253}
1254
1255static VALUE window_set_cursor(VALUE self, VALUE pos)
1256{
1257 VALUE lnum, col;
1258 win_T *win = get_win(self);
1259
1260 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001261 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001263 lnum = RARRAY_PTR(pos)[0];
1264 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 win->w_cursor.lnum = NUM2LONG(lnum);
1266 win->w_cursor.col = NUM2UINT(col);
1267 check_cursor(); /* put cursor on an existing line */
1268 update_screen(NOT_VALID);
1269 return Qnil;
1270}
1271
Bram Moolenaar6217cdc2012-04-25 12:28:09 +02001272static VALUE f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001273{
1274 return Qnil;
1275}
1276
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001277static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278{
1279 int i;
1280 VALUE str = rb_str_new("", 0);
1281
1282 for (i = 0; i < argc; i++) {
1283 if (i > 0) rb_str_cat(str, ", ", 2);
1284 rb_str_concat(str, rb_inspect(argv[i]));
1285 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001286 MSG(RSTRING_PTR(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 return Qnil;
1288}
1289
1290static void ruby_io_init(void)
1291{
1292#ifndef DYNAMIC_RUBY
1293 RUBYEXTERN VALUE rb_stdout;
1294#endif
1295
1296 rb_stdout = rb_obj_alloc(rb_cObject);
1297 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001298 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 rb_define_global_function("p", f_p, -1);
1300}
1301
1302static void ruby_vim_init(void)
1303{
1304 objtbl = rb_hash_new();
1305 rb_global_variable(&objtbl);
1306
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001307 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
1308 * alias "VIM" for backwards compatiblity. */
1309 mVIM = rb_define_module("Vim");
1310 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1312 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1313 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1314 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1315 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1316 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1317 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1318 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1319 rb_define_module_function(mVIM, "message", vim_message, 1);
1320 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1321 rb_define_module_function(mVIM, "command", vim_command, 1);
1322 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
1323
1324 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1325 rb_eStandardError);
1326 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1327 rb_eStandardError);
1328
1329 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1330 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1331 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1332 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1333 rb_define_method(cBuffer, "name", buffer_name, 0);
1334 rb_define_method(cBuffer, "number", buffer_number, 0);
1335 rb_define_method(cBuffer, "count", buffer_count, 0);
1336 rb_define_method(cBuffer, "length", buffer_count, 0);
1337 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1338 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1339 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1340 rb_define_method(cBuffer, "append", buffer_append, 2);
1341
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001342 /* Added line manipulation functions
1343 * SegPhault - 03/07/05 */
1344 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1345 rb_define_method(cBuffer, "line", line_s_current, 0);
1346 rb_define_method(cBuffer, "line=", set_current_line, 1);
1347
1348
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1350 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1351 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1352 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1353 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1354 rb_define_method(cVimWindow, "height", window_height, 0);
1355 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001356 rb_define_method(cVimWindow, "width", window_width, 0);
1357 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1359 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1360
1361 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1362 rb_define_virtual_variable("$curwin", window_s_current, 0);
1363}