blob: da4b8b2749069328a060c17777ce7ba26a2bf782 [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 Moolenaar3b9abb62013-05-12 14:11:17 +0200161#if defined(__ia64) && !defined(ruby_init_stack)
162# define ruby_init_stack(addr) ruby_init_stack((addr), rb_ia64_bsp())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#endif
164
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200165#if defined(DYNAMIC_RUBY) || defined(PROTO)
166# ifdef PROTO
167# define HINSTANCE int /* for generating prototypes */
168# endif
169
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170/*
171 * Wrapper defines
172 */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200173# define rb_assoc_new dll_rb_assoc_new
174# define rb_cObject (*dll_rb_cObject)
175# define rb_check_type dll_rb_check_type
176# define rb_class_path dll_rb_class_path
177# define rb_data_object_alloc dll_rb_data_object_alloc
178# define rb_define_class_under dll_rb_define_class_under
179# define rb_define_const dll_rb_define_const
180# define rb_define_global_function dll_rb_define_global_function
181# define rb_define_method dll_rb_define_method
182# define rb_define_module dll_rb_define_module
183# define rb_define_module_function dll_rb_define_module_function
184# define rb_define_singleton_method dll_rb_define_singleton_method
185# define rb_define_virtual_variable dll_rb_define_virtual_variable
186# define rb_stdout (*dll_rb_stdout)
187# define rb_eArgError (*dll_rb_eArgError)
188# define rb_eIndexError (*dll_rb_eIndexError)
189# define rb_eRuntimeError (*dll_rb_eRuntimeError)
190# define rb_eStandardError (*dll_rb_eStandardError)
191# define rb_eval_string_protect dll_rb_eval_string_protect
192# define rb_global_variable dll_rb_global_variable
193# define rb_hash_aset dll_rb_hash_aset
194# define rb_hash_new dll_rb_hash_new
195# define rb_inspect dll_rb_inspect
196# define rb_int2inum dll_rb_int2inum
197# if SIZEOF_INT < SIZEOF_LONG /* 64 bits only */
198# define rb_fix2int dll_rb_fix2int
199# define rb_num2int dll_rb_num2int
200# define rb_num2uint dll_rb_num2uint
201# endif
202# define rb_lastline_get dll_rb_lastline_get
203# define rb_lastline_set dll_rb_lastline_set
204# define rb_load_protect dll_rb_load_protect
205# ifndef RUBY19_OR_LATER
206# define rb_num2long dll_rb_num2long
207# endif
208# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 19
209# define rb_num2ulong dll_rb_num2ulong
210# endif
211# define rb_obj_alloc dll_rb_obj_alloc
212# define rb_obj_as_string dll_rb_obj_as_string
213# define rb_obj_id dll_rb_obj_id
214# define rb_raise dll_rb_raise
215# define rb_str_cat dll_rb_str_cat
216# define rb_str_concat dll_rb_str_concat
217# define rb_str_new dll_rb_str_new
218# ifdef rb_str_new2
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200219/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200220# define need_rb_str_new_cstr 1
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200221/* Ruby's headers #define rb_str_new_cstr to make use of GCC's
222 * __builtin_constant_p extension. */
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200223# undef rb_str_new_cstr
224# define rb_str_new_cstr dll_rb_str_new_cstr
Bram Moolenaar76a86062013-05-11 17:45:48 +0200225# else
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200226# define rb_str_new2 dll_rb_str_new2
Bram Moolenaar76a86062013-05-11 17:45:48 +0200227# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200228# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200229# define rb_string_value dll_rb_string_value
230# define rb_string_value_ptr dll_rb_string_value_ptr
231# define rb_float_new dll_rb_float_new
232# define rb_ary_new dll_rb_ary_new
233# define rb_ary_push dll_rb_ary_push
234# ifdef __ia64
235# define rb_ia64_bsp dll_rb_ia64_bsp
236# undef ruby_init_stack
237# define ruby_init_stack(addr) dll_ruby_init_stack((addr), rb_ia64_bsp())
238# else
239# define ruby_init_stack dll_ruby_init_stack
240# endif
241# else
242# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200243# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200244# ifdef RUBY19_OR_LATER
245# define rb_errinfo dll_rb_errinfo
246# else
247# define ruby_errinfo (*dll_ruby_errinfo)
248# endif
249# define ruby_init dll_ruby_init
250# define ruby_init_loadpath dll_ruby_init_loadpath
251# ifdef WIN3264
252# define NtInitialize dll_NtInitialize
253# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
254# define rb_w32_snprintf dll_rb_w32_snprintf
255# endif
256# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200258# ifdef RUBY19_OR_LATER
259# define ruby_script dll_ruby_script
260# define rb_enc_find_index dll_rb_enc_find_index
261# define rb_enc_find dll_rb_enc_find
262# define rb_enc_str_new dll_rb_enc_str_new
263# define rb_sprintf dll_rb_sprintf
264# define rb_require dll_rb_require
265# define ruby_process_options dll_ruby_process_options
266# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100267
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268/*
269 * Pointers for dynamic link
270 */
271static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200272VALUE *dll_rb_cFalseClass;
273VALUE *dll_rb_cFixnum;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200274# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100275VALUE *dll_rb_cFloat;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200276# endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200277VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278static VALUE *dll_rb_cObject;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200279VALUE *dll_rb_cSymbol;
280VALUE *dll_rb_cTrueClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281static void (*dll_rb_check_type) (VALUE,int);
282static VALUE (*dll_rb_class_path) (VALUE);
283static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
284static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
285static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
286static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
287static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
288static VALUE (*dll_rb_define_module) (const char*);
289static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
290static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
291static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
292static VALUE *dll_rb_stdout;
293static VALUE *dll_rb_eArgError;
294static VALUE *dll_rb_eIndexError;
295static VALUE *dll_rb_eRuntimeError;
296static VALUE *dll_rb_eStandardError;
297static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
298static void (*dll_rb_global_variable) (VALUE*);
299static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
300static VALUE (*dll_rb_hash_new) (void);
301static VALUE (*dll_rb_inspect) (VALUE);
302static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200303# if SIZEOF_INT < SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200304static long (*dll_rb_fix2int) (VALUE);
305static long (*dll_rb_num2int) (VALUE);
306static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200307# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308static VALUE (*dll_rb_lastline_get) (void);
309static void (*dll_rb_lastline_set) (VALUE);
310static void (*dll_rb_load_protect) (VALUE, int, int*);
311static long (*dll_rb_num2long) (VALUE);
312static unsigned long (*dll_rb_num2ulong) (VALUE);
313static VALUE (*dll_rb_obj_alloc) (VALUE);
314static VALUE (*dll_rb_obj_as_string) (VALUE);
315static VALUE (*dll_rb_obj_id) (VALUE);
316static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200317# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200318static VALUE (*dll_rb_string_value) (volatile VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200319# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200321# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
323static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
324static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200325# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200326/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
327static VALUE (*dll_rb_str_new_cstr) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200328# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200330# endif
331# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100332static VALUE (*dll_rb_errinfo) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200333# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334static VALUE *dll_ruby_errinfo;
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200335# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336static void (*dll_ruby_init) (void);
337static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200338# ifdef WIN3264
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100339static void (*dll_NtInitialize) (int*, char***);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200340# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200341static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200342# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200343# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200344# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100345static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
346static VALUE (*dll_rb_float_new) (double);
347static VALUE (*dll_rb_ary_new) (void);
348static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200349# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200350static void * (*dll_rb_ia64_bsp) (void);
351static void (*dll_ruby_init_stack)(VALUE*, void*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200352# else
Bram Moolenaar76a86062013-05-11 17:45:48 +0200353static void (*dll_ruby_init_stack)(VALUE*);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200354# endif
Bram Moolenaar76a86062013-05-11 17:45:48 +0200355# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200356# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100357static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200358# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200360# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100361static void (*dll_ruby_script) (const char*);
362static int (*dll_rb_enc_find_index) (const char*);
363static rb_encoding* (*dll_rb_enc_find) (const char*);
364static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
365static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100366static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100367static void* (*ruby_process_options)(int, char**);
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200368# endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100369
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200370# if defined(RUBY19_OR_LATER) && !defined(PROTO)
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100371SIGNED_VALUE rb_num2long_stub(VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100372{
373 return dll_rb_num2long(x);
374}
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100375VALUE rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100376{
377 return dll_rb_int2big(x);
378}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200379# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20 \
Bram Moolenaar0bcdd6e2013-04-14 16:19:03 +0200380 && SIZEOF_INT < SIZEOF_LONG
381long rb_fix2int_stub(VALUE x)
382{
383 return dll_rb_fix2int(x);
384}
385long rb_num2int_stub(VALUE x)
386{
387 return dll_rb_num2int(x);
388}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200389# endif
390# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaar886ed692013-02-26 13:41:35 +0100391VALUE
392rb_float_new_in_heap(double d)
393{
394 return dll_rb_float_new(d);
395}
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100396VALUE rb_num2ulong(VALUE x)
Bram Moolenaar886ed692013-02-26 13:41:35 +0100397{
398 return (long)RSHIFT((SIGNED_VALUE)(x),1);
399}
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200400# endif
401# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100402
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200403static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404
405/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000406 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408static struct
409{
410 char *name;
411 RUBY_PROC *ptr;
412} ruby_funcname_table[] =
413{
414 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
415 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
416 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200417# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100418 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200419# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
421 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
422 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
423 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
424 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
425 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
426 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
427 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
428 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
429 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
430 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
431 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
432 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
433 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
434 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
435 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
436 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
437 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
438 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
439 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
440 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
441 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
442 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
443 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
444 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
445 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200446# if SIZEOF_INT < SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200447 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
448 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
449 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200450# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
452 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
453 {"rb_load_protect", (RUBY_PROC*)&dll_rb_load_protect},
454 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
455 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
456 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
457 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
458 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
459 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200460# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200461 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200462# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200464# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
466 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
467 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200468# ifdef need_rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200469 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200470# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200472# endif
473# ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100474 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200475# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200477# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
479 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200480# ifdef WIN3264
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100481 {
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200482# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER < 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100483 "NtInitialize",
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200484# else
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100485 "ruby_sysinit",
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200486# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100487 (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200488# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200490# endif
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200491# endif
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200492# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100493 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200494# ifdef __ia64
Bram Moolenaar76a86062013-05-11 17:45:48 +0200495 {"rb_ia64_bsp", (RUBY_PROC*)&dll_rb_ia64_bsp},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200496# endif
Bram Moolenaar99685e62013-05-11 13:56:18 +0200497 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200498# if DYNAMIC_RUBY_VER <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100499 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200500# else
Bram Moolenaar886ed692013-02-26 13:41:35 +0100501 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200502# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100503 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
504 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200505# endif
506# ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100507 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100508 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
509 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
510 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
511 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
512 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100513 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100514 {"ruby_process_options", (RUBY_PROC*)&dll_ruby_process_options},
Bram Moolenaar3b9abb62013-05-12 14:11:17 +0200515# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 {"", NULL},
517};
518
519/*
520 * Free ruby.dll
521 */
522 static void
523end_dynamic_ruby()
524{
525 if (hinstRuby)
526 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200527 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200528 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 }
530}
531
532/*
533 * Load library and get all pointers.
534 * Parameter 'libname' provides name of DLL.
535 * Return OK or FAIL.
536 */
537 static int
538ruby_runtime_link_init(char *libname, int verbose)
539{
540 int i;
541
542 if (hinstRuby)
543 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200544 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 if (!hinstRuby)
546 {
547 if (verbose)
548 EMSG2(_(e_loadlib), libname);
549 return FAIL;
550 }
551
552 for (i = 0; ruby_funcname_table[i].ptr; ++i)
553 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200554 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 ruby_funcname_table[i].name)))
556 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200557 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200558 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 if (verbose)
560 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
561 return FAIL;
562 }
563 }
564 return OK;
565}
566
567/*
568 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
569 * else FALSE.
570 */
571 int
572ruby_enabled(verbose)
573 int verbose;
574{
575 return ruby_runtime_link_init(DYNAMIC_RUBY_DLL, verbose) == OK;
576}
577#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
578
579 void
580ruby_end()
581{
582#ifdef DYNAMIC_RUBY
583 end_dynamic_ruby();
584#endif
585}
586
587void ex_ruby(exarg_T *eap)
588{
589 int state;
590 char *script = NULL;
591
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000592 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 if (!eap->skip && ensure_ruby_initialized())
594 {
595 if (script == NULL)
596 rb_eval_string_protect((char *)eap->arg, &state);
597 else
598 rb_eval_string_protect(script, &state);
599 if (state)
600 error_print(state);
601 }
602 vim_free(script);
603}
604
Bram Moolenaar165641d2010-02-17 16:23:09 +0100605/*
606 * In Ruby 1.9 or later, ruby String object has encoding.
607 * conversion buffer string of vim to ruby String object using
608 * VIM encoding option.
609 */
610 static VALUE
611vim_str2rb_enc_str(const char *s)
612{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100613#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100614 int isnum;
615 long lval;
616 char_u *sval;
617 rb_encoding *enc;
618
619 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
620 if (isnum == 0)
621 {
622 enc = rb_enc_find((char *)sval);
623 vim_free(sval);
624 if (enc) {
625 return rb_enc_str_new(s, strlen(s), enc);
626 }
627 }
628#endif
629 return rb_str_new2(s);
630}
631
632 static VALUE
633eval_enc_string_protect(const char *str, int *state)
634{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100635#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100636 int isnum;
637 long lval;
638 char_u *sval;
639 rb_encoding *enc;
640 VALUE v;
641
642 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
643 if (isnum == 0)
644 {
645 enc = rb_enc_find((char *)sval);
646 vim_free(sval);
647 if (enc)
648 {
649 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
650 return rb_eval_string_protect(StringValuePtr(v), state);
651 }
652 }
653#endif
654 return rb_eval_string_protect(str, state);
655}
656
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657void ex_rubydo(exarg_T *eap)
658{
659 int state;
660 linenr_T i;
661
662 if (ensure_ruby_initialized())
663 {
664 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
665 return;
666 for (i = eap->line1; i <= eap->line2; i++) {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100667 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100669 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100671 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 if (state) {
673 error_print(state);
674 break;
675 }
676 line = rb_lastline_get();
677 if (!NIL_P(line)) {
678 if (TYPE(line) != T_STRING) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000679 EMSG(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 return;
681 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100682 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 changed();
684#ifdef SYNTAX_HL
685 syn_changed(i); /* recompute syntax hl. for this line */
686#endif
687 }
688 }
689 check_cursor();
690 update_curbuf(NOT_VALID);
691 }
692}
693
694void ex_rubyfile(exarg_T *eap)
695{
696 int state;
697
698 if (ensure_ruby_initialized())
699 {
700 rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
701 if (state) error_print(state);
702 }
703}
704
705void ruby_buffer_free(buf_T *buf)
706{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000707 if (buf->b_ruby_ref)
708 {
709 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
710 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 }
712}
713
714void ruby_window_free(win_T *win)
715{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000716 if (win->w_ruby_ref)
717 {
718 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
719 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 }
721}
722
723static int ensure_ruby_initialized(void)
724{
725 if (!ruby_initialized)
726 {
727#ifdef DYNAMIC_RUBY
728 if (ruby_enabled(TRUE))
729 {
730#endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100731#ifdef _WIN32
732 /* suggested by Ariya Mizutani */
733 int argc = 1;
734 char *argv[] = {"gvim.exe"};
735 NtInitialize(&argc, &argv);
736#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200737 {
Bram Moolenaar76a86062013-05-11 17:45:48 +0200738#if defined(RUBY_VERSION) && RUBY_VERSION >= 18
Bram Moolenaar99685e62013-05-11 13:56:18 +0200739 ruby_init_stack(ruby_stack_start);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100740#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200741 ruby_init();
742 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100743#ifdef RUBY19_OR_LATER
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100744 {
745 int dummy_argc = 2;
746 char *dummy_argv[] = {"vim-ruby", "-e0"};
747 ruby_process_options(dummy_argc, dummy_argv);
748 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100749 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100750#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100752#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100753 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 ruby_vim_init();
755 ruby_initialized = 1;
756#ifdef DYNAMIC_RUBY
757 }
758 else
759 {
760 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
761 return 0;
762 }
763#endif
764 }
765 return ruby_initialized;
766}
767
768static void error_print(int state)
769{
770#ifndef DYNAMIC_RUBY
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100771#if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
772 && !(defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 RUBYEXTERN VALUE ruby_errinfo;
774#endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 VALUE eclass;
777 VALUE einfo;
778 char buff[BUFSIZ];
779
780#define TAG_RETURN 0x1
781#define TAG_BREAK 0x2
782#define TAG_NEXT 0x3
783#define TAG_RETRY 0x4
784#define TAG_REDO 0x5
785#define TAG_RAISE 0x6
786#define TAG_THROW 0x7
787#define TAG_FATAL 0x8
788#define TAG_MASK 0xf
789
790 switch (state) {
791 case TAG_RETURN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000792 EMSG(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 break;
794 case TAG_NEXT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000795 EMSG(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 break;
797 case TAG_BREAK:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000798 EMSG(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 break;
800 case TAG_REDO:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000801 EMSG(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 break;
803 case TAG_RETRY:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000804 EMSG(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 break;
806 case TAG_RAISE:
807 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +0100808#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100809 eclass = CLASS_OF(rb_errinfo());
810 einfo = rb_obj_as_string(rb_errinfo());
811#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 eclass = CLASS_OF(ruby_errinfo);
813 einfo = rb_obj_as_string(ruby_errinfo);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100814#endif
815 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000816 EMSG(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 }
818 else {
819 VALUE epath;
820 char *p;
821
822 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000823 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar165641d2010-02-17 16:23:09 +0100824 RSTRING_PTR(epath), RSTRING_PTR(einfo));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 p = strchr(buff, '\n');
826 if (p) *p = '\0';
827 EMSG(buff);
828 }
829 break;
830 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000831 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 EMSG(buff);
833 break;
834 }
835}
836
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000837static VALUE vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838{
839 char *buff, *p;
840
841 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +0200842 if (RSTRING_LEN(str) > 0)
843 {
844 /* Only do this when the string isn't empty, alloc(0) causes trouble. */
845 buff = ALLOCA_N(char, RSTRING_LEN(str));
846 strcpy(buff, RSTRING_PTR(str));
847 p = strchr(buff, '\n');
848 if (p) *p = '\0';
849 MSG(buff);
850 }
851 else
852 {
853 MSG("");
854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 return Qnil;
856}
857
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000858static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100860 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 update_screen(NOT_VALID);
862 return Qnil;
863}
864
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000865static VALUE vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100867 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 return Qnil;
869}
870
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100871#ifdef FEAT_EVAL
872static VALUE vim_to_ruby(typval_T *tv)
873{
874 VALUE result = Qnil;
875
876 if (tv->v_type == VAR_STRING)
877 {
Bram Moolenaar94127e42010-03-19 23:08:48 +0100878 result = rb_str_new2(tv->vval.v_string == NULL
879 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100880 }
881 else if (tv->v_type == VAR_NUMBER)
882 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100883 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100884 }
885# ifdef FEAT_FLOAT
886 else if (tv->v_type == VAR_FLOAT)
887 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100888 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100889 }
890# endif
891 else if (tv->v_type == VAR_LIST)
892 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100893 list_T *list = tv->vval.v_list;
894 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100895
Bram Moolenaar639a2552010-03-17 18:15:23 +0100896 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100897
Bram Moolenaar639a2552010-03-17 18:15:23 +0100898 if (list != NULL)
899 {
900 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
901 {
902 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
903 }
904 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100905 }
906 else if (tv->v_type == VAR_DICT)
907 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100908 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100909
Bram Moolenaar639a2552010-03-17 18:15:23 +0100910 if (tv->vval.v_dict != NULL)
911 {
912 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
913 long_u todo = ht->ht_used;
914 hashitem_T *hi;
915 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100916
Bram Moolenaar639a2552010-03-17 18:15:23 +0100917 for (hi = ht->ht_array; todo > 0; ++hi)
918 {
919 if (!HASHITEM_EMPTY(hi))
920 {
921 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100922
Bram Moolenaar639a2552010-03-17 18:15:23 +0100923 di = dict_lookup(hi);
924 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100925 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +0100926 }
927 }
928 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100929 } /* else return Qnil; */
930
931 return result;
932}
933#endif
934
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000935static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936{
937#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100938 typval_T *tv;
939 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100941 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
942 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100944 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100946 result = vim_to_ruby(tv);
947
948 free_tv(tv);
949
950 return result;
951#else
952 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954}
955
956static VALUE buffer_new(buf_T *buf)
957{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000958 if (buf->b_ruby_ref)
959 {
960 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 }
Bram Moolenaare344bea2005-09-01 20:46:49 +0000962 else
963 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000965 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
967 return obj;
968 }
969}
970
971static buf_T *get_buf(VALUE obj)
972{
973 buf_T *buf;
974
975 Data_Get_Struct(obj, buf_T, buf);
976 if (buf == NULL)
977 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
978 return buf;
979}
980
981static VALUE buffer_s_current()
982{
983 return buffer_new(curbuf);
984}
985
986static VALUE buffer_s_count()
987{
988 buf_T *b;
989 int n = 0;
990
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000991 for (b = firstbuf; b != NULL; b = b->b_next)
992 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000993 /* Deleted buffers should not be counted
994 * SegPhault - 01/07/05 */
995 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000996 n++;
997 }
998
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 return INT2NUM(n);
1000}
1001
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001002static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003{
1004 buf_T *b;
1005 int n = NUM2INT(num);
1006
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001007 for (b = firstbuf; b != NULL; b = b->b_next)
1008 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001009 /* Deleted buffers should not be counted
1010 * SegPhault - 01/07/05 */
1011 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001012 continue;
1013
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001014 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001016
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001017 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 }
1019 return Qnil;
1020}
1021
1022static VALUE buffer_name(VALUE self)
1023{
1024 buf_T *buf = get_buf(self);
1025
Bram Moolenaar35a2e192006-03-13 22:07:11 +00001026 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027}
1028
1029static VALUE buffer_number(VALUE self)
1030{
1031 buf_T *buf = get_buf(self);
1032
1033 return INT2NUM(buf->b_fnum);
1034}
1035
1036static VALUE buffer_count(VALUE self)
1037{
1038 buf_T *buf = get_buf(self);
1039
1040 return INT2NUM(buf->b_ml.ml_line_count);
1041}
1042
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001043static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001045 if (n <= 0 || n > buf->b_ml.ml_line_count)
1046 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1047 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048}
1049
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001050static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051{
1052 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001053
1054 if (buf != NULL)
1055 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
1056 return Qnil; /* For stop warning */
1057}
1058
1059static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
1060{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001061 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001062 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001064 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1065 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001066 /* set curwin/curbuf for "buf" and save some things */
1067 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001068
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 if (u_savesub(n) == OK) {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001070 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 changed();
1072#ifdef SYNTAX_HL
1073 syn_changed(n); /* recompute syntax hl. for this line */
1074#endif
1075 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001076
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001077 /* restore curwin/curbuf and a few other things */
1078 aucmd_restbuf(&aco);
1079 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001080
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 update_curbuf(NOT_VALID);
1082 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001083 else
1084 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001085 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 }
1087 return str;
1088}
1089
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001090static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
1091{
1092 buf_T *buf = get_buf(self);
1093
1094 if (buf != NULL)
1095 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1096 return str;
1097}
1098
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099static VALUE buffer_delete(VALUE self, VALUE num)
1100{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001101 buf_T *buf = get_buf(self);
1102 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001103 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001105 if (n > 0 && n <= buf->b_ml.ml_line_count)
1106 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001107 /* set curwin/curbuf for "buf" and save some things */
1108 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001109
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 if (u_savedel(n, 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001112
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001113 /* Changes to non-active buffers should properly refresh
1114 * SegPhault - 01/09/05 */
1115 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001116
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 changed();
1118 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001119
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001120 /* restore curwin/curbuf and a few other things */
1121 aucmd_restbuf(&aco);
1122 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001123
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 update_curbuf(NOT_VALID);
1125 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001126 else
1127 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001128 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 }
1130 return Qnil;
1131}
1132
1133static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
1134{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001135 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001136 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001137 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001138 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139
Bram Moolenaar3c531602010-11-16 14:46:19 +01001140 if (line == NULL)
1141 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001142 rb_raise(rb_eIndexError, "NULL line");
1143 }
1144 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001145 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001146 /* set curwin/curbuf for "buf" and save some things */
1147 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001148
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 if (u_inssub(n + 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001151
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001152 /* Changes to non-active buffers should properly refresh screen
1153 * SegPhault - 12/20/04 */
1154 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001155
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001156 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001158
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001159 /* restore curwin/curbuf and a few other things */
1160 aucmd_restbuf(&aco);
1161 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001162
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 update_curbuf(NOT_VALID);
1164 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001165 else
1166 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001167 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 }
1169 return str;
1170}
1171
1172static VALUE window_new(win_T *win)
1173{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001174 if (win->w_ruby_ref)
1175 {
1176 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001178 else
1179 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001181 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1183 return obj;
1184 }
1185}
1186
1187static win_T *get_win(VALUE obj)
1188{
1189 win_T *win;
1190
1191 Data_Get_Struct(obj, win_T, win);
1192 if (win == NULL)
1193 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1194 return win;
1195}
1196
1197static VALUE window_s_current()
1198{
1199 return window_new(curwin);
1200}
1201
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001202/*
1203 * Added line manipulation functions
1204 * SegPhault - 03/07/05
1205 */
1206static VALUE line_s_current()
1207{
1208 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1209}
1210
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001211static VALUE set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001212{
1213 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1214}
1215
1216static VALUE current_line_number()
1217{
1218 return INT2FIX((int)curwin->w_cursor.lnum);
1219}
1220
1221
1222
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223static VALUE window_s_count()
1224{
1225#ifdef FEAT_WINDOWS
1226 win_T *w;
1227 int n = 0;
1228
Bram Moolenaarf740b292006-02-16 22:11:02 +00001229 for (w = firstwin; w != NULL; w = w->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 n++;
1231 return INT2NUM(n);
1232#else
1233 return INT2NUM(1);
1234#endif
1235}
1236
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001237static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238{
1239 win_T *w;
1240 int n = NUM2INT(num);
1241
1242#ifndef FEAT_WINDOWS
1243 w = curwin;
1244#else
1245 for (w = firstwin; w != NULL; w = w->w_next, --n)
1246#endif
1247 if (n == 0)
1248 return window_new(w);
1249 return Qnil;
1250}
1251
1252static VALUE window_buffer(VALUE self)
1253{
1254 win_T *win = get_win(self);
1255
1256 return buffer_new(win->w_buffer);
1257}
1258
1259static VALUE window_height(VALUE self)
1260{
1261 win_T *win = get_win(self);
1262
1263 return INT2NUM(win->w_height);
1264}
1265
1266static VALUE window_set_height(VALUE self, VALUE height)
1267{
1268 win_T *win = get_win(self);
1269 win_T *savewin = curwin;
1270
1271 curwin = win;
1272 win_setheight(NUM2INT(height));
1273 curwin = savewin;
1274 return height;
1275}
1276
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001277static VALUE window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001278{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001279 return INT2NUM(W_WIDTH(get_win(self)));
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001280}
1281
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001282static VALUE window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001283{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001284#ifdef FEAT_VERTSPLIT
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001285 win_T *win = get_win(self);
1286 win_T *savewin = curwin;
1287
1288 curwin = win;
1289 win_setwidth(NUM2INT(width));
1290 curwin = savewin;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001291#endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001292 return width;
1293}
1294
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295static VALUE window_cursor(VALUE self)
1296{
1297 win_T *win = get_win(self);
1298
1299 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1300}
1301
1302static VALUE window_set_cursor(VALUE self, VALUE pos)
1303{
1304 VALUE lnum, col;
1305 win_T *win = get_win(self);
1306
1307 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001308 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001310 lnum = RARRAY_PTR(pos)[0];
1311 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 win->w_cursor.lnum = NUM2LONG(lnum);
1313 win->w_cursor.col = NUM2UINT(col);
1314 check_cursor(); /* put cursor on an existing line */
1315 update_screen(NOT_VALID);
1316 return Qnil;
1317}
1318
Bram Moolenaar6217cdc2012-04-25 12:28:09 +02001319static VALUE f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001320{
1321 return Qnil;
1322}
1323
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001324static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325{
1326 int i;
1327 VALUE str = rb_str_new("", 0);
1328
1329 for (i = 0; i < argc; i++) {
1330 if (i > 0) rb_str_cat(str, ", ", 2);
1331 rb_str_concat(str, rb_inspect(argv[i]));
1332 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001333 MSG(RSTRING_PTR(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 return Qnil;
1335}
1336
1337static void ruby_io_init(void)
1338{
1339#ifndef DYNAMIC_RUBY
1340 RUBYEXTERN VALUE rb_stdout;
1341#endif
1342
1343 rb_stdout = rb_obj_alloc(rb_cObject);
1344 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001345 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 rb_define_global_function("p", f_p, -1);
1347}
1348
1349static void ruby_vim_init(void)
1350{
1351 objtbl = rb_hash_new();
1352 rb_global_variable(&objtbl);
1353
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001354 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001355 * alias "VIM" for backwards compatibility. */
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001356 mVIM = rb_define_module("Vim");
1357 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1359 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1360 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1361 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1362 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1363 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1364 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1365 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1366 rb_define_module_function(mVIM, "message", vim_message, 1);
1367 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1368 rb_define_module_function(mVIM, "command", vim_command, 1);
1369 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
1370
1371 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1372 rb_eStandardError);
1373 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1374 rb_eStandardError);
1375
1376 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1377 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1378 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1379 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1380 rb_define_method(cBuffer, "name", buffer_name, 0);
1381 rb_define_method(cBuffer, "number", buffer_number, 0);
1382 rb_define_method(cBuffer, "count", buffer_count, 0);
1383 rb_define_method(cBuffer, "length", buffer_count, 0);
1384 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1385 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1386 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1387 rb_define_method(cBuffer, "append", buffer_append, 2);
1388
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001389 /* Added line manipulation functions
1390 * SegPhault - 03/07/05 */
1391 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1392 rb_define_method(cBuffer, "line", line_s_current, 0);
1393 rb_define_method(cBuffer, "line=", set_current_line, 1);
1394
1395
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1397 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1398 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1399 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1400 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1401 rb_define_method(cVimWindow, "height", window_height, 0);
1402 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001403 rb_define_method(cVimWindow, "width", window_width, 0);
1404 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1406 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1407
1408 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1409 rb_define_virtual_variable("$curwin", window_s_current, 0);
1410}
Bram Moolenaar99685e62013-05-11 13:56:18 +02001411
1412void vim_ruby_init(void *stack_start)
1413{
1414 /* should get machine stack start address early in main function */
1415 ruby_stack_start = stack_start;
1416}