blob: 80aedf798d6a1323c78edefce288a778dd5a447f [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 Moolenaar76a86062013-05-11 17:45:48 +0200230# ifdef __ia64
231# define rb_ia64_bsp dll_rb_ia64_bsp
232# undef ruby_init_stack
233# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
234# else
235# define ruby_init_stack dll_ruby_init_stack
236# endif
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200237#else
238# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100239#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100240#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100241# define rb_errinfo dll_rb_errinfo
242#else
243# define ruby_errinfo (*dll_ruby_errinfo)
244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245#define ruby_init dll_ruby_init
246#define ruby_init_loadpath dll_ruby_init_loadpath
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200247#ifdef WIN3264
248# define NtInitialize dll_NtInitialize
249# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
250# define rb_w32_snprintf dll_rb_w32_snprintf
251# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252#endif
253
Bram Moolenaar639a2552010-03-17 18:15:23 +0100254#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100255# define ruby_script dll_ruby_script
256# define rb_enc_find_index dll_rb_enc_find_index
257# define rb_enc_find dll_rb_enc_find
258# define rb_enc_str_new dll_rb_enc_str_new
259# define rb_sprintf dll_rb_sprintf
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100260# define rb_require dll_rb_require
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100261# define ruby_process_options dll_ruby_process_options
Bram Moolenaar165641d2010-02-17 16:23:09 +0100262#endif
263
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264/*
265 * Pointers for dynamic link
266 */
267static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200268VALUE *dll_rb_cFalseClass;
269VALUE *dll_rb_cFixnum;
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100270#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
271VALUE *dll_rb_cFloat;
272#endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200273VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274static VALUE *dll_rb_cObject;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200275VALUE *dll_rb_cSymbol;
276VALUE *dll_rb_cTrueClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277static void (*dll_rb_check_type) (VALUE,int);
278static VALUE (*dll_rb_class_path) (VALUE);
279static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
280static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
281static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
282static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
283static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
284static VALUE (*dll_rb_define_module) (const char*);
285static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
286static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
287static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
288static VALUE *dll_rb_stdout;
289static VALUE *dll_rb_eArgError;
290static VALUE *dll_rb_eIndexError;
291static VALUE *dll_rb_eRuntimeError;
292static VALUE *dll_rb_eStandardError;
293static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
294static void (*dll_rb_global_variable) (VALUE*);
295static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
296static VALUE (*dll_rb_hash_new) (void);
297static VALUE (*dll_rb_inspect) (VALUE);
298static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaarb213da02012-10-03 18:06:59 +0200299#if SIZEOF_INT < SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200300static long (*dll_rb_fix2int) (VALUE);
301static long (*dll_rb_num2int) (VALUE);
302static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaarb213da02012-10-03 18:06:59 +0200303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304static VALUE (*dll_rb_lastline_get) (void);
305static void (*dll_rb_lastline_set) (VALUE);
306static void (*dll_rb_load_protect) (VALUE, int, int*);
307static long (*dll_rb_num2long) (VALUE);
308static unsigned long (*dll_rb_num2ulong) (VALUE);
309static VALUE (*dll_rb_obj_alloc) (VALUE);
310static VALUE (*dll_rb_obj_as_string) (VALUE);
311static VALUE (*dll_rb_obj_id) (VALUE);
312static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200313#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
314static VALUE (*dll_rb_string_value) (volatile VALUE*);
315#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
319static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
320static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200321#ifdef need_rb_str_new_cstr
322/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
323static VALUE (*dll_rb_str_new_cstr) (const char*);
324#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200326#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100327#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100328static VALUE (*dll_rb_errinfo) (void);
329#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330static VALUE *dll_ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +0100331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332static void (*dll_ruby_init) (void);
333static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200334#ifdef WIN3264
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100335static void (*dll_NtInitialize) (int*, char***);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200336# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
337static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
338# endif
339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100341static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
342static VALUE (*dll_rb_float_new) (double);
343static VALUE (*dll_rb_ary_new) (void);
344static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaar76a86062013-05-11 17:45:48 +0200345# ifdef __ia64
346static void * (*dll_rb_ia64_bsp) (void);
347static void (*dll_ruby_init_stack)(VALUE*, void*);
348# else
349static void (*dll_ruby_init_stack)(VALUE*);
350# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100351#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100352#ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100353static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355
Bram Moolenaar639a2552010-03-17 18:15:23 +0100356#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100357static void (*dll_ruby_script) (const char*);
358static int (*dll_rb_enc_find_index) (const char*);
359static rb_encoding* (*dll_rb_enc_find) (const char*);
360static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
361static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100362static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100363static void* (*ruby_process_options)(int, char**);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100364#endif
365
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100366#if defined(RUBY19_OR_LATER) && !defined(PROTO)
367SIGNED_VALUE rb_num2long_stub(VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100368{
369 return dll_rb_num2long(x);
370}
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100371VALUE rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100372{
373 return dll_rb_int2big(x);
374}
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200375#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20 \
376 && SIZEOF_INT < SIZEOF_LONG
377long rb_fix2int_stub(VALUE x)
378{
379 return dll_rb_fix2int(x);
380}
381long rb_num2int_stub(VALUE x)
382{
383 return dll_rb_num2int(x);
384}
385#endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100386#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
387VALUE
388rb_float_new_in_heap(double d)
389{
390 return dll_rb_float_new(d);
391}
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100392VALUE rb_num2ulong(VALUE x)
Bram Moolenaar886ed692013-02-26 13:41:35 +0100393{
394 return (long)RSHIFT((SIGNED_VALUE)(x),1);
395}
396#endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100397#endif
398
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200399static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400
401/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000402 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404static struct
405{
406 char *name;
407 RUBY_PROC *ptr;
408} ruby_funcname_table[] =
409{
410 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
411 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
412 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100413#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
414 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
417 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
418 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
419 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
420 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
421 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
422 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
423 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
424 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
425 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
426 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
427 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
428 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
429 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
430 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
431 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
432 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
433 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
434 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
435 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
436 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
437 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
438 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
439 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
440 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
441 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaarb213da02012-10-03 18:06:59 +0200442#if SIZEOF_INT < SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200443 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
444 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
445 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaarb213da02012-10-03 18:06:59 +0200446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
448 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
449 {"rb_load_protect", (RUBY_PROC*)&dll_rb_load_protect},
450 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
451 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
452 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
453 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
454 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
455 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200456#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
457 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
458#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
462 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
463 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200464#ifdef need_rb_str_new_cstr
465 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
466#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200468#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100469#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100470 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
471#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
475 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200476#ifdef WIN3264
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100477 {
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200478# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER < 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100479 "NtInitialize",
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200480# else
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100481 "ruby_sysinit",
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200482# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100483 (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200484# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200486# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487#endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100488#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
489 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar76a86062013-05-11 17:45:48 +0200490# ifdef __ia64
491 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
492# endif
Bram Moolenaar99685e62013-05-11 13:56:18 +0200493 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
Bram Moolenaar886ed692013-02-26 13:41:35 +0100494# if DYNAMIC_RUBY_VER <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100495 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar886ed692013-02-26 13:41:35 +0100496# else
497 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
498# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100499 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
500 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
501#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100502#ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100503 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100504 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
505 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
506 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
507 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
508 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100509 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100510 {"ruby_process_options", (RUBY_PROC*)&dll_ruby_process_options},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 {"", NULL},
513};
514
515/*
516 * Free ruby.dll
517 */
518 static void
519end_dynamic_ruby()
520{
521 if (hinstRuby)
522 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200523 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200524 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 }
526}
527
528/*
529 * Load library and get all pointers.
530 * Parameter 'libname' provides name of DLL.
531 * Return OK or FAIL.
532 */
533 static int
534ruby_runtime_link_init(char *libname, int verbose)
535{
536 int i;
537
538 if (hinstRuby)
539 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200540 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 if (!hinstRuby)
542 {
543 if (verbose)
544 EMSG2(_(e_loadlib), libname);
545 return FAIL;
546 }
547
548 for (i = 0; ruby_funcname_table[i].ptr; ++i)
549 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200550 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 ruby_funcname_table[i].name)))
552 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200553 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200554 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 if (verbose)
556 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
557 return FAIL;
558 }
559 }
560 return OK;
561}
562
563/*
564 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
565 * else FALSE.
566 */
567 int
568ruby_enabled(verbose)
569 int verbose;
570{
571 return ruby_runtime_link_init(DYNAMIC_RUBY_DLL, verbose) == OK;
572}
573#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
574
575 void
576ruby_end()
577{
578#ifdef DYNAMIC_RUBY
579 end_dynamic_ruby();
580#endif
581}
582
583void ex_ruby(exarg_T *eap)
584{
585 int state;
586 char *script = NULL;
587
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000588 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 if (!eap->skip && ensure_ruby_initialized())
590 {
591 if (script == NULL)
592 rb_eval_string_protect((char *)eap->arg, &state);
593 else
594 rb_eval_string_protect(script, &state);
595 if (state)
596 error_print(state);
597 }
598 vim_free(script);
599}
600
Bram Moolenaar165641d2010-02-17 16:23:09 +0100601/*
602 * In Ruby 1.9 or later, ruby String object has encoding.
603 * conversion buffer string of vim to ruby String object using
604 * VIM encoding option.
605 */
606 static VALUE
607vim_str2rb_enc_str(const char *s)
608{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100609#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100610 int isnum;
611 long lval;
612 char_u *sval;
613 rb_encoding *enc;
614
615 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
616 if (isnum == 0)
617 {
618 enc = rb_enc_find((char *)sval);
619 vim_free(sval);
620 if (enc) {
621 return rb_enc_str_new(s, strlen(s), enc);
622 }
623 }
624#endif
625 return rb_str_new2(s);
626}
627
628 static VALUE
629eval_enc_string_protect(const char *str, int *state)
630{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100631#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100632 int isnum;
633 long lval;
634 char_u *sval;
635 rb_encoding *enc;
636 VALUE v;
637
638 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
639 if (isnum == 0)
640 {
641 enc = rb_enc_find((char *)sval);
642 vim_free(sval);
643 if (enc)
644 {
645 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
646 return rb_eval_string_protect(StringValuePtr(v), state);
647 }
648 }
649#endif
650 return rb_eval_string_protect(str, state);
651}
652
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653void ex_rubydo(exarg_T *eap)
654{
655 int state;
656 linenr_T i;
657
658 if (ensure_ruby_initialized())
659 {
660 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
661 return;
662 for (i = eap->line1; i <= eap->line2; i++) {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100663 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100665 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100667 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 if (state) {
669 error_print(state);
670 break;
671 }
672 line = rb_lastline_get();
673 if (!NIL_P(line)) {
674 if (TYPE(line) != T_STRING) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000675 EMSG(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 return;
677 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100678 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 changed();
680#ifdef SYNTAX_HL
681 syn_changed(i); /* recompute syntax hl. for this line */
682#endif
683 }
684 }
685 check_cursor();
686 update_curbuf(NOT_VALID);
687 }
688}
689
690void ex_rubyfile(exarg_T *eap)
691{
692 int state;
693
694 if (ensure_ruby_initialized())
695 {
696 rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
697 if (state) error_print(state);
698 }
699}
700
701void ruby_buffer_free(buf_T *buf)
702{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000703 if (buf->b_ruby_ref)
704 {
705 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
706 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 }
708}
709
710void ruby_window_free(win_T *win)
711{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000712 if (win->w_ruby_ref)
713 {
714 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
715 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 }
717}
718
719static int ensure_ruby_initialized(void)
720{
721 if (!ruby_initialized)
722 {
723#ifdef DYNAMIC_RUBY
724 if (ruby_enabled(TRUE))
725 {
726#endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100727#ifdef _WIN32
728 /* suggested by Ariya Mizutani */
729 int argc = 1;
730 char *argv[] = {"gvim.exe"};
731 NtInitialize(&argc, &argv);
732#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200733 {
Bram Moolenaar76a86062013-05-11 17:45:48 +0200734#if defined(RUBY_VERSION) && RUBY_VERSION >= 18
Bram Moolenaar99685e62013-05-11 13:56:18 +0200735 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100736#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200737 ruby_init();
738 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100739#ifdef RUBY19_OR_LATER
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100740 {
741 int dummy_argc = 2;
742 char *dummy_argv[] = {"vim-ruby", "-e0"};
743 ruby_process_options(dummy_argc, dummy_argv);
744 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100745 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100746#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100748#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100749 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 ruby_vim_init();
751 ruby_initialized = 1;
752#ifdef DYNAMIC_RUBY
753 }
754 else
755 {
756 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
757 return 0;
758 }
759#endif
760 }
761 return ruby_initialized;
762}
763
764static void error_print(int state)
765{
766#ifndef DYNAMIC_RUBY
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100767#if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
768 && !(defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 RUBYEXTERN VALUE ruby_errinfo;
770#endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 VALUE eclass;
773 VALUE einfo;
774 char buff[BUFSIZ];
775
776#define TAG_RETURN 0x1
777#define TAG_BREAK 0x2
778#define TAG_NEXT 0x3
779#define TAG_RETRY 0x4
780#define TAG_REDO 0x5
781#define TAG_RAISE 0x6
782#define TAG_THROW 0x7
783#define TAG_FATAL 0x8
784#define TAG_MASK 0xf
785
786 switch (state) {
787 case TAG_RETURN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000788 EMSG(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 break;
790 case TAG_NEXT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000791 EMSG(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 break;
793 case TAG_BREAK:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000794 EMSG(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 break;
796 case TAG_REDO:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000797 EMSG(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 break;
799 case TAG_RETRY:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000800 EMSG(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 break;
802 case TAG_RAISE:
803 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +0100804#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100805 eclass = CLASS_OF(rb_errinfo());
806 einfo = rb_obj_as_string(rb_errinfo());
807#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 eclass = CLASS_OF(ruby_errinfo);
809 einfo = rb_obj_as_string(ruby_errinfo);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100810#endif
811 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000812 EMSG(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 }
814 else {
815 VALUE epath;
816 char *p;
817
818 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000819 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar165641d2010-02-17 16:23:09 +0100820 RSTRING_PTR(epath), RSTRING_PTR(einfo));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 p = strchr(buff, '\n');
822 if (p) *p = '\0';
823 EMSG(buff);
824 }
825 break;
826 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000827 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 EMSG(buff);
829 break;
830 }
831}
832
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000833static VALUE vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834{
835 char *buff, *p;
836
837 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +0200838 if (RSTRING_LEN(str) > 0)
839 {
840 /* Only do this when the string isn't empty, alloc(0) causes trouble. */
841 buff = ALLOCA_N(char, RSTRING_LEN(str));
842 strcpy(buff, RSTRING_PTR(str));
843 p = strchr(buff, '\n');
844 if (p) *p = '\0';
845 MSG(buff);
846 }
847 else
848 {
849 MSG("");
850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 return Qnil;
852}
853
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000854static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100856 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 update_screen(NOT_VALID);
858 return Qnil;
859}
860
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000861static VALUE vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100863 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 return Qnil;
865}
866
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100867#ifdef FEAT_EVAL
868static VALUE vim_to_ruby(typval_T *tv)
869{
870 VALUE result = Qnil;
871
872 if (tv->v_type == VAR_STRING)
873 {
Bram Moolenaar94127e42010-03-19 23:08:48 +0100874 result = rb_str_new2(tv->vval.v_string == NULL
875 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100876 }
877 else if (tv->v_type == VAR_NUMBER)
878 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100879 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100880 }
881# ifdef FEAT_FLOAT
882 else if (tv->v_type == VAR_FLOAT)
883 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100884 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100885 }
886# endif
887 else if (tv->v_type == VAR_LIST)
888 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100889 list_T *list = tv->vval.v_list;
890 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100891
Bram Moolenaar639a2552010-03-17 18:15:23 +0100892 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100893
Bram Moolenaar639a2552010-03-17 18:15:23 +0100894 if (list != NULL)
895 {
896 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
897 {
898 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
899 }
900 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100901 }
902 else if (tv->v_type == VAR_DICT)
903 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100904 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100905
Bram Moolenaar639a2552010-03-17 18:15:23 +0100906 if (tv->vval.v_dict != NULL)
907 {
908 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
909 long_u todo = ht->ht_used;
910 hashitem_T *hi;
911 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100912
Bram Moolenaar639a2552010-03-17 18:15:23 +0100913 for (hi = ht->ht_array; todo > 0; ++hi)
914 {
915 if (!HASHITEM_EMPTY(hi))
916 {
917 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100918
Bram Moolenaar639a2552010-03-17 18:15:23 +0100919 di = dict_lookup(hi);
920 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100921 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +0100922 }
923 }
924 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100925 } /* else return Qnil; */
926
927 return result;
928}
929#endif
930
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000931static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932{
933#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100934 typval_T *tv;
935 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100937 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
938 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100940 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100942 result = vim_to_ruby(tv);
943
944 free_tv(tv);
945
946 return result;
947#else
948 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950}
951
952static VALUE buffer_new(buf_T *buf)
953{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000954 if (buf->b_ruby_ref)
955 {
956 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 }
Bram Moolenaare344bea2005-09-01 20:46:49 +0000958 else
959 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000961 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
963 return obj;
964 }
965}
966
967static buf_T *get_buf(VALUE obj)
968{
969 buf_T *buf;
970
971 Data_Get_Struct(obj, buf_T, buf);
972 if (buf == NULL)
973 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
974 return buf;
975}
976
977static VALUE buffer_s_current()
978{
979 return buffer_new(curbuf);
980}
981
982static VALUE buffer_s_count()
983{
984 buf_T *b;
985 int n = 0;
986
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000987 for (b = firstbuf; b != NULL; b = b->b_next)
988 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000989 /* Deleted buffers should not be counted
990 * SegPhault - 01/07/05 */
991 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000992 n++;
993 }
994
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 return INT2NUM(n);
996}
997
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000998static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999{
1000 buf_T *b;
1001 int n = NUM2INT(num);
1002
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001003 for (b = firstbuf; b != NULL; b = b->b_next)
1004 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001005 /* Deleted buffers should not be counted
1006 * SegPhault - 01/07/05 */
1007 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001008 continue;
1009
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001010 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001012
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001013 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 }
1015 return Qnil;
1016}
1017
1018static VALUE buffer_name(VALUE self)
1019{
1020 buf_T *buf = get_buf(self);
1021
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001022 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023}
1024
1025static VALUE buffer_number(VALUE self)
1026{
1027 buf_T *buf = get_buf(self);
1028
1029 return INT2NUM(buf->b_fnum);
1030}
1031
1032static VALUE buffer_count(VALUE self)
1033{
1034 buf_T *buf = get_buf(self);
1035
1036 return INT2NUM(buf->b_ml.ml_line_count);
1037}
1038
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001039static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001041 if (n <= 0 || n > buf->b_ml.ml_line_count)
1042 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1043 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044}
1045
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001046static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047{
1048 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001049
1050 if (buf != NULL)
1051 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
1052 return Qnil; /* For stop warning */
1053}
1054
1055static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
1056{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001057 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001058 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001060 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1061 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001062 /* set curwin/curbuf for "buf" and save some things */
1063 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001064
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 if (u_savesub(n) == OK) {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001066 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 changed();
1068#ifdef SYNTAX_HL
1069 syn_changed(n); /* recompute syntax hl. for this line */
1070#endif
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", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 }
1083 return str;
1084}
1085
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001086static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
1087{
1088 buf_T *buf = get_buf(self);
1089
1090 if (buf != NULL)
1091 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1092 return str;
1093}
1094
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095static VALUE buffer_delete(VALUE self, VALUE num)
1096{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001097 buf_T *buf = get_buf(self);
1098 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001099 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001101 if (n > 0 && n <= buf->b_ml.ml_line_count)
1102 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001103 /* set curwin/curbuf for "buf" and save some things */
1104 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001105
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 if (u_savedel(n, 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001108
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001109 /* Changes to non-active buffers should properly refresh
1110 * SegPhault - 01/09/05 */
1111 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001112
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 changed();
1114 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001115
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001116 /* restore curwin/curbuf and a few other things */
1117 aucmd_restbuf(&aco);
1118 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001119
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 update_curbuf(NOT_VALID);
1121 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001122 else
1123 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001124 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 }
1126 return Qnil;
1127}
1128
1129static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
1130{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001131 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001132 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001133 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001134 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135
Bram Moolenaar3c531602010-11-16 14:46:19 +01001136 if (line == NULL)
1137 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001138 rb_raise(rb_eIndexError, "NULL line");
1139 }
1140 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001141 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001142 /* set curwin/curbuf for "buf" and save some things */
1143 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001144
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 if (u_inssub(n + 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001147
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001148 /* Changes to non-active buffers should properly refresh screen
1149 * SegPhault - 12/20/04 */
1150 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001151
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001152 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001154
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001155 /* restore curwin/curbuf and a few other things */
1156 aucmd_restbuf(&aco);
1157 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001158
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 update_curbuf(NOT_VALID);
1160 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001161 else
1162 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001163 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 }
1165 return str;
1166}
1167
1168static VALUE window_new(win_T *win)
1169{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001170 if (win->w_ruby_ref)
1171 {
1172 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001174 else
1175 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001177 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1179 return obj;
1180 }
1181}
1182
1183static win_T *get_win(VALUE obj)
1184{
1185 win_T *win;
1186
1187 Data_Get_Struct(obj, win_T, win);
1188 if (win == NULL)
1189 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1190 return win;
1191}
1192
1193static VALUE window_s_current()
1194{
1195 return window_new(curwin);
1196}
1197
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001198/*
1199 * Added line manipulation functions
1200 * SegPhault - 03/07/05
1201 */
1202static VALUE line_s_current()
1203{
1204 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1205}
1206
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001207static VALUE set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001208{
1209 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1210}
1211
1212static VALUE current_line_number()
1213{
1214 return INT2FIX((int)curwin->w_cursor.lnum);
1215}
1216
1217
1218
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219static VALUE window_s_count()
1220{
1221#ifdef FEAT_WINDOWS
1222 win_T *w;
1223 int n = 0;
1224
Bram Moolenaarf740b292006-02-16 22:11:02 +00001225 for (w = firstwin; w != NULL; w = w->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 n++;
1227 return INT2NUM(n);
1228#else
1229 return INT2NUM(1);
1230#endif
1231}
1232
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001233static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234{
1235 win_T *w;
1236 int n = NUM2INT(num);
1237
1238#ifndef FEAT_WINDOWS
1239 w = curwin;
1240#else
1241 for (w = firstwin; w != NULL; w = w->w_next, --n)
1242#endif
1243 if (n == 0)
1244 return window_new(w);
1245 return Qnil;
1246}
1247
1248static VALUE window_buffer(VALUE self)
1249{
1250 win_T *win = get_win(self);
1251
1252 return buffer_new(win->w_buffer);
1253}
1254
1255static VALUE window_height(VALUE self)
1256{
1257 win_T *win = get_win(self);
1258
1259 return INT2NUM(win->w_height);
1260}
1261
1262static VALUE window_set_height(VALUE self, VALUE height)
1263{
1264 win_T *win = get_win(self);
1265 win_T *savewin = curwin;
1266
1267 curwin = win;
1268 win_setheight(NUM2INT(height));
1269 curwin = savewin;
1270 return height;
1271}
1272
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001273static VALUE window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001274{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001275 return INT2NUM(W_WIDTH(get_win(self)));
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001276}
1277
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001278static VALUE window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001279{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001280#ifdef FEAT_VERTSPLIT
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001281 win_T *win = get_win(self);
1282 win_T *savewin = curwin;
1283
1284 curwin = win;
1285 win_setwidth(NUM2INT(width));
1286 curwin = savewin;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001287#endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001288 return width;
1289}
1290
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291static VALUE window_cursor(VALUE self)
1292{
1293 win_T *win = get_win(self);
1294
1295 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1296}
1297
1298static VALUE window_set_cursor(VALUE self, VALUE pos)
1299{
1300 VALUE lnum, col;
1301 win_T *win = get_win(self);
1302
1303 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001304 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001306 lnum = RARRAY_PTR(pos)[0];
1307 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 win->w_cursor.lnum = NUM2LONG(lnum);
1309 win->w_cursor.col = NUM2UINT(col);
1310 check_cursor(); /* put cursor on an existing line */
1311 update_screen(NOT_VALID);
1312 return Qnil;
1313}
1314
Bram Moolenaar6217cdc2012-04-25 12:28:09 +02001315static VALUE f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001316{
1317 return Qnil;
1318}
1319
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001320static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321{
1322 int i;
1323 VALUE str = rb_str_new("", 0);
1324
1325 for (i = 0; i < argc; i++) {
1326 if (i > 0) rb_str_cat(str, ", ", 2);
1327 rb_str_concat(str, rb_inspect(argv[i]));
1328 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001329 MSG(RSTRING_PTR(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 return Qnil;
1331}
1332
1333static void ruby_io_init(void)
1334{
1335#ifndef DYNAMIC_RUBY
1336 RUBYEXTERN VALUE rb_stdout;
1337#endif
1338
1339 rb_stdout = rb_obj_alloc(rb_cObject);
1340 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001341 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 rb_define_global_function("p", f_p, -1);
1343}
1344
1345static void ruby_vim_init(void)
1346{
1347 objtbl = rb_hash_new();
1348 rb_global_variable(&objtbl);
1349
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001350 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001351 * alias "VIM" for backwards compatibility. */
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001352 mVIM = rb_define_module("Vim");
1353 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1355 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1356 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1357 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1358 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1359 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1360 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1361 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1362 rb_define_module_function(mVIM, "message", vim_message, 1);
1363 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1364 rb_define_module_function(mVIM, "command", vim_command, 1);
1365 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
1366
1367 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1368 rb_eStandardError);
1369 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1370 rb_eStandardError);
1371
1372 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1373 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1374 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1375 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1376 rb_define_method(cBuffer, "name", buffer_name, 0);
1377 rb_define_method(cBuffer, "number", buffer_number, 0);
1378 rb_define_method(cBuffer, "count", buffer_count, 0);
1379 rb_define_method(cBuffer, "length", buffer_count, 0);
1380 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1381 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1382 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1383 rb_define_method(cBuffer, "append", buffer_append, 2);
1384
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001385 /* Added line manipulation functions
1386 * SegPhault - 03/07/05 */
1387 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1388 rb_define_method(cBuffer, "line", line_s_current, 0);
1389 rb_define_method(cBuffer, "line=", set_current_line, 1);
1390
1391
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1393 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1394 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1395 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1396 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1397 rb_define_method(cVimWindow, "height", window_height, 0);
1398 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001399 rb_define_method(cVimWindow, "width", window_width, 0);
1400 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1402 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1403
1404 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1405 rb_define_virtual_variable("$curwin", window_s_current, 0);
1406}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001407
1408void vim_ruby_init(void *stack_start)
1409{
1410 /* should get machine stack start address early in main function */
1411 ruby_stack_start = stack_start;
1412}