blob: d810d3770bb211d32f36dff0be48691f99d8596b [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 Moolenaar071d4272004-06-13 20:20:40 +000091#include <ruby.h>
Bram Moolenaar639a2552010-03-17 18:15:23 +010092#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +010093# include <ruby/encoding.h>
94#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000095
Bram Moolenaar7a8ef142010-12-24 13:39:35 +010096#undef off_t /* ruby defines off_t as _int64, Mingw uses long */
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#undef EXTERN
98#undef _
99
100/* T_DATA defined both by Ruby and Mac header files, hack around it... */
Bram Moolenaarcb635362007-05-12 13:02:42 +0000101#if defined(MACOS_X_UNIX) || defined(macintosh)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102# define __OPENTRANSPORT__
103# define __OPENTRANSPORTPROTOCOL__
104# define __OPENTRANSPORTPROVIDERS__
105#endif
106
Bram Moolenaar165641d2010-02-17 16:23:09 +0100107/*
108 * Backward compatiblity for Ruby 1.8 and earlier.
109 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
110 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
111 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
112 */
113#ifndef StringValuePtr
114# define StringValuePtr(s) STR2CSTR(s)
115#endif
116#ifndef RARRAY_LEN
117# define RARRAY_LEN(s) RARRAY(s)->len
118#endif
119#ifndef RARRAY_PTR
120# define RARRAY_PTR(s) RARRAY(s)->ptr
121#endif
122#ifndef RSTRING_LEN
123# define RSTRING_LEN(s) RSTRING(s)->len
124#endif
125#ifndef RSTRING_PTR
126# define RSTRING_PTR(s) RSTRING(s)->ptr
127#endif
128
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#include "vim.h"
130#include "version.h"
131
132#if defined(PROTO) && !defined(FEAT_RUBY)
133/* Define these to be able to generate the function prototypes. */
134# define VALUE int
135# define RUBY_DATA_FUNC int
136#endif
137
138static int ruby_initialized = 0;
139static VALUE objtbl;
140
141static VALUE mVIM;
142static VALUE cBuffer;
143static VALUE cVimWindow;
144static VALUE eDeletedBufferError;
145static VALUE eDeletedWindowError;
146
147static int ensure_ruby_initialized(void);
148static void error_print(int);
149static void ruby_io_init(void);
150static void ruby_vim_init(void);
151
152#if defined(DYNAMIC_RUBY) || defined(PROTO)
153#ifdef PROTO
154# define HINSTANCE int /* for generating prototypes */
155#endif
156
157/*
158 * Wrapper defines
159 */
160#define rb_assoc_new dll_rb_assoc_new
161#define rb_cObject (*dll_rb_cObject)
162#define rb_check_type dll_rb_check_type
163#define rb_class_path dll_rb_class_path
164#define rb_data_object_alloc dll_rb_data_object_alloc
165#define rb_define_class_under dll_rb_define_class_under
166#define rb_define_const dll_rb_define_const
167#define rb_define_global_function dll_rb_define_global_function
168#define rb_define_method dll_rb_define_method
169#define rb_define_module dll_rb_define_module
170#define rb_define_module_function dll_rb_define_module_function
171#define rb_define_singleton_method dll_rb_define_singleton_method
172#define rb_define_virtual_variable dll_rb_define_virtual_variable
173#define rb_stdout (*dll_rb_stdout)
174#define rb_eArgError (*dll_rb_eArgError)
175#define rb_eIndexError (*dll_rb_eIndexError)
176#define rb_eRuntimeError (*dll_rb_eRuntimeError)
177#define rb_eStandardError (*dll_rb_eStandardError)
178#define rb_eval_string_protect dll_rb_eval_string_protect
179#define rb_global_variable dll_rb_global_variable
180#define rb_hash_aset dll_rb_hash_aset
181#define rb_hash_new dll_rb_hash_new
182#define rb_inspect dll_rb_inspect
183#define rb_int2inum dll_rb_int2inum
Bram Moolenaarb213da02012-10-03 18:06:59 +0200184#if SIZEOF_INT < SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200185#define rb_fix2int dll_rb_fix2int
186#define rb_num2int dll_rb_num2int
187#define rb_num2uint dll_rb_num2uint
Bram Moolenaarb213da02012-10-03 18:06:59 +0200188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189#define rb_lastline_get dll_rb_lastline_get
190#define rb_lastline_set dll_rb_lastline_set
191#define rb_load_protect dll_rb_load_protect
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200192#ifndef RUBY19_OR_LATER
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193#define rb_num2long dll_rb_num2long
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200194#endif
Bram Moolenaar886ed692013-02-26 13:41:35 +0100195#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER <= 19
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196#define rb_num2ulong dll_rb_num2ulong
Bram Moolenaar886ed692013-02-26 13:41:35 +0100197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198#define rb_obj_alloc dll_rb_obj_alloc
199#define rb_obj_as_string dll_rb_obj_as_string
200#define rb_obj_id dll_rb_obj_id
201#define rb_raise dll_rb_raise
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202#define rb_str_cat dll_rb_str_cat
203#define rb_str_concat dll_rb_str_concat
204#define rb_str_new dll_rb_str_new
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200205#ifdef rb_str_new2
206/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
207# define need_rb_str_new_cstr 1
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200208/* Ruby's headers #define rb_str_new_cstr to make use of GCC's
209 * __builtin_constant_p extension. */
210# undef rb_str_new_cstr
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200211# define rb_str_new_cstr dll_rb_str_new_cstr
212#else
Bram Moolenaar218116c2010-05-20 21:46:00 +0200213# define rb_str_new2 dll_rb_str_new2
214#endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100215#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200216# define rb_string_value dll_rb_string_value
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100217# define rb_string_value_ptr dll_rb_string_value_ptr
218# define rb_float_new dll_rb_float_new
219# define rb_ary_new dll_rb_ary_new
220# define rb_ary_push dll_rb_ary_push
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200221#else
222# define rb_str2cstr dll_rb_str2cstr
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100223#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100224#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100225# define rb_errinfo dll_rb_errinfo
226#else
227# define ruby_errinfo (*dll_ruby_errinfo)
228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229#define ruby_init dll_ruby_init
230#define ruby_init_loadpath dll_ruby_init_loadpath
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200231#ifdef WIN3264
232# define NtInitialize dll_NtInitialize
233# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
234# define rb_w32_snprintf dll_rb_w32_snprintf
235# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236#endif
237
Bram Moolenaar639a2552010-03-17 18:15:23 +0100238#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100239# define ruby_script dll_ruby_script
240# define rb_enc_find_index dll_rb_enc_find_index
241# define rb_enc_find dll_rb_enc_find
242# define rb_enc_str_new dll_rb_enc_str_new
243# define rb_sprintf dll_rb_sprintf
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100244# define rb_require dll_rb_require
Bram Moolenaar639a2552010-03-17 18:15:23 +0100245# define ruby_init_stack dll_ruby_init_stack
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100246# define ruby_process_options dll_ruby_process_options
Bram Moolenaar165641d2010-02-17 16:23:09 +0100247#endif
248
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249/*
250 * Pointers for dynamic link
251 */
252static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200253VALUE *dll_rb_cFalseClass;
254VALUE *dll_rb_cFixnum;
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100255#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
256VALUE *dll_rb_cFloat;
257#endif
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200258VALUE *dll_rb_cNilClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259static VALUE *dll_rb_cObject;
Bram Moolenaarba52cde2010-06-25 04:29:11 +0200260VALUE *dll_rb_cSymbol;
261VALUE *dll_rb_cTrueClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262static void (*dll_rb_check_type) (VALUE,int);
263static VALUE (*dll_rb_class_path) (VALUE);
264static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
265static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
266static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
267static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
268static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
269static VALUE (*dll_rb_define_module) (const char*);
270static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
271static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
272static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
273static VALUE *dll_rb_stdout;
274static VALUE *dll_rb_eArgError;
275static VALUE *dll_rb_eIndexError;
276static VALUE *dll_rb_eRuntimeError;
277static VALUE *dll_rb_eStandardError;
278static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
279static void (*dll_rb_global_variable) (VALUE*);
280static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
281static VALUE (*dll_rb_hash_new) (void);
282static VALUE (*dll_rb_inspect) (VALUE);
283static VALUE (*dll_rb_int2inum) (long);
Bram Moolenaarb213da02012-10-03 18:06:59 +0200284#if SIZEOF_INT < SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200285static long (*dll_rb_fix2int) (VALUE);
286static long (*dll_rb_num2int) (VALUE);
287static unsigned long (*dll_rb_num2uint) (VALUE);
Bram Moolenaarb213da02012-10-03 18:06:59 +0200288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289static VALUE (*dll_rb_lastline_get) (void);
290static void (*dll_rb_lastline_set) (VALUE);
291static void (*dll_rb_load_protect) (VALUE, int, int*);
292static long (*dll_rb_num2long) (VALUE);
293static unsigned long (*dll_rb_num2ulong) (VALUE);
294static VALUE (*dll_rb_obj_alloc) (VALUE);
295static VALUE (*dll_rb_obj_as_string) (VALUE);
296static VALUE (*dll_rb_obj_id) (VALUE);
297static void (*dll_rb_raise) (VALUE, const char*, ...);
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200298#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
299static VALUE (*dll_rb_string_value) (volatile VALUE*);
300#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301static char *(*dll_rb_str2cstr) (VALUE,int*);
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
304static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
305static VALUE (*dll_rb_str_new) (const char*, long);
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200306#ifdef need_rb_str_new_cstr
307/* Ruby may #define rb_str_new2 to use rb_str_new_cstr. */
308static VALUE (*dll_rb_str_new_cstr) (const char*);
309#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200311#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100312#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100313static VALUE (*dll_rb_errinfo) (void);
314#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315static VALUE *dll_ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +0100316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317static void (*dll_ruby_init) (void);
318static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200319#ifdef WIN3264
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100320static void (*dll_NtInitialize) (int*, char***);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200321# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
322static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
323# endif
324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100326static char * (*dll_rb_string_value_ptr) (volatile VALUE*);
327static VALUE (*dll_rb_float_new) (double);
328static VALUE (*dll_rb_ary_new) (void);
329static VALUE (*dll_rb_ary_push) (VALUE, VALUE);
330#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100331#ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100332static VALUE (*dll_rb_int2big)(SIGNED_VALUE);
333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334
Bram Moolenaar639a2552010-03-17 18:15:23 +0100335#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100336static void (*dll_ruby_script) (const char*);
337static int (*dll_rb_enc_find_index) (const char*);
338static rb_encoding* (*dll_rb_enc_find) (const char*);
339static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
340static VALUE (*dll_rb_sprintf) (const char*, ...);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100341static VALUE (*dll_rb_require) (const char*);
Bram Moolenaar639a2552010-03-17 18:15:23 +0100342static void (*ruby_init_stack)(VALUE*);
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100343static void* (*ruby_process_options)(int, char**);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100344#endif
345
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100346#if defined(RUBY19_OR_LATER) && !defined(PROTO)
347SIGNED_VALUE rb_num2long_stub(VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100348{
349 return dll_rb_num2long(x);
350}
Bram Moolenaarff8cf2b2012-11-24 13:39:00 +0100351VALUE rb_int2big_stub(SIGNED_VALUE x)
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100352{
353 return dll_rb_int2big(x);
354}
Bram Moolenaar886ed692013-02-26 13:41:35 +0100355#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
356VALUE
357rb_float_new_in_heap(double d)
358{
359 return dll_rb_float_new(d);
360}
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100361VALUE rb_num2ulong(VALUE x)
Bram Moolenaar886ed692013-02-26 13:41:35 +0100362{
363 return (long)RSHIFT((SIGNED_VALUE)(x),1);
364}
365#endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100366#endif
367
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200368static HINSTANCE hinstRuby = NULL; /* Instance of ruby.dll */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369
370/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000371 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373static struct
374{
375 char *name;
376 RUBY_PROC *ptr;
377} ruby_funcname_table[] =
378{
379 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
380 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
381 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
Bram Moolenaardb3fbe52013-03-07 15:16:21 +0100382#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 20
383 {"rb_cFloat", (RUBY_PROC*)&dll_rb_cFloat},
384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
386 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
387 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
388 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
389 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
390 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
391 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
392 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
393 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
394 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
395 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
396 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
397 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
398 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
399 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
400 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
401 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
402 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
403 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
404 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
405 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
406 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
407 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
408 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
409 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
410 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
Bram Moolenaarb213da02012-10-03 18:06:59 +0200411#if SIZEOF_INT < SIZEOF_LONG /* 64 bits only */
Bram Moolenaar2623b4f2012-09-18 16:36:32 +0200412 {"rb_fix2int", (RUBY_PROC*)&dll_rb_fix2int},
413 {"rb_num2int", (RUBY_PROC*)&dll_rb_num2int},
414 {"rb_num2uint", (RUBY_PROC*)&dll_rb_num2uint},
Bram Moolenaarb213da02012-10-03 18:06:59 +0200415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
417 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
418 {"rb_load_protect", (RUBY_PROC*)&dll_rb_load_protect},
419 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
420 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
421 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
422 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
423 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
424 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200425#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
426 {"rb_string_value", (RUBY_PROC*)&dll_rb_string_value},
427#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
431 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
432 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200433#ifdef need_rb_str_new_cstr
434 {"rb_str_new_cstr", (RUBY_PROC*)&dll_rb_str_new_cstr},
435#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar1d2beae2010-05-22 21:56:55 +0200437#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100438#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100439 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
440#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
444 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200445#ifdef WIN3264
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100446 {
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200447# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER < 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100448 "NtInitialize",
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200449# else
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100450 "ruby_sysinit",
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200451# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100452 (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200453# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200455# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456#endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100457#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
458 {"rb_string_value_ptr", (RUBY_PROC*)&dll_rb_string_value_ptr},
Bram Moolenaar886ed692013-02-26 13:41:35 +0100459# if DYNAMIC_RUBY_VER <= 19
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100460 {"rb_float_new", (RUBY_PROC*)&dll_rb_float_new},
Bram Moolenaar886ed692013-02-26 13:41:35 +0100461# else
462 {"rb_float_new_in_heap", (RUBY_PROC*)&dll_rb_float_new},
463# endif
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100464 {"rb_ary_new", (RUBY_PROC*)&dll_rb_ary_new},
465 {"rb_ary_push", (RUBY_PROC*)&dll_rb_ary_push},
466#endif
Bram Moolenaar639a2552010-03-17 18:15:23 +0100467#ifdef RUBY19_OR_LATER
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100468 {"rb_int2big", (RUBY_PROC*)&dll_rb_int2big},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100469 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
470 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
471 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
472 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
473 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100474 {"rb_require", (RUBY_PROC*)&dll_rb_require},
Bram Moolenaar639a2552010-03-17 18:15:23 +0100475 {"ruby_init_stack", (RUBY_PROC*)&dll_ruby_init_stack},
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100476 {"ruby_process_options", (RUBY_PROC*)&dll_ruby_process_options},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 {"", NULL},
479};
480
481/*
482 * Free ruby.dll
483 */
484 static void
485end_dynamic_ruby()
486{
487 if (hinstRuby)
488 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200489 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200490 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 }
492}
493
494/*
495 * Load library and get all pointers.
496 * Parameter 'libname' provides name of DLL.
497 * Return OK or FAIL.
498 */
499 static int
500ruby_runtime_link_init(char *libname, int verbose)
501{
502 int i;
503
504 if (hinstRuby)
505 return OK;
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200506 hinstRuby = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 if (!hinstRuby)
508 {
509 if (verbose)
510 EMSG2(_(e_loadlib), libname);
511 return FAIL;
512 }
513
514 for (i = 0; ruby_funcname_table[i].ptr; ++i)
515 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200516 if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 ruby_funcname_table[i].name)))
518 {
Bram Moolenaarf9b5ef82010-09-29 13:02:53 +0200519 close_dll(hinstRuby);
Bram Moolenaar3ca71f12010-10-27 16:49:47 +0200520 hinstRuby = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 if (verbose)
522 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
523 return FAIL;
524 }
525 }
526 return OK;
527}
528
529/*
530 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
531 * else FALSE.
532 */
533 int
534ruby_enabled(verbose)
535 int verbose;
536{
537 return ruby_runtime_link_init(DYNAMIC_RUBY_DLL, verbose) == OK;
538}
539#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
540
541 void
542ruby_end()
543{
544#ifdef DYNAMIC_RUBY
545 end_dynamic_ruby();
546#endif
547}
548
549void ex_ruby(exarg_T *eap)
550{
551 int state;
552 char *script = NULL;
553
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000554 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 if (!eap->skip && ensure_ruby_initialized())
556 {
557 if (script == NULL)
558 rb_eval_string_protect((char *)eap->arg, &state);
559 else
560 rb_eval_string_protect(script, &state);
561 if (state)
562 error_print(state);
563 }
564 vim_free(script);
565}
566
Bram Moolenaar165641d2010-02-17 16:23:09 +0100567/*
568 * In Ruby 1.9 or later, ruby String object has encoding.
569 * conversion buffer string of vim to ruby String object using
570 * VIM encoding option.
571 */
572 static VALUE
573vim_str2rb_enc_str(const char *s)
574{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100575#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100576 int isnum;
577 long lval;
578 char_u *sval;
579 rb_encoding *enc;
580
581 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
582 if (isnum == 0)
583 {
584 enc = rb_enc_find((char *)sval);
585 vim_free(sval);
586 if (enc) {
587 return rb_enc_str_new(s, strlen(s), enc);
588 }
589 }
590#endif
591 return rb_str_new2(s);
592}
593
594 static VALUE
595eval_enc_string_protect(const char *str, int *state)
596{
Bram Moolenaar639a2552010-03-17 18:15:23 +0100597#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100598 int isnum;
599 long lval;
600 char_u *sval;
601 rb_encoding *enc;
602 VALUE v;
603
604 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
605 if (isnum == 0)
606 {
607 enc = rb_enc_find((char *)sval);
608 vim_free(sval);
609 if (enc)
610 {
611 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
612 return rb_eval_string_protect(StringValuePtr(v), state);
613 }
614 }
615#endif
616 return rb_eval_string_protect(str, state);
617}
618
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619void ex_rubydo(exarg_T *eap)
620{
621 int state;
622 linenr_T i;
623
624 if (ensure_ruby_initialized())
625 {
626 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
627 return;
628 for (i = eap->line1; i <= eap->line2; i++) {
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100629 VALUE line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100631 line = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100633 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 if (state) {
635 error_print(state);
636 break;
637 }
638 line = rb_lastline_get();
639 if (!NIL_P(line)) {
640 if (TYPE(line) != T_STRING) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000641 EMSG(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 return;
643 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100644 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 changed();
646#ifdef SYNTAX_HL
647 syn_changed(i); /* recompute syntax hl. for this line */
648#endif
649 }
650 }
651 check_cursor();
652 update_curbuf(NOT_VALID);
653 }
654}
655
656void ex_rubyfile(exarg_T *eap)
657{
658 int state;
659
660 if (ensure_ruby_initialized())
661 {
662 rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
663 if (state) error_print(state);
664 }
665}
666
667void ruby_buffer_free(buf_T *buf)
668{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000669 if (buf->b_ruby_ref)
670 {
671 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
672 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 }
674}
675
676void ruby_window_free(win_T *win)
677{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000678 if (win->w_ruby_ref)
679 {
680 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
681 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 }
683}
684
685static int ensure_ruby_initialized(void)
686{
687 if (!ruby_initialized)
688 {
689#ifdef DYNAMIC_RUBY
690 if (ruby_enabled(TRUE))
691 {
692#endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100693#ifdef _WIN32
694 /* suggested by Ariya Mizutani */
695 int argc = 1;
696 char *argv[] = {"gvim.exe"};
697 NtInitialize(&argc, &argv);
698#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200699 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100700#ifdef RUBY19_OR_LATER
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200701 RUBY_INIT_STACK;
Bram Moolenaar165641d2010-02-17 16:23:09 +0100702#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200703 ruby_init();
704 }
Bram Moolenaar639a2552010-03-17 18:15:23 +0100705#ifdef RUBY19_OR_LATER
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100706 {
707 int dummy_argc = 2;
708 char *dummy_argv[] = {"vim-ruby", "-e0"};
709 ruby_process_options(dummy_argc, dummy_argv);
710 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100711 ruby_script("vim-ruby");
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100712#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 ruby_init_loadpath();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100714#endif
Bram Moolenaar7a8ef142010-12-24 13:39:35 +0100715 ruby_io_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 ruby_vim_init();
717 ruby_initialized = 1;
718#ifdef DYNAMIC_RUBY
719 }
720 else
721 {
722 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
723 return 0;
724 }
725#endif
726 }
727 return ruby_initialized;
728}
729
730static void error_print(int state)
731{
732#ifndef DYNAMIC_RUBY
Bram Moolenaar42d57f02010-03-10 12:47:00 +0100733#if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19) \
734 && !(defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 19)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 RUBYEXTERN VALUE ruby_errinfo;
736#endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 VALUE eclass;
739 VALUE einfo;
740 char buff[BUFSIZ];
741
742#define TAG_RETURN 0x1
743#define TAG_BREAK 0x2
744#define TAG_NEXT 0x3
745#define TAG_RETRY 0x4
746#define TAG_REDO 0x5
747#define TAG_RAISE 0x6
748#define TAG_THROW 0x7
749#define TAG_FATAL 0x8
750#define TAG_MASK 0xf
751
752 switch (state) {
753 case TAG_RETURN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000754 EMSG(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 break;
756 case TAG_NEXT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000757 EMSG(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 break;
759 case TAG_BREAK:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000760 EMSG(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 break;
762 case TAG_REDO:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000763 EMSG(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 break;
765 case TAG_RETRY:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000766 EMSG(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 break;
768 case TAG_RAISE:
769 case TAG_FATAL:
Bram Moolenaar639a2552010-03-17 18:15:23 +0100770#ifdef RUBY19_OR_LATER
Bram Moolenaar165641d2010-02-17 16:23:09 +0100771 eclass = CLASS_OF(rb_errinfo());
772 einfo = rb_obj_as_string(rb_errinfo());
773#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 eclass = CLASS_OF(ruby_errinfo);
775 einfo = rb_obj_as_string(ruby_errinfo);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100776#endif
777 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000778 EMSG(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 }
780 else {
781 VALUE epath;
782 char *p;
783
784 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000785 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar165641d2010-02-17 16:23:09 +0100786 RSTRING_PTR(epath), RSTRING_PTR(einfo));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 p = strchr(buff, '\n');
788 if (p) *p = '\0';
789 EMSG(buff);
790 }
791 break;
792 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000793 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 EMSG(buff);
795 break;
796 }
797}
798
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000799static VALUE vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800{
801 char *buff, *p;
802
803 str = rb_obj_as_string(str);
Bram Moolenaar3f5f7952011-08-04 19:34:59 +0200804 if (RSTRING_LEN(str) > 0)
805 {
806 /* Only do this when the string isn't empty, alloc(0) causes trouble. */
807 buff = ALLOCA_N(char, RSTRING_LEN(str));
808 strcpy(buff, RSTRING_PTR(str));
809 p = strchr(buff, '\n');
810 if (p) *p = '\0';
811 MSG(buff);
812 }
813 else
814 {
815 MSG("");
816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 return Qnil;
818}
819
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000820static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100822 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 update_screen(NOT_VALID);
824 return Qnil;
825}
826
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000827static VALUE vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100829 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 return Qnil;
831}
832
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100833#ifdef FEAT_EVAL
834static VALUE vim_to_ruby(typval_T *tv)
835{
836 VALUE result = Qnil;
837
838 if (tv->v_type == VAR_STRING)
839 {
Bram Moolenaar94127e42010-03-19 23:08:48 +0100840 result = rb_str_new2(tv->vval.v_string == NULL
841 ? "" : (char *)(tv->vval.v_string));
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100842 }
843 else if (tv->v_type == VAR_NUMBER)
844 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100845 result = INT2NUM(tv->vval.v_number);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100846 }
847# ifdef FEAT_FLOAT
848 else if (tv->v_type == VAR_FLOAT)
849 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100850 result = rb_float_new(tv->vval.v_float);
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100851 }
852# endif
853 else if (tv->v_type == VAR_LIST)
854 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100855 list_T *list = tv->vval.v_list;
856 listitem_T *curr;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100857
Bram Moolenaar639a2552010-03-17 18:15:23 +0100858 result = rb_ary_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100859
Bram Moolenaar639a2552010-03-17 18:15:23 +0100860 if (list != NULL)
861 {
862 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
863 {
864 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
865 }
866 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100867 }
868 else if (tv->v_type == VAR_DICT)
869 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100870 result = rb_hash_new();
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100871
Bram Moolenaar639a2552010-03-17 18:15:23 +0100872 if (tv->vval.v_dict != NULL)
873 {
874 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
875 long_u todo = ht->ht_used;
876 hashitem_T *hi;
877 dictitem_T *di;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100878
Bram Moolenaar639a2552010-03-17 18:15:23 +0100879 for (hi = ht->ht_array; todo > 0; ++hi)
880 {
881 if (!HASHITEM_EMPTY(hi))
882 {
883 --todo;
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100884
Bram Moolenaar639a2552010-03-17 18:15:23 +0100885 di = dict_lookup(hi);
886 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100887 vim_to_ruby(&di->di_tv));
Bram Moolenaar639a2552010-03-17 18:15:23 +0100888 }
889 }
890 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100891 } /* else return Qnil; */
892
893 return result;
894}
895#endif
896
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000897static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898{
899#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100900 typval_T *tv;
901 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100903 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
904 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 {
Bram Moolenaar639a2552010-03-17 18:15:23 +0100906 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100908 result = vim_to_ruby(tv);
909
910 free_tv(tv);
911
912 return result;
913#else
914 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916}
917
918static VALUE buffer_new(buf_T *buf)
919{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000920 if (buf->b_ruby_ref)
921 {
922 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 }
Bram Moolenaare344bea2005-09-01 20:46:49 +0000924 else
925 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000927 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
929 return obj;
930 }
931}
932
933static buf_T *get_buf(VALUE obj)
934{
935 buf_T *buf;
936
937 Data_Get_Struct(obj, buf_T, buf);
938 if (buf == NULL)
939 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
940 return buf;
941}
942
943static VALUE buffer_s_current()
944{
945 return buffer_new(curbuf);
946}
947
948static VALUE buffer_s_count()
949{
950 buf_T *b;
951 int n = 0;
952
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000953 for (b = firstbuf; b != NULL; b = b->b_next)
954 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000955 /* Deleted buffers should not be counted
956 * SegPhault - 01/07/05 */
957 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000958 n++;
959 }
960
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 return INT2NUM(n);
962}
963
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000964static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965{
966 buf_T *b;
967 int n = NUM2INT(num);
968
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000969 for (b = firstbuf; b != NULL; b = b->b_next)
970 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000971 /* Deleted buffers should not be counted
972 * SegPhault - 01/07/05 */
973 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000974 continue;
975
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000976 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000978
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000979 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 }
981 return Qnil;
982}
983
984static VALUE buffer_name(VALUE self)
985{
986 buf_T *buf = get_buf(self);
987
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000988 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989}
990
991static VALUE buffer_number(VALUE self)
992{
993 buf_T *buf = get_buf(self);
994
995 return INT2NUM(buf->b_fnum);
996}
997
998static VALUE buffer_count(VALUE self)
999{
1000 buf_T *buf = get_buf(self);
1001
1002 return INT2NUM(buf->b_ml.ml_line_count);
1003}
1004
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001005static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006{
Bram Moolenaar3c531602010-11-16 14:46:19 +01001007 if (n <= 0 || n > buf->b_ml.ml_line_count)
1008 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
1009 return vim_str2rb_enc_str((char *)ml_get_buf(buf, n, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010}
1011
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001012static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013{
1014 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001015
1016 if (buf != NULL)
1017 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
1018 return Qnil; /* For stop warning */
1019}
1020
1021static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
1022{
Bram Moolenaar165641d2010-02-17 16:23:09 +01001023 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001024 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001026 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
1027 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001028 /* set curwin/curbuf for "buf" and save some things */
1029 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001030
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 if (u_savesub(n) == OK) {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001032 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 changed();
1034#ifdef SYNTAX_HL
1035 syn_changed(n); /* recompute syntax hl. for this line */
1036#endif
1037 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001038
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001039 /* restore curwin/curbuf and a few other things */
1040 aucmd_restbuf(&aco);
1041 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001042
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 update_curbuf(NOT_VALID);
1044 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001045 else
1046 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001047 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 }
1049 return str;
1050}
1051
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001052static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
1053{
1054 buf_T *buf = get_buf(self);
1055
1056 if (buf != NULL)
1057 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
1058 return str;
1059}
1060
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061static VALUE buffer_delete(VALUE self, VALUE num)
1062{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001063 buf_T *buf = get_buf(self);
1064 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001065 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001067 if (n > 0 && n <= buf->b_ml.ml_line_count)
1068 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001069 /* set curwin/curbuf for "buf" and save some things */
1070 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001071
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 if (u_savedel(n, 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001074
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001075 /* Changes to non-active buffers should properly refresh
1076 * SegPhault - 01/09/05 */
1077 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001078
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 changed();
1080 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001081
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001082 /* restore curwin/curbuf and a few other things */
1083 aucmd_restbuf(&aco);
1084 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001085
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 update_curbuf(NOT_VALID);
1087 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001088 else
1089 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001090 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 }
1092 return Qnil;
1093}
1094
1095static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
1096{
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001097 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001098 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001099 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001100 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101
Bram Moolenaar3c531602010-11-16 14:46:19 +01001102 if (line == NULL)
1103 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001104 rb_raise(rb_eIndexError, "NULL line");
1105 }
1106 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001107 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001108 /* set curwin/curbuf for "buf" and save some things */
1109 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001110
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 if (u_inssub(n + 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001113
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001114 /* Changes to non-active buffers should properly refresh screen
1115 * SegPhault - 12/20/04 */
1116 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001117
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001118 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001120
Bram Moolenaar20ff7922006-06-20 19:10:43 +00001121 /* restore curwin/curbuf and a few other things */
1122 aucmd_restbuf(&aco);
1123 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00001124
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 update_curbuf(NOT_VALID);
1126 }
Bram Moolenaar3c531602010-11-16 14:46:19 +01001127 else
1128 {
Bram Moolenaar165641d2010-02-17 16:23:09 +01001129 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 }
1131 return str;
1132}
1133
1134static VALUE window_new(win_T *win)
1135{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001136 if (win->w_ruby_ref)
1137 {
1138 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00001140 else
1141 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001143 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
1145 return obj;
1146 }
1147}
1148
1149static win_T *get_win(VALUE obj)
1150{
1151 win_T *win;
1152
1153 Data_Get_Struct(obj, win_T, win);
1154 if (win == NULL)
1155 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
1156 return win;
1157}
1158
1159static VALUE window_s_current()
1160{
1161 return window_new(curwin);
1162}
1163
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001164/*
1165 * Added line manipulation functions
1166 * SegPhault - 03/07/05
1167 */
1168static VALUE line_s_current()
1169{
1170 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1171}
1172
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001173static VALUE set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001174{
1175 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1176}
1177
1178static VALUE current_line_number()
1179{
1180 return INT2FIX((int)curwin->w_cursor.lnum);
1181}
1182
1183
1184
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185static VALUE window_s_count()
1186{
1187#ifdef FEAT_WINDOWS
1188 win_T *w;
1189 int n = 0;
1190
Bram Moolenaarf740b292006-02-16 22:11:02 +00001191 for (w = firstwin; w != NULL; w = w->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 n++;
1193 return INT2NUM(n);
1194#else
1195 return INT2NUM(1);
1196#endif
1197}
1198
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001199static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200{
1201 win_T *w;
1202 int n = NUM2INT(num);
1203
1204#ifndef FEAT_WINDOWS
1205 w = curwin;
1206#else
1207 for (w = firstwin; w != NULL; w = w->w_next, --n)
1208#endif
1209 if (n == 0)
1210 return window_new(w);
1211 return Qnil;
1212}
1213
1214static VALUE window_buffer(VALUE self)
1215{
1216 win_T *win = get_win(self);
1217
1218 return buffer_new(win->w_buffer);
1219}
1220
1221static VALUE window_height(VALUE self)
1222{
1223 win_T *win = get_win(self);
1224
1225 return INT2NUM(win->w_height);
1226}
1227
1228static VALUE window_set_height(VALUE self, VALUE height)
1229{
1230 win_T *win = get_win(self);
1231 win_T *savewin = curwin;
1232
1233 curwin = win;
1234 win_setheight(NUM2INT(height));
1235 curwin = savewin;
1236 return height;
1237}
1238
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001239static VALUE window_width(VALUE self UNUSED)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001240{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001241 return INT2NUM(W_WIDTH(get_win(self)));
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001242}
1243
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001244static VALUE window_set_width(VALUE self UNUSED, VALUE width)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001245{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001246#ifdef FEAT_VERTSPLIT
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001247 win_T *win = get_win(self);
1248 win_T *savewin = curwin;
1249
1250 curwin = win;
1251 win_setwidth(NUM2INT(width));
1252 curwin = savewin;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001253#endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001254 return width;
1255}
1256
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257static VALUE window_cursor(VALUE self)
1258{
1259 win_T *win = get_win(self);
1260
1261 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1262}
1263
1264static VALUE window_set_cursor(VALUE self, VALUE pos)
1265{
1266 VALUE lnum, col;
1267 win_T *win = get_win(self);
1268
1269 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001270 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001272 lnum = RARRAY_PTR(pos)[0];
1273 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 win->w_cursor.lnum = NUM2LONG(lnum);
1275 win->w_cursor.col = NUM2UINT(col);
1276 check_cursor(); /* put cursor on an existing line */
1277 update_screen(NOT_VALID);
1278 return Qnil;
1279}
1280
Bram Moolenaar6217cdc2012-04-25 12:28:09 +02001281static VALUE f_nop(VALUE self UNUSED)
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001282{
1283 return Qnil;
1284}
1285
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001286static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287{
1288 int i;
1289 VALUE str = rb_str_new("", 0);
1290
1291 for (i = 0; i < argc; i++) {
1292 if (i > 0) rb_str_cat(str, ", ", 2);
1293 rb_str_concat(str, rb_inspect(argv[i]));
1294 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001295 MSG(RSTRING_PTR(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 return Qnil;
1297}
1298
1299static void ruby_io_init(void)
1300{
1301#ifndef DYNAMIC_RUBY
1302 RUBYEXTERN VALUE rb_stdout;
1303#endif
1304
1305 rb_stdout = rb_obj_alloc(rb_cObject);
1306 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
Bram Moolenaar35df7d22012-04-20 18:05:47 +02001307 rb_define_singleton_method(rb_stdout, "flush", f_nop, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 rb_define_global_function("p", f_p, -1);
1309}
1310
1311static void ruby_vim_init(void)
1312{
1313 objtbl = rb_hash_new();
1314 rb_global_variable(&objtbl);
1315
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001316 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
1317 * alias "VIM" for backwards compatiblity. */
1318 mVIM = rb_define_module("Vim");
1319 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1321 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1322 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1323 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1324 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1325 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1326 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1327 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1328 rb_define_module_function(mVIM, "message", vim_message, 1);
1329 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1330 rb_define_module_function(mVIM, "command", vim_command, 1);
1331 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
1332
1333 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1334 rb_eStandardError);
1335 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1336 rb_eStandardError);
1337
1338 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1339 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1340 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1341 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1342 rb_define_method(cBuffer, "name", buffer_name, 0);
1343 rb_define_method(cBuffer, "number", buffer_number, 0);
1344 rb_define_method(cBuffer, "count", buffer_count, 0);
1345 rb_define_method(cBuffer, "length", buffer_count, 0);
1346 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1347 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1348 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1349 rb_define_method(cBuffer, "append", buffer_append, 2);
1350
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001351 /* Added line manipulation functions
1352 * SegPhault - 03/07/05 */
1353 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1354 rb_define_method(cBuffer, "line", line_s_current, 0);
1355 rb_define_method(cBuffer, "line=", set_current_line, 1);
1356
1357
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1359 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1360 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1361 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1362 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1363 rb_define_method(cVimWindow, "height", window_height, 0);
1364 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001365 rb_define_method(cVimWindow, "width", window_width, 0);
1366 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1368 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1369
1370 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1371 rb_define_virtual_variable("$curwin", window_s_current, 0);
1372}