blob: 44fd5ee42d8d149ecbe9ffb7d2db1b3eb838b39f [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
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200161#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
162# if defined(__ia64) && !defined(ruby_init_stack)
163# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
164# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#endif
166
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200167#if defined(DYNAMIC_RUBY) || defined(PROTO)
168# ifdef PROTO
169# define HINSTANCE int /* for generating prototypes */
170# endif
171
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172/*
173 * Wrapper defines
174 */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200175# define rb_assoc_new dll_rb_assoc_new
176# define rb_cObject (*dll_rb_cObject)
177# define rb_check_type dll_rb_check_type
178# define rb_class_path dll_rb_class_path
179# define rb_data_object_alloc dll_rb_data_object_alloc
180# define rb_define_class_under dll_rb_define_class_under
181# define rb_define_const dll_rb_define_const
182# define rb_define_global_function dll_rb_define_global_function
183# define rb_define_method dll_rb_define_method
184# define rb_define_module dll_rb_define_module
185# define rb_define_module_function dll_rb_define_module_function
186# define rb_define_singleton_method dll_rb_define_singleton_method
187# define rb_define_virtual_variable dll_rb_define_virtual_variable
188# define rb_stdout (*dll_rb_stdout)
189# define rb_eArgError (*dll_rb_eArgError)
190# define rb_eIndexError (*dll_rb_eIndexError)
191# define rb_eRuntimeError (*dll_rb_eRuntimeError)
192# define rb_eStandardError (*dll_rb_eStandardError)
193# define rb_eval_string_protect dll_rb_eval_string_protect
194# define rb_global_variable dll_rb_global_variable
195# define rb_hash_aset dll_rb_hash_aset
196# define rb_hash_new dll_rb_hash_new
197# define rb_inspect dll_rb_inspect
198# define rb_int2inum dll_rb_int2inum
199# if SIZEOF_INT < SIZEOF_LONG /* 64 bits only */
200# define rb_fix2int dll_rb_fix2int
201# define rb_num2int dll_rb_num2int
202# define rb_num2uint dll_rb_num2uint
203# endif
204# define rb_lastline_get dll_rb_lastline_get
205# define rb_lastline_set dll_rb_lastline_set
206# define rb_load_protect dll_rb_load_protect
207# ifndef RUBY19_OR_LATER
208# define rb_num2long dll_rb_num2long
209# endif
210# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 19
211# define rb_num2ulong dll_rb_num2ulong
212# endif
213# define rb_obj_alloc dll_rb_obj_alloc
214# define rb_obj_as_string dll_rb_obj_as_string
215# define rb_obj_id dll_rb_obj_id
216# define rb_raise dll_rb_raise
217# define rb_str_cat dll_rb_str_cat
218# define rb_str_concat dll_rb_str_concat
219# define rb_str_new dll_rb_str_new
220# ifdef rb_str_new2
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200221/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200222# define need_rb_str_new_cstr 1
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200223/* Ruby's headers #define rb_str_new_cstr to make use of GCC's
224 * __builtin_constant_p extension. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200225# undef rb_str_new_cstr
226# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200227# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200228# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200229# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200230# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200231# define rb_string_value dll_rb_string_value
232# define rb_string_value_ptr dll_rb_string_value_ptr
233# define rb_float_new dll_rb_float_new
234# define rb_ary_new dll_rb_ary_new
235# define rb_ary_push dll_rb_ary_push
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200236# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
237# ifdef __ia64
238# define rb_ia64_bsp dll_rb_ia64_bsp
239# undef ruby_init_stack
240# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
241# else
242# define ruby_init_stack dll_ruby_init_stack
243# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200244# endif
245# else
246# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200247# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200248# ifdef RUBY19_OR_LATER
249# define rb_errinfo dll_rb_errinfo
250# else
251# define ruby_errinfo (*dll_ruby_errinfo)
252# endif
253# define ruby_init dll_ruby_init
254# define ruby_init_loadpath dll_ruby_init_loadpath
255# ifdef WIN3264
256# define NtInitialize dll_NtInitialize
257# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
258# define rb_w32_snprintf dll_rb_w32_snprintf
259# endif
260# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200262# ifdef RUBY19_OR_LATER
263# define ruby_script dll_ruby_script
264# define rb_enc_find_index dll_rb_enc_find_index
265# define rb_enc_find dll_rb_enc_find
266# define rb_enc_str_new dll_rb_enc_str_new
267# define rb_sprintf dll_rb_sprintf
268# define rb_require dll_rb_require
269# define ruby_process_options dll_ruby_process_options
270# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100271
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272/*
273 * Pointers for dynamic link
274 */
275static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200276VALUE *dll_rb_cFalseClass;
277VALUE *dll_rb_cFixnum;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200278# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100279VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200280# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200281VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282static VALUE *dll_rb_cObject;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200283VALUE *dll_rb_cSymbol;
284VALUE *dll_rb_cTrueClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285static void (*dll_rb_check_type) (VALUE,int);
286static VALUE (*dll_rb_class_path) (VALUE);
287static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
288static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
289static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
290static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
291static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
292static VALUE (*dll_rb_define_module) (const char*);
293static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
294static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
295static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
296static VALUE *dll_rb_stdout;
297static VALUE *dll_rb_eArgError;
298static VALUE *dll_rb_eIndexError;
299static VALUE *dll_rb_eRuntimeError;
300static VALUE *dll_rb_eStandardError;
301static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
302static void (*dll_rb_global_variable) (VALUE*);
303static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
304static VALUE (*dll_rb_hash_new) (void);
305static VALUE (*dll_rb_inspect) (VALUE);
306static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200307# if SIZEOF_INT < SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200308static long (*dll_rb_fix2int) (VALUE);
309static long (*dll_rb_num2int) (VALUE);
310static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200311# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312static VALUE (*dll_rb_lastline_get) (void);
313static void (*dll_rb_lastline_set) (VALUE);
314static void (*dll_rb_load_protect) (VALUE, int, int*);
315static long (*dll_rb_num2long) (VALUE);
316static unsigned long (*dll_rb_num2ulong) (VALUE);
317static VALUE (*dll_rb_obj_alloc) (VALUE);
318static VALUE (*dll_rb_obj_as_string) (VALUE);
319static VALUE (*dll_rb_obj_id) (VALUE);
320static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200321# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200322static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200323# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200325# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
327static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
328static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200329# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200330/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
331static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200332# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200334# endif
335# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100336static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200337# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200339# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340static void (*dll_ruby_init) (void);
341static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200342# ifdef WIN3264
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100343static void (*dll_NtInitialize) (int*, char***);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200344# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200345static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200346# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200347# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200348# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100349static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
350static VALUE (*dll_rb_float_new) (double);
351static VALUE (*dll_rb_ary_new) (void);
352static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200353# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
354# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200355static void * (*dll_rb_ia64_bsp) (void);
356static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200357# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200358static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200359# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200360# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200361# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200362# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100363static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200364# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200366# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100367static void (*dll_ruby_script) (const char*);
368static int (*dll_rb_enc_find_index) (const char*);
369static rb_encoding* (*dll_rb_enc_find) (const char*);
370static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
371static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100372static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100373static void* (*ruby_process_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200374# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100375
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200376# if defined(RUBY19_OR_LATER) && !defined(PROTO)
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100377SIGNED_VALUE rb_num2long_stub(VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100378{
379 return dll_rb_num2long(x);
380}
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100381VALUE rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100382{
383 return dll_rb_int2big(x);
384}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200385# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20 \
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200386 && SIZEOF_INT < SIZEOF_LONG
387long rb_fix2int_stub(VALUE x)
388{
389 return dll_rb_fix2int(x);
390}
391long rb_num2int_stub(VALUE x)
392{
393 return dll_rb_num2int(x);
394}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200395# endif
396# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaar886ed692013-02-26 13:41:35 +0100397VALUE
398rb_float_new_in_heap(double d)
399{
400 return dll_rb_float_new(d);
401}
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100402VALUE rb_num2ulong(VALUE x)
Bram Moolenaar886ed692013-02-26 13:41:35 +0100403{
404 return (long)RSHIFT((SIGNED_VALUE)(x),1);
405}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200406# endif
407# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100408
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200409static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410
411/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000412 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414static struct
415{
416 char *name;
417 RUBY_PROC *ptr;
418} ruby_funcname_table[] =
419{
420 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
421 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
422 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200423# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100424 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200425# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
427 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
428 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
429 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
430 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
431 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
432 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
433 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
434 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
435 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
436 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
437 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
438 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
439 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
440 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
441 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
442 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
443 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
444 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
445 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
446 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
447 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
448 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
449 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
450 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
451 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200452# if SIZEOF_INT < SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200453 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
454 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
455 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200456# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
458 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
459 {"rb_load_protect", (RUBY_PROC*)&dll_rb_load_protect},
460 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
461 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
462 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
463 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
464 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
465 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200466# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200467 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200468# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200470# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
472 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
473 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200474# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200475 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200476# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200478# endif
479# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100480 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200481# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200483# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
485 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200486# ifdef WIN3264
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100487 {
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200488# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER < 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100489 "NtInitialize",
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200490# else
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100491 "ruby_sysinit",
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200492# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100493 (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200494# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200496# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200497# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200498# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100499 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200500# if DYNAMIC_RUBY_VER <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100501 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200502# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100503 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200504# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100505 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
506 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200507# endif
508# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100509 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100510 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
511 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
512 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
513 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
514 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100515 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100516 {"ruby_process_options", (RUBY_PROC*)&dll_ruby_process_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200517# endif
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200518# if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
519# ifdef __ia64
520 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
521# endif
522 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
523# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 {"", NULL},
525};
526
527/*
528 * Free ruby.dll
529 */
530 static void
531end_dynamic_ruby()
532{
533 if (hinstRuby)
534 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200535 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200536 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 }
538}
539
540/*
541 * Load library and get all pointers.
542 * Parameter 'libname' provides name of DLL.
543 * Return OK or FAIL.
544 */
545 static int
546ruby_runtime_link_init(char *libname, int verbose)
547{
548 int i;
549
550 if (hinstRuby)
551 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200552 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 if (!hinstRuby)
554 {
555 if (verbose)
556 EMSG2(_(e_loadlib), libname);
557 return FAIL;
558 }
559
560 for (i = 0; ruby_funcname_table[i].ptr; ++i)
561 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200562 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 ruby_funcname_table[i].name)))
564 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200565 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200566 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 if (verbose)
568 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
569 return FAIL;
570 }
571 }
572 return OK;
573}
574
575/*
576 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
577 * else FALSE.
578 */
579 int
580ruby_enabled(verbose)
581 int verbose;
582{
583 return ruby_runtime_link_init(DYNAMIC_RUBY_DLL, verbose) == OK;
584}
585#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
586
587 void
588ruby_end()
589{
590#ifdef DYNAMIC_RUBY
591 end_dynamic_ruby();
592#endif
593}
594
595void ex_ruby(exarg_T *eap)
596{
597 int state;
598 char *script = NULL;
599
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000600 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 if (!eap->skip && ensure_ruby_initialized())
602 {
603 if (script == NULL)
604 rb_eval_string_protect((char *)eap->arg, &state);
605 else
606 rb_eval_string_protect(script, &state);
607 if (state)
608 error_print(state);
609 }
610 vim_free(script);
611}
612
Bram Moolenaar165641d2010-02-17 16:23:09 +0100613/*
614 * In Ruby 1.9 or later, ruby String object has encoding.
615 * conversion buffer string of vim to ruby String object using
616 * VIM encoding option.
617 */
618 static VALUE
619vim_str2rb_enc_str(const char *s)
620{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100621#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100622 int isnum;
623 long lval;
624 char_u *sval;
625 rb_encoding *enc;
626
627 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
628 if (isnum == 0)
629 {
630 enc = rb_enc_find((char *)sval);
631 vim_free(sval);
632 if (enc) {
633 return rb_enc_str_new(s, strlen(s), enc);
634 }
635 }
636#endif
637 return rb_str_new2(s);
638}
639
640 static VALUE
641eval_enc_string_protect(const char *str, int *state)
642{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100643#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100644 int isnum;
645 long lval;
646 char_u *sval;
647 rb_encoding *enc;
648 VALUE v;
649
650 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
651 if (isnum == 0)
652 {
653 enc = rb_enc_find((char *)sval);
654 vim_free(sval);
655 if (enc)
656 {
657 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
658 return rb_eval_string_protect(StringValuePtr(v), state);
659 }
660 }
661#endif
662 return rb_eval_string_protect(str, state);
663}
664
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665void ex_rubydo(exarg_T *eap)
666{
667 int state;
668 linenr_T i;
669
670 if (ensure_ruby_initialized())
671 {
672 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
673 return;
674 for (i = eap->line1; i <= eap->line2; i++) {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100675 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100677 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100679 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 if (state) {
681 error_print(state);
682 break;
683 }
684 line = rb_lastline_get();
685 if (!NIL_P(line)) {
686 if (TYPE(line) != T_STRING) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000687 EMSG(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 return;
689 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100690 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 changed();
692#ifdef SYNTAX_HL
693 syn_changed(i); /* recompute syntax hl. for this line */
694#endif
695 }
696 }
697 check_cursor();
698 update_curbuf(NOT_VALID);
699 }
700}
701
702void ex_rubyfile(exarg_T *eap)
703{
704 int state;
705
706 if (ensure_ruby_initialized())
707 {
708 rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
709 if (state) error_print(state);
710 }
711}
712
713void ruby_buffer_free(buf_T *buf)
714{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000715 if (buf->b_ruby_ref)
716 {
717 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
718 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 }
720}
721
722void ruby_window_free(win_T *win)
723{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000724 if (win->w_ruby_ref)
725 {
726 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
727 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 }
729}
730
731static int ensure_ruby_initialized(void)
732{
733 if (!ruby_initialized)
734 {
735#ifdef DYNAMIC_RUBY
736 if (ruby_enabled(TRUE))
737 {
738#endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100739#ifdef _WIN32
740 /* suggested by Ariya Mizutani */
741 int argc = 1;
742 char *argv[] = {"gvim.exe"};
743 NtInitialize(&argc, &argv);
744#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200745 {
Bram Moolenaar10f3a792013-05-20 12:52:29 +0200746#if defined(RUBY19_OR_LATER) || defined(RUBY_INIT_STACK)
Bram Moolenaar99685e62013-05-11 13:56:18 +0200747 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100748#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200749 ruby_init();
750 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100751#ifdef RUBY19_OR_LATER
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100752 {
753 int dummy_argc = 2;
754 char *dummy_argv[] = {"vim-ruby", "-e0"};
755 ruby_process_options(dummy_argc, dummy_argv);
756 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100757 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100758#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100760#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100761 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 ruby_vim_init();
763 ruby_initialized = 1;
764#ifdef DYNAMIC_RUBY
765 }
766 else
767 {
768 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
769 return 0;
770 }
771#endif
772 }
773 return ruby_initialized;
774}
775
776static void error_print(int state)
777{
778#ifndef DYNAMIC_RUBY
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100779#if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
780 && !(defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 RUBYEXTERN VALUE ruby_errinfo;
782#endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 VALUE eclass;
785 VALUE einfo;
786 char buff[BUFSIZ];
787
788#define TAG_RETURN 0x1
789#define TAG_BREAK 0x2
790#define TAG_NEXT 0x3
791#define TAG_RETRY 0x4
792#define TAG_REDO 0x5
793#define TAG_RAISE 0x6
794#define TAG_THROW 0x7
795#define TAG_FATAL 0x8
796#define TAG_MASK 0xf
797
798 switch (state) {
799 case TAG_RETURN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000800 EMSG(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 break;
802 case TAG_NEXT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000803 EMSG(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 break;
805 case TAG_BREAK:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000806 EMSG(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 break;
808 case TAG_REDO:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000809 EMSG(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 break;
811 case TAG_RETRY:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000812 EMSG(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 break;
814 case TAG_RAISE:
815 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +0100816#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100817 eclass = CLASS_OF(rb_errinfo());
818 einfo = rb_obj_as_string(rb_errinfo());
819#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 eclass = CLASS_OF(ruby_errinfo);
821 einfo = rb_obj_as_string(ruby_errinfo);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100822#endif
823 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000824 EMSG(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 }
826 else {
827 VALUE epath;
828 char *p;
829
830 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000831 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar165641d2010-02-17 16:23:09 +0100832 RSTRING_PTR(epath), RSTRING_PTR(einfo));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 p = strchr(buff, '\n');
834 if (p) *p = '\0';
835 EMSG(buff);
836 }
837 break;
838 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000839 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 EMSG(buff);
841 break;
842 }
843}
844
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000845static VALUE vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846{
847 char *buff, *p;
848
849 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +0200850 if (RSTRING_LEN(str) > 0)
851 {
852 /* Only do this when the string isn't empty, alloc(0) causes trouble. */
853 buff = ALLOCA_N(char, RSTRING_LEN(str));
854 strcpy(buff, RSTRING_PTR(str));
855 p = strchr(buff, '\n');
856 if (p) *p = '\0';
857 MSG(buff);
858 }
859 else
860 {
861 MSG("");
862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 return Qnil;
864}
865
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000866static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100868 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 update_screen(NOT_VALID);
870 return Qnil;
871}
872
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000873static VALUE vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100875 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 return Qnil;
877}
878
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100879#ifdef FEAT_EVAL
880static VALUE vim_to_ruby(typval_T *tv)
881{
882 VALUE result = Qnil;
883
884 if (tv->v_type == VAR_STRING)
885 {
Bram Moolenaar94127e42010-03-19 23:08:48 +0100886 result = rb_str_new2(tv->vval.v_string == NULL
887 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100888 }
889 else if (tv->v_type == VAR_NUMBER)
890 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100891 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100892 }
893# ifdef FEAT_FLOAT
894 else if (tv->v_type == VAR_FLOAT)
895 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100896 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100897 }
898# endif
899 else if (tv->v_type == VAR_LIST)
900 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100901 list_T *list = tv->vval.v_list;
902 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100903
Bram Moolenaar639a2552010-03-17 18:15:23 +0100904 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100905
Bram Moolenaar639a2552010-03-17 18:15:23 +0100906 if (list != NULL)
907 {
908 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
909 {
910 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
911 }
912 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100913 }
914 else if (tv->v_type == VAR_DICT)
915 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100916 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100917
Bram Moolenaar639a2552010-03-17 18:15:23 +0100918 if (tv->vval.v_dict != NULL)
919 {
920 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
921 long_u todo = ht->ht_used;
922 hashitem_T *hi;
923 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100924
Bram Moolenaar639a2552010-03-17 18:15:23 +0100925 for (hi = ht->ht_array; todo > 0; ++hi)
926 {
927 if (!HASHITEM_EMPTY(hi))
928 {
929 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100930
Bram Moolenaar639a2552010-03-17 18:15:23 +0100931 di = dict_lookup(hi);
932 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100933 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +0100934 }
935 }
936 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100937 } /* else return Qnil; */
938
939 return result;
940}
941#endif
942
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000943static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944{
945#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100946 typval_T *tv;
947 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100949 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
950 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100952 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100954 result = vim_to_ruby(tv);
955
956 free_tv(tv);
957
958 return result;
959#else
960 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962}
963
964static VALUE buffer_new(buf_T *buf)
965{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000966 if (buf->b_ruby_ref)
967 {
968 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 }
Bram Moolenaare344bea2005-09-01 20:46:49 +0000970 else
971 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000973 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
975 return obj;
976 }
977}
978
979static buf_T *get_buf(VALUE obj)
980{
981 buf_T *buf;
982
983 Data_Get_Struct(obj, buf_T, buf);
984 if (buf == NULL)
985 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
986 return buf;
987}
988
989static VALUE buffer_s_current()
990{
991 return buffer_new(curbuf);
992}
993
994static VALUE buffer_s_count()
995{
996 buf_T *b;
997 int n = 0;
998
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000999 for (b = firstbuf; b != NULL; b = b->b_next)
1000 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001001 /* Deleted buffers should not be counted
1002 * SegPhault - 01/07/05 */
1003 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001004 n++;
1005 }
1006
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 return INT2NUM(n);
1008}
1009
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001010static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011{
1012 buf_T *b;
1013 int n = NUM2INT(num);
1014
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001015 for (b = firstbuf; b != NULL; b = b->b_next)
1016 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001017 /* Deleted buffers should not be counted
1018 * SegPhault - 01/07/05 */
1019 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001020 continue;
1021
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001022 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001024
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001025 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 }
1027 return Qnil;
1028}
1029
1030static VALUE buffer_name(VALUE self)
1031{
1032 buf_T *buf = get_buf(self);
1033
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001034 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035}
1036
1037static VALUE buffer_number(VALUE self)
1038{
1039 buf_T *buf = get_buf(self);
1040
1041 return INT2NUM(buf->b_fnum);
1042}
1043
1044static VALUE buffer_count(VALUE self)
1045{
1046 buf_T *buf = get_buf(self);
1047
1048 return INT2NUM(buf->b_ml.ml_line_count);
1049}
1050
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001051static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001053 if (n <= 0 || n > buf->b_ml.ml_line_count)
1054 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1055 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056}
1057
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001058static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059{
1060 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001061
1062 if (buf != NULL)
1063 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
1064 return Qnil; /* For stop warning */
1065}
1066
1067static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
1068{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001069 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001070 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001072 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1073 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001074 /* set curwin/curbuf for "buf" and save some things */
1075 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001076
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 if (u_savesub(n) == OK) {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001078 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 changed();
1080#ifdef SYNTAX_HL
1081 syn_changed(n); /* recompute syntax hl. for this line */
1082#endif
1083 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001084
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001085 /* restore curwin/curbuf and a few other things */
1086 aucmd_restbuf(&aco);
1087 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001088
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 update_curbuf(NOT_VALID);
1090 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001091 else
1092 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001093 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 }
1095 return str;
1096}
1097
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001098static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
1099{
1100 buf_T *buf = get_buf(self);
1101
1102 if (buf != NULL)
1103 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1104 return str;
1105}
1106
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107static VALUE buffer_delete(VALUE self, VALUE num)
1108{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001109 buf_T *buf = get_buf(self);
1110 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001111 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001113 if (n > 0 && n <= buf->b_ml.ml_line_count)
1114 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001115 /* set curwin/curbuf for "buf" and save some things */
1116 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001117
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 if (u_savedel(n, 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001120
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001121 /* Changes to non-active buffers should properly refresh
1122 * SegPhault - 01/09/05 */
1123 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001124
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 changed();
1126 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001127
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001128 /* restore curwin/curbuf and a few other things */
1129 aucmd_restbuf(&aco);
1130 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001131
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 update_curbuf(NOT_VALID);
1133 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001134 else
1135 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001136 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 }
1138 return Qnil;
1139}
1140
1141static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
1142{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001143 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001144 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001145 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001146 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147
Bram Moolenaar3c531602010-11-16 14:46:19 +01001148 if (line == NULL)
1149 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001150 rb_raise(rb_eIndexError, "NULL line");
1151 }
1152 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001153 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001154 /* set curwin/curbuf for "buf" and save some things */
1155 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001156
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 if (u_inssub(n + 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001159
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001160 /* Changes to non-active buffers should properly refresh screen
1161 * SegPhault - 12/20/04 */
1162 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001163
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001164 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001166
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001167 /* restore curwin/curbuf and a few other things */
1168 aucmd_restbuf(&aco);
1169 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001170
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 update_curbuf(NOT_VALID);
1172 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001173 else
1174 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001175 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 }
1177 return str;
1178}
1179
1180static VALUE window_new(win_T *win)
1181{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001182 if (win->w_ruby_ref)
1183 {
1184 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001186 else
1187 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001189 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1191 return obj;
1192 }
1193}
1194
1195static win_T *get_win(VALUE obj)
1196{
1197 win_T *win;
1198
1199 Data_Get_Struct(obj, win_T, win);
1200 if (win == NULL)
1201 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1202 return win;
1203}
1204
1205static VALUE window_s_current()
1206{
1207 return window_new(curwin);
1208}
1209
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001210/*
1211 * Added line manipulation functions
1212 * SegPhault - 03/07/05
1213 */
1214static VALUE line_s_current()
1215{
1216 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1217}
1218
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001219static VALUE set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001220{
1221 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1222}
1223
1224static VALUE current_line_number()
1225{
1226 return INT2FIX((int)curwin->w_cursor.lnum);
1227}
1228
1229
1230
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231static VALUE window_s_count()
1232{
1233#ifdef FEAT_WINDOWS
1234 win_T *w;
1235 int n = 0;
1236
Bram Moolenaarf740b292006-02-16 22:11:02 +00001237 for (w = firstwin; w != NULL; w = w->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 n++;
1239 return INT2NUM(n);
1240#else
1241 return INT2NUM(1);
1242#endif
1243}
1244
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001245static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246{
1247 win_T *w;
1248 int n = NUM2INT(num);
1249
1250#ifndef FEAT_WINDOWS
1251 w = curwin;
1252#else
1253 for (w = firstwin; w != NULL; w = w->w_next, --n)
1254#endif
1255 if (n == 0)
1256 return window_new(w);
1257 return Qnil;
1258}
1259
1260static VALUE window_buffer(VALUE self)
1261{
1262 win_T *win = get_win(self);
1263
1264 return buffer_new(win->w_buffer);
1265}
1266
1267static VALUE window_height(VALUE self)
1268{
1269 win_T *win = get_win(self);
1270
1271 return INT2NUM(win->w_height);
1272}
1273
1274static VALUE window_set_height(VALUE self, VALUE height)
1275{
1276 win_T *win = get_win(self);
1277 win_T *savewin = curwin;
1278
1279 curwin = win;
1280 win_setheight(NUM2INT(height));
1281 curwin = savewin;
1282 return height;
1283}
1284
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001285static VALUE window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001286{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001287 return INT2NUM(W_WIDTH(get_win(self)));
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001288}
1289
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001290static VALUE window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001291{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001292#ifdef FEAT_VERTSPLIT
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001293 win_T *win = get_win(self);
1294 win_T *savewin = curwin;
1295
1296 curwin = win;
1297 win_setwidth(NUM2INT(width));
1298 curwin = savewin;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001299#endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001300 return width;
1301}
1302
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303static VALUE window_cursor(VALUE self)
1304{
1305 win_T *win = get_win(self);
1306
1307 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1308}
1309
1310static VALUE window_set_cursor(VALUE self, VALUE pos)
1311{
1312 VALUE lnum, col;
1313 win_T *win = get_win(self);
1314
1315 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001316 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001318 lnum = RARRAY_PTR(pos)[0];
1319 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 win->w_cursor.lnum = NUM2LONG(lnum);
1321 win->w_cursor.col = NUM2UINT(col);
1322 check_cursor(); /* put cursor on an existing line */
1323 update_screen(NOT_VALID);
1324 return Qnil;
1325}
1326
Bram Moolenaar6217cdc2012-04-25 12:28:09 +02001327static VALUE f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001328{
1329 return Qnil;
1330}
1331
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001332static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333{
1334 int i;
1335 VALUE str = rb_str_new("", 0);
1336
1337 for (i = 0; i < argc; i++) {
1338 if (i > 0) rb_str_cat(str, ", ", 2);
1339 rb_str_concat(str, rb_inspect(argv[i]));
1340 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001341 MSG(RSTRING_PTR(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 return Qnil;
1343}
1344
1345static void ruby_io_init(void)
1346{
1347#ifndef DYNAMIC_RUBY
1348 RUBYEXTERN VALUE rb_stdout;
1349#endif
1350
1351 rb_stdout = rb_obj_alloc(rb_cObject);
1352 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001353 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 rb_define_global_function("p", f_p, -1);
1355}
1356
1357static void ruby_vim_init(void)
1358{
1359 objtbl = rb_hash_new();
1360 rb_global_variable(&objtbl);
1361
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001362 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001363 * alias "VIM" for backwards compatibility. */
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001364 mVIM = rb_define_module("Vim");
1365 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1367 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1368 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1369 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1370 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1371 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1372 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1373 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1374 rb_define_module_function(mVIM, "message", vim_message, 1);
1375 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1376 rb_define_module_function(mVIM, "command", vim_command, 1);
1377 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
1378
1379 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1380 rb_eStandardError);
1381 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1382 rb_eStandardError);
1383
1384 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1385 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1386 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1387 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1388 rb_define_method(cBuffer, "name", buffer_name, 0);
1389 rb_define_method(cBuffer, "number", buffer_number, 0);
1390 rb_define_method(cBuffer, "count", buffer_count, 0);
1391 rb_define_method(cBuffer, "length", buffer_count, 0);
1392 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1393 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1394 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1395 rb_define_method(cBuffer, "append", buffer_append, 2);
1396
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001397 /* Added line manipulation functions
1398 * SegPhault - 03/07/05 */
1399 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1400 rb_define_method(cBuffer, "line", line_s_current, 0);
1401 rb_define_method(cBuffer, "line=", set_current_line, 1);
1402
1403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1405 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1406 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1407 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1408 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1409 rb_define_method(cVimWindow, "height", window_height, 0);
1410 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001411 rb_define_method(cVimWindow, "width", window_width, 0);
1412 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1414 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1415
1416 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1417 rb_define_virtual_variable("$curwin", window_s_current, 0);
1418}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001419
1420void vim_ruby_init(void *stack_start)
1421{
1422 /* should get machine stack start address early in main function */
1423 ruby_stack_start = stack_start;
1424}