blob: f1752f04711255cf433f81a11e3fdc6d45c1393f [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004 *
5 * Ruby interface by Shugo Maeda
6 * with improvements by SegPhault (Ryan Paul)
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +02007 * with improvements by Jon Maken
Bram Moolenaar071d4272004-06-13 20:20:40 +00008 *
9 * Do ":help uganda" in Vim to read copying and usage conditions.
10 * Do ":help credits" in Vim to see a list of people who contributed.
11 * See README.txt for an overview of the Vim source code.
12 */
13
Bram Moolenaar2d73ff42010-10-27 17:40:59 +020014#ifdef HAVE_CONFIG_H
15# include "auto/config.h"
16#endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020017
Bram Moolenaare2793352011-01-17 19:53:27 +010018#include <stdio.h>
19#include <string.h>
20
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#ifdef _WIN32
22# if !defined(DYNAMIC_RUBY_VER) || (DYNAMIC_RUBY_VER < 18)
23# define NT
24# endif
25# ifndef DYNAMIC_RUBY
26# define IMPORT /* For static dll usage __declspec(dllimport) */
27# define RUBYEXTERN __declspec(dllimport)
28# endif
29#endif
30#ifndef RUBYEXTERN
31# define RUBYEXTERN extern
32#endif
33
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020034#ifdef DYNAMIC_RUBY
Bram Moolenaar071d4272004-06-13 20:20:40 +000035/*
36 * This is tricky. In ruby.h there is (inline) function rb_class_of()
37 * definition. This function use these variables. But we want function to
38 * use dll_* variables.
39 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000040# define rb_cFalseClass (*dll_rb_cFalseClass)
41# define rb_cFixnum (*dll_rb_cFixnum)
Bram Moolenaardb3fbe52013-03-07 15:16:21 +010042# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
43# define rb_cFloat (*dll_rb_cFloat)
44# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000045# define rb_cNilClass (*dll_rb_cNilClass)
46# define rb_cSymbol (*dll_rb_cSymbol)
47# define rb_cTrueClass (*dll_rb_cTrueClass)
48# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
49/*
Bram Moolenaar42d57f02010-03-10 12:47:00 +010050 * On ver 1.8, all Ruby functions are exported with "__declspec(dllimport)"
51 * in ruby.h. But it causes trouble for these variables, because it is
Bram Moolenaar071d4272004-06-13 20:20:40 +000052 * defined in this file. When defined this RUBY_EXPORT it modified to
53 * "extern" and be able to avoid this problem.
54 */
55# define RUBY_EXPORT
56# endif
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020057
Bram Moolenaar2d73ff42010-10-27 17:40:59 +020058#if !(defined(WIN32) || defined(_WIN64))
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020059# include <dlfcn.h>
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020060# define HINSTANCE void*
61# define RUBY_PROC void*
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020062# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
63# define symbol_from_dll dlsym
64# define close_dll dlclose
65#else
Bram Moolenaar3ca71f12010-10-27 16:49:47 +020066# define RUBY_PROC FARPROC
Bram Moolenaarebbcb822010-10-23 14:02:54 +020067# define load_dll vimLoadLib
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020068# define symbol_from_dll GetProcAddress
69# define close_dll FreeLibrary
Bram Moolenaar071d4272004-06-13 20:20:40 +000070#endif
71
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +020072#endif /* ifdef DYNAMIC_RUBY */
73
Bram Moolenaar0b69c732010-02-17 15:11:50 +010074/* suggested by Ariya Mizutani */
75#if (_MSC_VER == 1200)
76# undef _WIN32_WINNT
77#endif
78
Bram Moolenaar639a2552010-03-17 18:15:23 +010079#if (defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
80 || (defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
81# define RUBY19_OR_LATER 1
82#endif
83
Bram Moolenaar42d57f02010-03-10 12:47:00 +010084#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19
85/* Ruby 1.9 defines a number of static functions which use rb_num2long and
86 * rb_int2big */
87# define rb_num2long rb_num2long_stub
88# define rb_int2big rb_int2big_stub
89#endif
90
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +020091#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20 \
92 && SIZEOF_INT < SIZEOF_LONG
93/* Ruby 2.0 defines a number of static functions which use rb_fix2int and
94 * rb_num2int if SIZEOF_INT < SIZEOF_LONG (64bit) */
95# define rb_fix2int rb_fix2int_stub
96# define rb_num2int rb_num2int_stub
97#endif
98
Bram Moolenaar071d4272004-06-13 20:20:40 +000099#include <ruby.h>
Bram Moolenaar639a2552010-03-17 18:15:23 +0100100#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100101# include <ruby/encoding.h>
102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100104#undef off_t /* ruby defines off_t as _int64, Mingw uses long */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105#undef EXTERN
106#undef _
107
108/* T_DATA defined both by Ruby and Mac header files, hack around it... */
Bram Moolenaarcb635362007-05-12 13:02:42 +0000109#if defined(MACOS_X_UNIX) || defined(macintosh)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110# define __OPENTRANSPORT__
111# define __OPENTRANSPORTPROTOCOL__
112# define __OPENTRANSPORTPROVIDERS__
113#endif
114
Bram Moolenaar165641d2010-02-17 16:23:09 +0100115/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200116 * Backward compatibility for Ruby 1.8 and earlier.
Bram Moolenaar165641d2010-02-17 16:23:09 +0100117 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
118 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
119 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
120 */
121#ifndef StringValuePtr
122# define StringValuePtr(s) STR2CSTR(s)
123#endif
124#ifndef RARRAY_LEN
125# define RARRAY_LEN(s) RARRAY(s)->len
126#endif
127#ifndef RARRAY_PTR
128# define RARRAY_PTR(s) RARRAY(s)->ptr
129#endif
130#ifndef RSTRING_LEN
131# define RSTRING_LEN(s) RSTRING(s)->len
132#endif
133#ifndef RSTRING_PTR
134# define RSTRING_PTR(s) RSTRING(s)->ptr
135#endif
136
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#include "vim.h"
138#include "version.h"
139
140#if defined(PROTO) && !defined(FEAT_RUBY)
141/* Define these to be able to generate the function prototypes. */
142# define VALUE int
143# define RUBY_DATA_FUNC int
144#endif
145
146static int ruby_initialized = 0;
Bram Moolenaar99685e62013-05-11 13:56:18 +0200147static void *ruby_stack_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148static VALUE objtbl;
149
150static VALUE mVIM;
151static VALUE cBuffer;
152static VALUE cVimWindow;
153static VALUE eDeletedBufferError;
154static VALUE eDeletedWindowError;
155
156static int ensure_ruby_initialized(void);
157static void error_print(int);
158static void ruby_io_init(void);
159static void ruby_vim_init(void);
160
161#if defined(DYNAMIC_RUBY) || defined(PROTO)
162#ifdef PROTO
163# define HINSTANCE int /* for generating prototypes */
164#endif
165
166/*
167 * Wrapper defines
168 */
169#define rb_assoc_new dll_rb_assoc_new
170#define rb_cObject (*dll_rb_cObject)
171#define rb_check_type dll_rb_check_type
172#define rb_class_path dll_rb_class_path
173#define rb_data_object_alloc dll_rb_data_object_alloc
174#define rb_define_class_under dll_rb_define_class_under
175#define rb_define_const dll_rb_define_const
176#define rb_define_global_function dll_rb_define_global_function
177#define rb_define_method dll_rb_define_method
178#define rb_define_module dll_rb_define_module
179#define rb_define_module_function dll_rb_define_module_function
180#define rb_define_singleton_method dll_rb_define_singleton_method
181#define rb_define_virtual_variable dll_rb_define_virtual_variable
182#define rb_stdout (*dll_rb_stdout)
183#define rb_eArgError (*dll_rb_eArgError)
184#define rb_eIndexError (*dll_rb_eIndexError)
185#define rb_eRuntimeError (*dll_rb_eRuntimeError)
186#define rb_eStandardError (*dll_rb_eStandardError)
187#define rb_eval_string_protect dll_rb_eval_string_protect
188#define rb_global_variable dll_rb_global_variable
189#define rb_hash_aset dll_rb_hash_aset
190#define rb_hash_new dll_rb_hash_new
191#define rb_inspect dll_rb_inspect
192#define rb_int2inum dll_rb_int2inum
Bram Moolenaarb213da02012-10-03 18:06:59 +0200193#if SIZEOF_INT < SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200194#define rb_fix2int dll_rb_fix2int
195#define rb_num2int dll_rb_num2int
196#define rb_num2uint dll_rb_num2uint
Bram Moolenaarb213da02012-10-03 18:06:59 +0200197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198#define rb_lastline_get dll_rb_lastline_get
199#define rb_lastline_set dll_rb_lastline_set
200#define rb_load_protect dll_rb_load_protect
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200201#ifndef RUBY19_OR_LATER
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202#define rb_num2long dll_rb_num2long
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200203#endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100204#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 19
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205#define rb_num2ulong dll_rb_num2ulong
Bram Moolenaar886ed692013-02-26 13:41:35 +0100206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#define rb_obj_alloc dll_rb_obj_alloc
208#define rb_obj_as_string dll_rb_obj_as_string
209#define rb_obj_id dll_rb_obj_id
210#define rb_raise dll_rb_raise
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211#define rb_str_cat dll_rb_str_cat
212#define rb_str_concat dll_rb_str_concat
213#define rb_str_new dll_rb_str_new
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200214#ifdef rb_str_new2
215/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
216# define need_rb_str_new_cstr 1
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200217/* Ruby's headers #define rb_str_new_cstr to make use of GCC's
218 * __builtin_constant_p extension. */
219# undef rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200220# define rb_str_new_cstr dll_rb_str_new_cstr
221#else
Bram Moolenaar218116c2010-05-20 21:46:00 +0200222# define rb_str_new2 dll_rb_str_new2
223#endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100224#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200225# define rb_string_value dll_rb_string_value
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100226# define rb_string_value_ptr dll_rb_string_value_ptr
227# define rb_float_new dll_rb_float_new
228# define rb_ary_new dll_rb_ary_new
229# define rb_ary_push dll_rb_ary_push
Bram Moolenaar99685e62013-05-11 13:56:18 +0200230# define ruby_init_stack dll_ruby_init_stack
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200231#else
232# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100233#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100234#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100235# define rb_errinfo dll_rb_errinfo
236#else
237# define ruby_errinfo (*dll_ruby_errinfo)
238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239#define ruby_init dll_ruby_init
240#define ruby_init_loadpath dll_ruby_init_loadpath
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200241#ifdef WIN3264
242# define NtInitialize dll_NtInitialize
243# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
244# define rb_w32_snprintf dll_rb_w32_snprintf
245# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246#endif
247
Bram Moolenaar639a2552010-03-17 18:15:23 +0100248#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100249# define ruby_script dll_ruby_script
250# define rb_enc_find_index dll_rb_enc_find_index
251# define rb_enc_find dll_rb_enc_find
252# define rb_enc_str_new dll_rb_enc_str_new
253# define rb_sprintf dll_rb_sprintf
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100254# define rb_require dll_rb_require
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100255# define ruby_process_options dll_ruby_process_options
Bram Moolenaar165641d2010-02-17 16:23:09 +0100256#endif
257
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258/*
259 * Pointers for dynamic link
260 */
261static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200262VALUE *dll_rb_cFalseClass;
263VALUE *dll_rb_cFixnum;
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100264#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
265VALUE *dll_rb_cFloat;
266#endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200267VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268static VALUE *dll_rb_cObject;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200269VALUE *dll_rb_cSymbol;
270VALUE *dll_rb_cTrueClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271static void (*dll_rb_check_type) (VALUE,int);
272static VALUE (*dll_rb_class_path) (VALUE);
273static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
274static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
275static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
276static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
277static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
278static VALUE (*dll_rb_define_module) (const char*);
279static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
280static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
281static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
282static VALUE *dll_rb_stdout;
283static VALUE *dll_rb_eArgError;
284static VALUE *dll_rb_eIndexError;
285static VALUE *dll_rb_eRuntimeError;
286static VALUE *dll_rb_eStandardError;
287static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
288static void (*dll_rb_global_variable) (VALUE*);
289static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
290static VALUE (*dll_rb_hash_new) (void);
291static VALUE (*dll_rb_inspect) (VALUE);
292static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaarb213da02012-10-03 18:06:59 +0200293#if SIZEOF_INT < SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200294static long (*dll_rb_fix2int) (VALUE);
295static long (*dll_rb_num2int) (VALUE);
296static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaarb213da02012-10-03 18:06:59 +0200297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298static VALUE (*dll_rb_lastline_get) (void);
299static void (*dll_rb_lastline_set) (VALUE);
300static void (*dll_rb_load_protect) (VALUE, int, int*);
301static long (*dll_rb_num2long) (VALUE);
302static unsigned long (*dll_rb_num2ulong) (VALUE);
303static VALUE (*dll_rb_obj_alloc) (VALUE);
304static VALUE (*dll_rb_obj_as_string) (VALUE);
305static VALUE (*dll_rb_obj_id) (VALUE);
306static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200307#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
308static VALUE (*dll_rb_string_value) (volatile VALUE*);
309#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
313static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
314static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200315#ifdef need_rb_str_new_cstr
316/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
317static VALUE (*dll_rb_str_new_cstr) (const char*);
318#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200320#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100321#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100322static VALUE (*dll_rb_errinfo) (void);
323#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324static VALUE *dll_ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +0100325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326static void (*dll_ruby_init) (void);
327static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200328#ifdef WIN3264
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100329static void (*dll_NtInitialize) (int*, char***);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200330# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
331static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
332# endif
333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100335static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
336static VALUE (*dll_rb_float_new) (double);
337static VALUE (*dll_rb_ary_new) (void);
338static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaar99685e62013-05-11 13:56:18 +0200339static void (*ruby_init_stack)(VALUE*);
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100340#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100341#ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100342static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344
Bram Moolenaar639a2552010-03-17 18:15:23 +0100345#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100346static void (*dll_ruby_script) (const char*);
347static int (*dll_rb_enc_find_index) (const char*);
348static rb_encoding* (*dll_rb_enc_find) (const char*);
349static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
350static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100351static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100352static void* (*ruby_process_options)(int, char**);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100353#endif
354
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100355#if defined(RUBY19_OR_LATER) && !defined(PROTO)
356SIGNED_VALUE rb_num2long_stub(VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100357{
358 return dll_rb_num2long(x);
359}
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100360VALUE rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100361{
362 return dll_rb_int2big(x);
363}
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200364#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20 \
365 && SIZEOF_INT < SIZEOF_LONG
366long rb_fix2int_stub(VALUE x)
367{
368 return dll_rb_fix2int(x);
369}
370long rb_num2int_stub(VALUE x)
371{
372 return dll_rb_num2int(x);
373}
374#endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100375#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
376VALUE
377rb_float_new_in_heap(double d)
378{
379 return dll_rb_float_new(d);
380}
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100381VALUE rb_num2ulong(VALUE x)
Bram Moolenaar886ed692013-02-26 13:41:35 +0100382{
383 return (long)RSHIFT((SIGNED_VALUE)(x),1);
384}
385#endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100386#endif
387
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200388static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389
390/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000391 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393static struct
394{
395 char *name;
396 RUBY_PROC *ptr;
397} ruby_funcname_table[] =
398{
399 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
400 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
401 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100402#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
403 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
406 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
407 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
408 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
409 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
410 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
411 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
412 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
413 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
414 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
415 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
416 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
417 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
418 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
419 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
420 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
421 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
422 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
423 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
424 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
425 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
426 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
427 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
428 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
429 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
430 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaarb213da02012-10-03 18:06:59 +0200431#if SIZEOF_INT < SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200432 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
433 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
434 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaarb213da02012-10-03 18:06:59 +0200435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
437 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
438 {"rb_load_protect", (RUBY_PROC*)&dll_rb_load_protect},
439 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
440 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
441 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
442 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
443 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
444 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200445#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
446 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
447#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
451 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
452 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200453#ifdef need_rb_str_new_cstr
454 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
455#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200457#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100458#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100459 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
460#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
464 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200465#ifdef WIN3264
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100466 {
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200467# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER < 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100468 "NtInitialize",
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200469# else
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100470 "ruby_sysinit",
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200471# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100472 (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200473# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200475# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476#endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100477#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
478 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar99685e62013-05-11 13:56:18 +0200479 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
Bram Moolenaar886ed692013-02-26 13:41:35 +0100480# if DYNAMIC_RUBY_VER <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100481 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar886ed692013-02-26 13:41:35 +0100482# else
483 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
484# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100485 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
486 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
487#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100488#ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100489 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100490 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
491 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
492 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
493 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
494 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100495 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100496 {"ruby_process_options", (RUBY_PROC*)&dll_ruby_process_options},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 {"", NULL},
499};
500
501/*
502 * Free ruby.dll
503 */
504 static void
505end_dynamic_ruby()
506{
507 if (hinstRuby)
508 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200509 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200510 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 }
512}
513
514/*
515 * Load library and get all pointers.
516 * Parameter 'libname' provides name of DLL.
517 * Return OK or FAIL.
518 */
519 static int
520ruby_runtime_link_init(char *libname, int verbose)
521{
522 int i;
523
524 if (hinstRuby)
525 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200526 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 if (!hinstRuby)
528 {
529 if (verbose)
530 EMSG2(_(e_loadlib), libname);
531 return FAIL;
532 }
533
534 for (i = 0; ruby_funcname_table[i].ptr; ++i)
535 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200536 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 ruby_funcname_table[i].name)))
538 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200539 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200540 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 if (verbose)
542 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
543 return FAIL;
544 }
545 }
546 return OK;
547}
548
549/*
550 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
551 * else FALSE.
552 */
553 int
554ruby_enabled(verbose)
555 int verbose;
556{
557 return ruby_runtime_link_init(DYNAMIC_RUBY_DLL, verbose) == OK;
558}
559#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
560
561 void
562ruby_end()
563{
564#ifdef DYNAMIC_RUBY
565 end_dynamic_ruby();
566#endif
567}
568
569void ex_ruby(exarg_T *eap)
570{
571 int state;
572 char *script = NULL;
573
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000574 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 if (!eap->skip && ensure_ruby_initialized())
576 {
577 if (script == NULL)
578 rb_eval_string_protect((char *)eap->arg, &state);
579 else
580 rb_eval_string_protect(script, &state);
581 if (state)
582 error_print(state);
583 }
584 vim_free(script);
585}
586
Bram Moolenaar165641d2010-02-17 16:23:09 +0100587/*
588 * In Ruby 1.9 or later, ruby String object has encoding.
589 * conversion buffer string of vim to ruby String object using
590 * VIM encoding option.
591 */
592 static VALUE
593vim_str2rb_enc_str(const char *s)
594{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100595#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100596 int isnum;
597 long lval;
598 char_u *sval;
599 rb_encoding *enc;
600
601 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
602 if (isnum == 0)
603 {
604 enc = rb_enc_find((char *)sval);
605 vim_free(sval);
606 if (enc) {
607 return rb_enc_str_new(s, strlen(s), enc);
608 }
609 }
610#endif
611 return rb_str_new2(s);
612}
613
614 static VALUE
615eval_enc_string_protect(const char *str, int *state)
616{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100617#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100618 int isnum;
619 long lval;
620 char_u *sval;
621 rb_encoding *enc;
622 VALUE v;
623
624 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
625 if (isnum == 0)
626 {
627 enc = rb_enc_find((char *)sval);
628 vim_free(sval);
629 if (enc)
630 {
631 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
632 return rb_eval_string_protect(StringValuePtr(v), state);
633 }
634 }
635#endif
636 return rb_eval_string_protect(str, state);
637}
638
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639void ex_rubydo(exarg_T *eap)
640{
641 int state;
642 linenr_T i;
643
644 if (ensure_ruby_initialized())
645 {
646 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
647 return;
648 for (i = eap->line1; i <= eap->line2; i++) {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100649 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100651 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100653 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 if (state) {
655 error_print(state);
656 break;
657 }
658 line = rb_lastline_get();
659 if (!NIL_P(line)) {
660 if (TYPE(line) != T_STRING) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000661 EMSG(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 return;
663 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100664 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 changed();
666#ifdef SYNTAX_HL
667 syn_changed(i); /* recompute syntax hl. for this line */
668#endif
669 }
670 }
671 check_cursor();
672 update_curbuf(NOT_VALID);
673 }
674}
675
676void ex_rubyfile(exarg_T *eap)
677{
678 int state;
679
680 if (ensure_ruby_initialized())
681 {
682 rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
683 if (state) error_print(state);
684 }
685}
686
687void ruby_buffer_free(buf_T *buf)
688{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000689 if (buf->b_ruby_ref)
690 {
691 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
692 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 }
694}
695
696void ruby_window_free(win_T *win)
697{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000698 if (win->w_ruby_ref)
699 {
700 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
701 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 }
703}
704
705static int ensure_ruby_initialized(void)
706{
707 if (!ruby_initialized)
708 {
709#ifdef DYNAMIC_RUBY
710 if (ruby_enabled(TRUE))
711 {
712#endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100713#ifdef _WIN32
714 /* suggested by Ariya Mizutani */
715 int argc = 1;
716 char *argv[] = {"gvim.exe"};
717 NtInitialize(&argc, &argv);
718#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200719 {
Bram Moolenaar99685e62013-05-11 13:56:18 +0200720#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
721 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100722#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200723 ruby_init();
724 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100725#ifdef RUBY19_OR_LATER
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100726 {
727 int dummy_argc = 2;
728 char *dummy_argv[] = {"vim-ruby", "-e0"};
729 ruby_process_options(dummy_argc, dummy_argv);
730 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100731 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100732#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100734#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100735 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 ruby_vim_init();
737 ruby_initialized = 1;
738#ifdef DYNAMIC_RUBY
739 }
740 else
741 {
742 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
743 return 0;
744 }
745#endif
746 }
747 return ruby_initialized;
748}
749
750static void error_print(int state)
751{
752#ifndef DYNAMIC_RUBY
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100753#if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
754 && !(defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 RUBYEXTERN VALUE ruby_errinfo;
756#endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 VALUE eclass;
759 VALUE einfo;
760 char buff[BUFSIZ];
761
762#define TAG_RETURN 0x1
763#define TAG_BREAK 0x2
764#define TAG_NEXT 0x3
765#define TAG_RETRY 0x4
766#define TAG_REDO 0x5
767#define TAG_RAISE 0x6
768#define TAG_THROW 0x7
769#define TAG_FATAL 0x8
770#define TAG_MASK 0xf
771
772 switch (state) {
773 case TAG_RETURN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000774 EMSG(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 break;
776 case TAG_NEXT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000777 EMSG(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 break;
779 case TAG_BREAK:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000780 EMSG(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 break;
782 case TAG_REDO:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000783 EMSG(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 break;
785 case TAG_RETRY:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000786 EMSG(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 break;
788 case TAG_RAISE:
789 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +0100790#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100791 eclass = CLASS_OF(rb_errinfo());
792 einfo = rb_obj_as_string(rb_errinfo());
793#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 eclass = CLASS_OF(ruby_errinfo);
795 einfo = rb_obj_as_string(ruby_errinfo);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100796#endif
797 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000798 EMSG(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 }
800 else {
801 VALUE epath;
802 char *p;
803
804 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000805 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar165641d2010-02-17 16:23:09 +0100806 RSTRING_PTR(epath), RSTRING_PTR(einfo));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 p = strchr(buff, '\n');
808 if (p) *p = '\0';
809 EMSG(buff);
810 }
811 break;
812 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000813 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 EMSG(buff);
815 break;
816 }
817}
818
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000819static VALUE vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820{
821 char *buff, *p;
822
823 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +0200824 if (RSTRING_LEN(str) > 0)
825 {
826 /* Only do this when the string isn't empty, alloc(0) causes trouble. */
827 buff = ALLOCA_N(char, RSTRING_LEN(str));
828 strcpy(buff, RSTRING_PTR(str));
829 p = strchr(buff, '\n');
830 if (p) *p = '\0';
831 MSG(buff);
832 }
833 else
834 {
835 MSG("");
836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 return Qnil;
838}
839
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000840static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100842 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 update_screen(NOT_VALID);
844 return Qnil;
845}
846
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000847static VALUE vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100849 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 return Qnil;
851}
852
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100853#ifdef FEAT_EVAL
854static VALUE vim_to_ruby(typval_T *tv)
855{
856 VALUE result = Qnil;
857
858 if (tv->v_type == VAR_STRING)
859 {
Bram Moolenaar94127e42010-03-19 23:08:48 +0100860 result = rb_str_new2(tv->vval.v_string == NULL
861 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100862 }
863 else if (tv->v_type == VAR_NUMBER)
864 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100865 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100866 }
867# ifdef FEAT_FLOAT
868 else if (tv->v_type == VAR_FLOAT)
869 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100870 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100871 }
872# endif
873 else if (tv->v_type == VAR_LIST)
874 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100875 list_T *list = tv->vval.v_list;
876 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100877
Bram Moolenaar639a2552010-03-17 18:15:23 +0100878 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100879
Bram Moolenaar639a2552010-03-17 18:15:23 +0100880 if (list != NULL)
881 {
882 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
883 {
884 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
885 }
886 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100887 }
888 else if (tv->v_type == VAR_DICT)
889 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100890 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100891
Bram Moolenaar639a2552010-03-17 18:15:23 +0100892 if (tv->vval.v_dict != NULL)
893 {
894 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
895 long_u todo = ht->ht_used;
896 hashitem_T *hi;
897 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100898
Bram Moolenaar639a2552010-03-17 18:15:23 +0100899 for (hi = ht->ht_array; todo > 0; ++hi)
900 {
901 if (!HASHITEM_EMPTY(hi))
902 {
903 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100904
Bram Moolenaar639a2552010-03-17 18:15:23 +0100905 di = dict_lookup(hi);
906 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100907 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +0100908 }
909 }
910 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100911 } /* else return Qnil; */
912
913 return result;
914}
915#endif
916
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000917static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918{
919#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100920 typval_T *tv;
921 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100923 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
924 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100926 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100928 result = vim_to_ruby(tv);
929
930 free_tv(tv);
931
932 return result;
933#else
934 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936}
937
938static VALUE buffer_new(buf_T *buf)
939{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000940 if (buf->b_ruby_ref)
941 {
942 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 }
Bram Moolenaare344bea2005-09-01 20:46:49 +0000944 else
945 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000947 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
949 return obj;
950 }
951}
952
953static buf_T *get_buf(VALUE obj)
954{
955 buf_T *buf;
956
957 Data_Get_Struct(obj, buf_T, buf);
958 if (buf == NULL)
959 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
960 return buf;
961}
962
963static VALUE buffer_s_current()
964{
965 return buffer_new(curbuf);
966}
967
968static VALUE buffer_s_count()
969{
970 buf_T *b;
971 int n = 0;
972
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000973 for (b = firstbuf; b != NULL; b = b->b_next)
974 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000975 /* Deleted buffers should not be counted
976 * SegPhault - 01/07/05 */
977 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000978 n++;
979 }
980
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 return INT2NUM(n);
982}
983
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000984static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985{
986 buf_T *b;
987 int n = NUM2INT(num);
988
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000989 for (b = firstbuf; b != NULL; b = b->b_next)
990 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000991 /* Deleted buffers should not be counted
992 * SegPhault - 01/07/05 */
993 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000994 continue;
995
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000996 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000998
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000999 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 }
1001 return Qnil;
1002}
1003
1004static VALUE buffer_name(VALUE self)
1005{
1006 buf_T *buf = get_buf(self);
1007
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001008 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009}
1010
1011static VALUE buffer_number(VALUE self)
1012{
1013 buf_T *buf = get_buf(self);
1014
1015 return INT2NUM(buf->b_fnum);
1016}
1017
1018static VALUE buffer_count(VALUE self)
1019{
1020 buf_T *buf = get_buf(self);
1021
1022 return INT2NUM(buf->b_ml.ml_line_count);
1023}
1024
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001025static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001027 if (n <= 0 || n > buf->b_ml.ml_line_count)
1028 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1029 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030}
1031
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001032static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033{
1034 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001035
1036 if (buf != NULL)
1037 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
1038 return Qnil; /* For stop warning */
1039}
1040
1041static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
1042{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001043 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001044 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001046 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1047 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001048 /* set curwin/curbuf for "buf" and save some things */
1049 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001050
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 if (u_savesub(n) == OK) {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001052 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 changed();
1054#ifdef SYNTAX_HL
1055 syn_changed(n); /* recompute syntax hl. for this line */
1056#endif
1057 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001058
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001059 /* restore curwin/curbuf and a few other things */
1060 aucmd_restbuf(&aco);
1061 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001062
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 update_curbuf(NOT_VALID);
1064 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001065 else
1066 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001067 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 }
1069 return str;
1070}
1071
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001072static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
1073{
1074 buf_T *buf = get_buf(self);
1075
1076 if (buf != NULL)
1077 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1078 return str;
1079}
1080
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081static VALUE buffer_delete(VALUE self, VALUE num)
1082{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001083 buf_T *buf = get_buf(self);
1084 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001085 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001087 if (n > 0 && n <= buf->b_ml.ml_line_count)
1088 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001089 /* set curwin/curbuf for "buf" and save some things */
1090 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001091
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 if (u_savedel(n, 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001094
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001095 /* Changes to non-active buffers should properly refresh
1096 * SegPhault - 01/09/05 */
1097 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001098
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 changed();
1100 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001101
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001102 /* restore curwin/curbuf and a few other things */
1103 aucmd_restbuf(&aco);
1104 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001105
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 update_curbuf(NOT_VALID);
1107 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001108 else
1109 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001110 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 }
1112 return Qnil;
1113}
1114
1115static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
1116{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001117 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001118 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001119 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001120 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121
Bram Moolenaar3c531602010-11-16 14:46:19 +01001122 if (line == NULL)
1123 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001124 rb_raise(rb_eIndexError, "NULL line");
1125 }
1126 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001127 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001128 /* set curwin/curbuf for "buf" and save some things */
1129 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001130
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 if (u_inssub(n + 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001133
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001134 /* Changes to non-active buffers should properly refresh screen
1135 * SegPhault - 12/20/04 */
1136 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001137
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001138 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001140
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001141 /* restore curwin/curbuf and a few other things */
1142 aucmd_restbuf(&aco);
1143 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001144
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 update_curbuf(NOT_VALID);
1146 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001147 else
1148 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001149 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 }
1151 return str;
1152}
1153
1154static VALUE window_new(win_T *win)
1155{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001156 if (win->w_ruby_ref)
1157 {
1158 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001160 else
1161 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001163 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1165 return obj;
1166 }
1167}
1168
1169static win_T *get_win(VALUE obj)
1170{
1171 win_T *win;
1172
1173 Data_Get_Struct(obj, win_T, win);
1174 if (win == NULL)
1175 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1176 return win;
1177}
1178
1179static VALUE window_s_current()
1180{
1181 return window_new(curwin);
1182}
1183
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001184/*
1185 * Added line manipulation functions
1186 * SegPhault - 03/07/05
1187 */
1188static VALUE line_s_current()
1189{
1190 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1191}
1192
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001193static VALUE set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001194{
1195 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1196}
1197
1198static VALUE current_line_number()
1199{
1200 return INT2FIX((int)curwin->w_cursor.lnum);
1201}
1202
1203
1204
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205static VALUE window_s_count()
1206{
1207#ifdef FEAT_WINDOWS
1208 win_T *w;
1209 int n = 0;
1210
Bram Moolenaarf740b292006-02-16 22:11:02 +00001211 for (w = firstwin; w != NULL; w = w->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 n++;
1213 return INT2NUM(n);
1214#else
1215 return INT2NUM(1);
1216#endif
1217}
1218
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001219static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220{
1221 win_T *w;
1222 int n = NUM2INT(num);
1223
1224#ifndef FEAT_WINDOWS
1225 w = curwin;
1226#else
1227 for (w = firstwin; w != NULL; w = w->w_next, --n)
1228#endif
1229 if (n == 0)
1230 return window_new(w);
1231 return Qnil;
1232}
1233
1234static VALUE window_buffer(VALUE self)
1235{
1236 win_T *win = get_win(self);
1237
1238 return buffer_new(win->w_buffer);
1239}
1240
1241static VALUE window_height(VALUE self)
1242{
1243 win_T *win = get_win(self);
1244
1245 return INT2NUM(win->w_height);
1246}
1247
1248static VALUE window_set_height(VALUE self, VALUE height)
1249{
1250 win_T *win = get_win(self);
1251 win_T *savewin = curwin;
1252
1253 curwin = win;
1254 win_setheight(NUM2INT(height));
1255 curwin = savewin;
1256 return height;
1257}
1258
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001259static VALUE window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001260{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001261 return INT2NUM(W_WIDTH(get_win(self)));
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001262}
1263
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001264static VALUE window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001265{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001266#ifdef FEAT_VERTSPLIT
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001267 win_T *win = get_win(self);
1268 win_T *savewin = curwin;
1269
1270 curwin = win;
1271 win_setwidth(NUM2INT(width));
1272 curwin = savewin;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001273#endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001274 return width;
1275}
1276
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277static VALUE window_cursor(VALUE self)
1278{
1279 win_T *win = get_win(self);
1280
1281 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1282}
1283
1284static VALUE window_set_cursor(VALUE self, VALUE pos)
1285{
1286 VALUE lnum, col;
1287 win_T *win = get_win(self);
1288
1289 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001290 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001292 lnum = RARRAY_PTR(pos)[0];
1293 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 win->w_cursor.lnum = NUM2LONG(lnum);
1295 win->w_cursor.col = NUM2UINT(col);
1296 check_cursor(); /* put cursor on an existing line */
1297 update_screen(NOT_VALID);
1298 return Qnil;
1299}
1300
Bram Moolenaar6217cdc2012-04-25 12:28:09 +02001301static VALUE f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001302{
1303 return Qnil;
1304}
1305
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001306static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307{
1308 int i;
1309 VALUE str = rb_str_new("", 0);
1310
1311 for (i = 0; i < argc; i++) {
1312 if (i > 0) rb_str_cat(str, ", ", 2);
1313 rb_str_concat(str, rb_inspect(argv[i]));
1314 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001315 MSG(RSTRING_PTR(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 return Qnil;
1317}
1318
1319static void ruby_io_init(void)
1320{
1321#ifndef DYNAMIC_RUBY
1322 RUBYEXTERN VALUE rb_stdout;
1323#endif
1324
1325 rb_stdout = rb_obj_alloc(rb_cObject);
1326 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001327 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 rb_define_global_function("p", f_p, -1);
1329}
1330
1331static void ruby_vim_init(void)
1332{
1333 objtbl = rb_hash_new();
1334 rb_global_variable(&objtbl);
1335
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001336 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001337 * alias "VIM" for backwards compatibility. */
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001338 mVIM = rb_define_module("Vim");
1339 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1341 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1342 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1343 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1344 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1345 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1346 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1347 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1348 rb_define_module_function(mVIM, "message", vim_message, 1);
1349 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1350 rb_define_module_function(mVIM, "command", vim_command, 1);
1351 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
1352
1353 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1354 rb_eStandardError);
1355 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1356 rb_eStandardError);
1357
1358 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1359 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1360 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1361 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1362 rb_define_method(cBuffer, "name", buffer_name, 0);
1363 rb_define_method(cBuffer, "number", buffer_number, 0);
1364 rb_define_method(cBuffer, "count", buffer_count, 0);
1365 rb_define_method(cBuffer, "length", buffer_count, 0);
1366 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1367 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1368 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1369 rb_define_method(cBuffer, "append", buffer_append, 2);
1370
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001371 /* Added line manipulation functions
1372 * SegPhault - 03/07/05 */
1373 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1374 rb_define_method(cBuffer, "line", line_s_current, 0);
1375 rb_define_method(cBuffer, "line=", set_current_line, 1);
1376
1377
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1379 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1380 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1381 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1382 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1383 rb_define_method(cVimWindow, "height", window_height, 0);
1384 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001385 rb_define_method(cVimWindow, "width", window_width, 0);
1386 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1388 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1389
1390 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1391 rb_define_virtual_variable("$curwin", window_s_current, 0);
1392}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001393
1394void vim_ruby_init(void *stack_start)
1395{
1396 /* should get machine stack start address early in main function */
1397 ruby_stack_start = stack_start;
1398}