blob: 6d28de37e54809831a459a68b2c3493d94118d3f [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 Moolenaar071d4272004-06-13 20:20:40 +00007 *
8 * Do ":help uganda" in Vim to read copying and usage conditions.
9 * Do ":help credits" in Vim to see a list of people who contributed.
10 * See README.txt for an overview of the Vim source code.
11 */
12
13#include <stdio.h>
14#include <string.h>
15
16#ifdef _WIN32
17# if !defined(DYNAMIC_RUBY_VER) || (DYNAMIC_RUBY_VER < 18)
18# define NT
19# endif
20# ifndef DYNAMIC_RUBY
21# define IMPORT /* For static dll usage __declspec(dllimport) */
22# define RUBYEXTERN __declspec(dllimport)
23# endif
24#endif
25#ifndef RUBYEXTERN
26# define RUBYEXTERN extern
27#endif
28
29/*
30 * This is tricky. In ruby.h there is (inline) function rb_class_of()
31 * definition. This function use these variables. But we want function to
32 * use dll_* variables.
33 */
34#ifdef DYNAMIC_RUBY
35# define rb_cFalseClass (*dll_rb_cFalseClass)
36# define rb_cFixnum (*dll_rb_cFixnum)
37# define rb_cNilClass (*dll_rb_cNilClass)
38# define rb_cSymbol (*dll_rb_cSymbol)
39# define rb_cTrueClass (*dll_rb_cTrueClass)
40# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
41/*
42 * On ver 1.8, all Ruby functions are exported with "__declspce(dllimport)"
43 * in ruby.h. But it cause trouble for these variables, because it is
44 * defined in this file. When defined this RUBY_EXPORT it modified to
45 * "extern" and be able to avoid this problem.
46 */
47# define RUBY_EXPORT
48# endif
49#endif
50
Bram Moolenaar0b69c732010-02-17 15:11:50 +010051/* suggested by Ariya Mizutani */
52#if (_MSC_VER == 1200)
53# undef _WIN32_WINNT
54#endif
55
Bram Moolenaar071d4272004-06-13 20:20:40 +000056#include <ruby.h>
Bram Moolenaar165641d2010-02-17 16:23:09 +010057#if defined(RUBY_VERSION) && RUBY_VERSION >= 19
58# include <ruby/encoding.h>
59#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000060
61#undef EXTERN
62#undef _
63
64/* T_DATA defined both by Ruby and Mac header files, hack around it... */
Bram Moolenaarcb635362007-05-12 13:02:42 +000065#if defined(MACOS_X_UNIX) || defined(macintosh)
Bram Moolenaar071d4272004-06-13 20:20:40 +000066# define __OPENTRANSPORT__
67# define __OPENTRANSPORTPROTOCOL__
68# define __OPENTRANSPORTPROVIDERS__
69#endif
70
Bram Moolenaar165641d2010-02-17 16:23:09 +010071/*
72 * Backward compatiblity for Ruby 1.8 and earlier.
73 * Ruby 1.9 does not provide STR2CSTR, instead StringValuePtr is provided.
74 * Ruby 1.9 does not provide RXXX(s)->len and RXXX(s)->ptr, instead
75 * RXXX_LEN(s) and RXXX_PTR(s) are provided.
76 */
77#ifndef StringValuePtr
78# define StringValuePtr(s) STR2CSTR(s)
79#endif
80#ifndef RARRAY_LEN
81# define RARRAY_LEN(s) RARRAY(s)->len
82#endif
83#ifndef RARRAY_PTR
84# define RARRAY_PTR(s) RARRAY(s)->ptr
85#endif
86#ifndef RSTRING_LEN
87# define RSTRING_LEN(s) RSTRING(s)->len
88#endif
89#ifndef RSTRING_PTR
90# define RSTRING_PTR(s) RSTRING(s)->ptr
91#endif
92
Bram Moolenaar071d4272004-06-13 20:20:40 +000093#include "vim.h"
94#include "version.h"
95
96#if defined(PROTO) && !defined(FEAT_RUBY)
97/* Define these to be able to generate the function prototypes. */
98# define VALUE int
99# define RUBY_DATA_FUNC int
100#endif
101
102static int ruby_initialized = 0;
103static VALUE objtbl;
104
105static VALUE mVIM;
106static VALUE cBuffer;
107static VALUE cVimWindow;
108static VALUE eDeletedBufferError;
109static VALUE eDeletedWindowError;
110
111static int ensure_ruby_initialized(void);
112static void error_print(int);
113static void ruby_io_init(void);
114static void ruby_vim_init(void);
115
116#if defined(DYNAMIC_RUBY) || defined(PROTO)
117#ifdef PROTO
118# define HINSTANCE int /* for generating prototypes */
119#endif
120
121/*
122 * Wrapper defines
123 */
124#define rb_assoc_new dll_rb_assoc_new
125#define rb_cObject (*dll_rb_cObject)
126#define rb_check_type dll_rb_check_type
127#define rb_class_path dll_rb_class_path
128#define rb_data_object_alloc dll_rb_data_object_alloc
129#define rb_define_class_under dll_rb_define_class_under
130#define rb_define_const dll_rb_define_const
131#define rb_define_global_function dll_rb_define_global_function
132#define rb_define_method dll_rb_define_method
133#define rb_define_module dll_rb_define_module
134#define rb_define_module_function dll_rb_define_module_function
135#define rb_define_singleton_method dll_rb_define_singleton_method
136#define rb_define_virtual_variable dll_rb_define_virtual_variable
137#define rb_stdout (*dll_rb_stdout)
138#define rb_eArgError (*dll_rb_eArgError)
139#define rb_eIndexError (*dll_rb_eIndexError)
140#define rb_eRuntimeError (*dll_rb_eRuntimeError)
141#define rb_eStandardError (*dll_rb_eStandardError)
142#define rb_eval_string_protect dll_rb_eval_string_protect
143#define rb_global_variable dll_rb_global_variable
144#define rb_hash_aset dll_rb_hash_aset
145#define rb_hash_new dll_rb_hash_new
146#define rb_inspect dll_rb_inspect
147#define rb_int2inum dll_rb_int2inum
148#define rb_lastline_get dll_rb_lastline_get
149#define rb_lastline_set dll_rb_lastline_set
150#define rb_load_protect dll_rb_load_protect
151#define rb_num2long dll_rb_num2long
152#define rb_num2ulong dll_rb_num2ulong
153#define rb_obj_alloc dll_rb_obj_alloc
154#define rb_obj_as_string dll_rb_obj_as_string
155#define rb_obj_id dll_rb_obj_id
156#define rb_raise dll_rb_raise
157#define rb_str2cstr dll_rb_str2cstr
158#define rb_str_cat dll_rb_str_cat
159#define rb_str_concat dll_rb_str_concat
160#define rb_str_new dll_rb_str_new
161#define rb_str_new2 dll_rb_str_new2
Bram Moolenaar165641d2010-02-17 16:23:09 +0100162#if defined(RUBY_VERSION) && RUBY_VERSION >= 19
163# define rb_errinfo dll_rb_errinfo
164#else
165# define ruby_errinfo (*dll_ruby_errinfo)
166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167#define ruby_init dll_ruby_init
168#define ruby_init_loadpath dll_ruby_init_loadpath
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100169#define NtInitialize dll_NtInitialize
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
171# define rb_w32_snprintf dll_rb_w32_snprintf
172#endif
173
Bram Moolenaar165641d2010-02-17 16:23:09 +0100174#if defined(RUBY_VERSION) && RUBY_VERSION >= 19
175# define ruby_script dll_ruby_script
176# define rb_enc_find_index dll_rb_enc_find_index
177# define rb_enc_find dll_rb_enc_find
178# define rb_enc_str_new dll_rb_enc_str_new
179# define rb_sprintf dll_rb_sprintf
180#endif
181
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182/*
183 * Pointers for dynamic link
184 */
185static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
186static VALUE *dll_rb_cFalseClass;
187static VALUE *dll_rb_cFixnum;
188static VALUE *dll_rb_cNilClass;
189static VALUE *dll_rb_cObject;
190static VALUE *dll_rb_cSymbol;
191static VALUE *dll_rb_cTrueClass;
192static void (*dll_rb_check_type) (VALUE,int);
193static VALUE (*dll_rb_class_path) (VALUE);
194static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
195static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
196static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
197static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
198static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
199static VALUE (*dll_rb_define_module) (const char*);
200static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
201static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
202static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
203static VALUE *dll_rb_stdout;
204static VALUE *dll_rb_eArgError;
205static VALUE *dll_rb_eIndexError;
206static VALUE *dll_rb_eRuntimeError;
207static VALUE *dll_rb_eStandardError;
208static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
209static void (*dll_rb_global_variable) (VALUE*);
210static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
211static VALUE (*dll_rb_hash_new) (void);
212static VALUE (*dll_rb_inspect) (VALUE);
213static VALUE (*dll_rb_int2inum) (long);
214static VALUE (*dll_rb_int2inum) (long);
215static VALUE (*dll_rb_lastline_get) (void);
216static void (*dll_rb_lastline_set) (VALUE);
217static void (*dll_rb_load_protect) (VALUE, int, int*);
218static long (*dll_rb_num2long) (VALUE);
219static unsigned long (*dll_rb_num2ulong) (VALUE);
220static VALUE (*dll_rb_obj_alloc) (VALUE);
221static VALUE (*dll_rb_obj_as_string) (VALUE);
222static VALUE (*dll_rb_obj_id) (VALUE);
223static void (*dll_rb_raise) (VALUE, const char*, ...);
224static char *(*dll_rb_str2cstr) (VALUE,int*);
225static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
226static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
227static VALUE (*dll_rb_str_new) (const char*, long);
228static VALUE (*dll_rb_str_new2) (const char*);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100229#if defined(RUBY_VERSION) && RUBY_VERSION >= 19
230static VALUE (*dll_rb_errinfo) (void);
231#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232static VALUE *dll_ruby_errinfo;
Bram Moolenaar165641d2010-02-17 16:23:09 +0100233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234static void (*dll_ruby_init) (void);
235static void (*dll_ruby_init_loadpath) (void);
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100236static void (*dll_NtInitialize) (int*, char***);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
238static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
239#endif
240
Bram Moolenaar165641d2010-02-17 16:23:09 +0100241#if defined(RUBY_VERSION) && RUBY_VERSION >= 19
242static void (*dll_ruby_script) (const char*);
243static int (*dll_rb_enc_find_index) (const char*);
244static rb_encoding* (*dll_rb_enc_find) (const char*);
245static VALUE (*dll_rb_enc_str_new) (const char*, long, rb_encoding*);
246static VALUE (*dll_rb_sprintf) (const char*, ...);
247#endif
248
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249static HINSTANCE hinstRuby = 0; /* Instance of ruby.dll */
250
251/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000252 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 */
254#define RUBY_PROC FARPROC
255static struct
256{
257 char *name;
258 RUBY_PROC *ptr;
259} ruby_funcname_table[] =
260{
261 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
262 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
263 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
264 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
265 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
266 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
267 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
268 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
269 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
270 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
271 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
272 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
273 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
274 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
275 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
276 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
277 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
278 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
279 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
280 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
281 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
282 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
283 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
284 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
285 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
286 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
287 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
288 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
289 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
290 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
291 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
292 {"rb_load_protect", (RUBY_PROC*)&dll_rb_load_protect},
293 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
294 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
295 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
296 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
297 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
298 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
299 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
300 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
301 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
302 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
303 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100304#if defined(RUBY_VERSION) && RUBY_VERSION >= 19
305 {"rb_errinfo", (RUBY_PROC*)&dll_rb_errinfo},
306#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
Bram Moolenaar165641d2010-02-17 16:23:09 +0100308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
310 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100311 {"NtInitialize", (RUBY_PROC*)&dll_NtInitialize},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
313 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
314#endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100315#if defined(RUBY_VERSION) && RUBY_VERSION >= 19
316 {"ruby_script", (RUBY_PROC*)&dll_ruby_script},
317 {"rb_enc_find_index", (RUBY_PROC*)&dll_rb_enc_find_index},
318 {"rb_enc_find", (RUBY_PROC*)&dll_rb_enc_find},
319 {"rb_enc_str_new", (RUBY_PROC*)&dll_rb_enc_str_new},
320 {"rb_sprintf", (RUBY_PROC*)&dll_rb_sprintf},
321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 {"", NULL},
323};
324
325/*
326 * Free ruby.dll
327 */
328 static void
329end_dynamic_ruby()
330{
331 if (hinstRuby)
332 {
333 FreeLibrary(hinstRuby);
334 hinstRuby = 0;
335 }
336}
337
338/*
339 * Load library and get all pointers.
340 * Parameter 'libname' provides name of DLL.
341 * Return OK or FAIL.
342 */
343 static int
344ruby_runtime_link_init(char *libname, int verbose)
345{
346 int i;
347
348 if (hinstRuby)
349 return OK;
350 hinstRuby = LoadLibrary(libname);
351 if (!hinstRuby)
352 {
353 if (verbose)
354 EMSG2(_(e_loadlib), libname);
355 return FAIL;
356 }
357
358 for (i = 0; ruby_funcname_table[i].ptr; ++i)
359 {
360 if (!(*ruby_funcname_table[i].ptr = GetProcAddress(hinstRuby,
361 ruby_funcname_table[i].name)))
362 {
363 FreeLibrary(hinstRuby);
364 hinstRuby = 0;
365 if (verbose)
366 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
367 return FAIL;
368 }
369 }
370 return OK;
371}
372
373/*
374 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
375 * else FALSE.
376 */
377 int
378ruby_enabled(verbose)
379 int verbose;
380{
381 return ruby_runtime_link_init(DYNAMIC_RUBY_DLL, verbose) == OK;
382}
383#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
384
385 void
386ruby_end()
387{
388#ifdef DYNAMIC_RUBY
389 end_dynamic_ruby();
390#endif
391}
392
393void ex_ruby(exarg_T *eap)
394{
395 int state;
396 char *script = NULL;
397
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000398 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 if (!eap->skip && ensure_ruby_initialized())
400 {
401 if (script == NULL)
402 rb_eval_string_protect((char *)eap->arg, &state);
403 else
404 rb_eval_string_protect(script, &state);
405 if (state)
406 error_print(state);
407 }
408 vim_free(script);
409}
410
Bram Moolenaar165641d2010-02-17 16:23:09 +0100411/*
412 * In Ruby 1.9 or later, ruby String object has encoding.
413 * conversion buffer string of vim to ruby String object using
414 * VIM encoding option.
415 */
416 static VALUE
417vim_str2rb_enc_str(const char *s)
418{
419#if defined(RUBY_VERSION) && RUBY_VERSION >= 19
420 int isnum;
421 long lval;
422 char_u *sval;
423 rb_encoding *enc;
424
425 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
426 if (isnum == 0)
427 {
428 enc = rb_enc_find((char *)sval);
429 vim_free(sval);
430 if (enc) {
431 return rb_enc_str_new(s, strlen(s), enc);
432 }
433 }
434#endif
435 return rb_str_new2(s);
436}
437
438 static VALUE
439eval_enc_string_protect(const char *str, int *state)
440{
441#if defined(RUBY_VERSION) && RUBY_VERSION >= 19
442 int isnum;
443 long lval;
444 char_u *sval;
445 rb_encoding *enc;
446 VALUE v;
447
448 isnum = get_option_value((char_u *)"enc", &lval, &sval, 0);
449 if (isnum == 0)
450 {
451 enc = rb_enc_find((char *)sval);
452 vim_free(sval);
453 if (enc)
454 {
455 v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str);
456 return rb_eval_string_protect(StringValuePtr(v), state);
457 }
458 }
459#endif
460 return rb_eval_string_protect(str, state);
461}
462
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463void ex_rubydo(exarg_T *eap)
464{
465 int state;
466 linenr_T i;
467
468 if (ensure_ruby_initialized())
469 {
470 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
471 return;
472 for (i = eap->line1; i <= eap->line2; i++) {
473 VALUE line, oldline;
474
Bram Moolenaar165641d2010-02-17 16:23:09 +0100475 line = oldline = vim_str2rb_enc_str((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 rb_lastline_set(line);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100477 eval_enc_string_protect((char *) eap->arg, &state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 if (state) {
479 error_print(state);
480 break;
481 }
482 line = rb_lastline_get();
483 if (!NIL_P(line)) {
484 if (TYPE(line) != T_STRING) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000485 EMSG(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 return;
487 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100488 ml_replace(i, (char_u *) StringValuePtr(line), 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 changed();
490#ifdef SYNTAX_HL
491 syn_changed(i); /* recompute syntax hl. for this line */
492#endif
493 }
494 }
495 check_cursor();
496 update_curbuf(NOT_VALID);
497 }
498}
499
500void ex_rubyfile(exarg_T *eap)
501{
502 int state;
503
504 if (ensure_ruby_initialized())
505 {
506 rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
507 if (state) error_print(state);
508 }
509}
510
511void ruby_buffer_free(buf_T *buf)
512{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000513 if (buf->b_ruby_ref)
514 {
515 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
516 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 }
518}
519
520void ruby_window_free(win_T *win)
521{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000522 if (win->w_ruby_ref)
523 {
524 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
525 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 }
527}
528
529static int ensure_ruby_initialized(void)
530{
531 if (!ruby_initialized)
532 {
533#ifdef DYNAMIC_RUBY
534 if (ruby_enabled(TRUE))
535 {
536#endif
Bram Moolenaar0b69c732010-02-17 15:11:50 +0100537#ifdef _WIN32
538 /* suggested by Ariya Mizutani */
539 int argc = 1;
540 char *argv[] = {"gvim.exe"};
541 NtInitialize(&argc, &argv);
542#endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100543#if defined(RUBY_VERSION) && RUBY_VERSION >= 19
544 RUBY_INIT_STACK;
545#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 ruby_init();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100547#if defined(RUBY_VERSION) && RUBY_VERSION >= 19
548 ruby_script("vim-ruby");
549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 ruby_init_loadpath();
551 ruby_io_init();
Bram Moolenaar165641d2010-02-17 16:23:09 +0100552#if defined(RUBY_VERSION) && RUBY_VERSION >= 19
553 rb_enc_find_index("encdb");
554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 ruby_vim_init();
556 ruby_initialized = 1;
557#ifdef DYNAMIC_RUBY
558 }
559 else
560 {
561 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
562 return 0;
563 }
564#endif
565 }
566 return ruby_initialized;
567}
568
569static void error_print(int state)
570{
571#ifndef DYNAMIC_RUBY
Bram Moolenaar165641d2010-02-17 16:23:09 +0100572#if !(defined(RUBY_VERSION) && RUBY_VERSION >= 19)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 RUBYEXTERN VALUE ruby_errinfo;
574#endif
Bram Moolenaar165641d2010-02-17 16:23:09 +0100575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 VALUE eclass;
577 VALUE einfo;
578 char buff[BUFSIZ];
579
580#define TAG_RETURN 0x1
581#define TAG_BREAK 0x2
582#define TAG_NEXT 0x3
583#define TAG_RETRY 0x4
584#define TAG_REDO 0x5
585#define TAG_RAISE 0x6
586#define TAG_THROW 0x7
587#define TAG_FATAL 0x8
588#define TAG_MASK 0xf
589
590 switch (state) {
591 case TAG_RETURN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000592 EMSG(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 break;
594 case TAG_NEXT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000595 EMSG(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 break;
597 case TAG_BREAK:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000598 EMSG(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 break;
600 case TAG_REDO:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000601 EMSG(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 break;
603 case TAG_RETRY:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000604 EMSG(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 break;
606 case TAG_RAISE:
607 case TAG_FATAL:
Bram Moolenaar165641d2010-02-17 16:23:09 +0100608#if defined(RUBY_VERSION) && RUBY_VERSION >= 19
609 eclass = CLASS_OF(rb_errinfo());
610 einfo = rb_obj_as_string(rb_errinfo());
611#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 eclass = CLASS_OF(ruby_errinfo);
613 einfo = rb_obj_as_string(ruby_errinfo);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100614#endif
615 if (eclass == rb_eRuntimeError && RSTRING_LEN(einfo) == 0) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000616 EMSG(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 }
618 else {
619 VALUE epath;
620 char *p;
621
622 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000623 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar165641d2010-02-17 16:23:09 +0100624 RSTRING_PTR(epath), RSTRING_PTR(einfo));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 p = strchr(buff, '\n');
626 if (p) *p = '\0';
627 EMSG(buff);
628 }
629 break;
630 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000631 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 EMSG(buff);
633 break;
634 }
635}
636
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000637static VALUE vim_message(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638{
639 char *buff, *p;
640
641 str = rb_obj_as_string(str);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100642 buff = ALLOCA_N(char, RSTRING_LEN(str));
643 strcpy(buff, RSTRING_PTR(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 p = strchr(buff, '\n');
645 if (p) *p = '\0';
646 MSG(buff);
647 return Qnil;
648}
649
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000650static VALUE vim_set_option(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100652 do_set((char_u *)StringValuePtr(str), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 update_screen(NOT_VALID);
654 return Qnil;
655}
656
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000657static VALUE vim_command(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100659 do_cmdline_cmd((char_u *)StringValuePtr(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 return Qnil;
661}
662
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100663#ifdef FEAT_EVAL
664static VALUE vim_to_ruby(typval_T *tv)
665{
666 VALUE result = Qnil;
667
668 if (tv->v_type == VAR_STRING)
669 {
670 result = rb_str_new2((char *)tv->vval.v_string);
671 }
672 else if (tv->v_type == VAR_NUMBER)
673 {
674 result = INT2NUM(tv->vval.v_number);
675 }
676# ifdef FEAT_FLOAT
677 else if (tv->v_type == VAR_FLOAT)
678 {
679 result = rb_float_new(tv->vval.v_float);
680 }
681# endif
682 else if (tv->v_type == VAR_LIST)
683 {
684 list_T *list = tv->vval.v_list;
685 listitem_T *curr;
686
687 result = rb_ary_new();
688
689 if (list != NULL)
690 {
691 for (curr = list->lv_first; curr != NULL; curr = curr->li_next)
692 {
693 rb_ary_push(result, vim_to_ruby(&curr->li_tv));
694 }
695 }
696 }
697 else if (tv->v_type == VAR_DICT)
698 {
699 result = rb_hash_new();
700
701 if (tv->vval.v_dict != NULL)
702 {
703 hashtab_T *ht = &tv->vval.v_dict->dv_hashtab;
704 long_u todo = ht->ht_used;
705 hashitem_T *hi;
706 dictitem_T *di;
707
708 for (hi = ht->ht_array; todo > 0; ++hi)
709 {
710 if (!HASHITEM_EMPTY(hi))
711 {
712 --todo;
713
714 di = dict_lookup(hi);
715 rb_hash_aset(result, rb_str_new2((char *)hi->hi_key),
716 vim_to_ruby(&di->di_tv));
717 }
718 }
719 }
720 } /* else return Qnil; */
721
722 return result;
723}
724#endif
725
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000726static VALUE vim_evaluate(VALUE self UNUSED, VALUE str)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727{
728#ifdef FEAT_EVAL
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100729 typval_T *tv;
730 VALUE result;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100732 tv = eval_expr((char_u *)StringValuePtr(str), NULL);
733 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 {
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100735 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 }
Bram Moolenaar3fac56e2010-02-24 15:48:04 +0100737 result = vim_to_ruby(tv);
738
739 free_tv(tv);
740
741 return result;
742#else
743 return Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745}
746
747static VALUE buffer_new(buf_T *buf)
748{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000749 if (buf->b_ruby_ref)
750 {
751 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 }
Bram Moolenaare344bea2005-09-01 20:46:49 +0000753 else
754 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000756 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
758 return obj;
759 }
760}
761
762static buf_T *get_buf(VALUE obj)
763{
764 buf_T *buf;
765
766 Data_Get_Struct(obj, buf_T, buf);
767 if (buf == NULL)
768 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
769 return buf;
770}
771
772static VALUE buffer_s_current()
773{
774 return buffer_new(curbuf);
775}
776
777static VALUE buffer_s_count()
778{
779 buf_T *b;
780 int n = 0;
781
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000782 for (b = firstbuf; b != NULL; b = b->b_next)
783 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000784 /* Deleted buffers should not be counted
785 * SegPhault - 01/07/05 */
786 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000787 n++;
788 }
789
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 return INT2NUM(n);
791}
792
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000793static VALUE buffer_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794{
795 buf_T *b;
796 int n = NUM2INT(num);
797
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000798 for (b = firstbuf; b != NULL; b = b->b_next)
799 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000800 /* Deleted buffers should not be counted
801 * SegPhault - 01/07/05 */
802 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000803 continue;
804
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000805 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000807
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000808 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 }
810 return Qnil;
811}
812
813static VALUE buffer_name(VALUE self)
814{
815 buf_T *buf = get_buf(self);
816
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000817 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818}
819
820static VALUE buffer_number(VALUE self)
821{
822 buf_T *buf = get_buf(self);
823
824 return INT2NUM(buf->b_fnum);
825}
826
827static VALUE buffer_count(VALUE self)
828{
829 buf_T *buf = get_buf(self);
830
831 return INT2NUM(buf->b_ml.ml_line_count);
832}
833
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000834static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835{
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000836 if (n > 0 && n <= buf->b_ml.ml_line_count)
837 {
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000838 char *line = (char *)ml_get_buf(buf, n, FALSE);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100839 return line ? vim_str2rb_enc_str(line) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 }
Bram Moolenaar165641d2010-02-17 16:23:09 +0100841 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000842#ifndef __GNUC__
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000843 return Qnil; /* For stop warning */
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845}
846
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000847static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848{
849 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000850
851 if (buf != NULL)
852 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
853 return Qnil; /* For stop warning */
854}
855
856static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
857{
Bram Moolenaar165641d2010-02-17 16:23:09 +0100858 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000859 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000861 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
862 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000863 /* set curwin/curbuf for "buf" and save some things */
864 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000865
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 if (u_savesub(n) == OK) {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000867 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 changed();
869#ifdef SYNTAX_HL
870 syn_changed(n); /* recompute syntax hl. for this line */
871#endif
872 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000873
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000874 /* restore curwin/curbuf and a few other things */
875 aucmd_restbuf(&aco);
876 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +0000877
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 update_curbuf(NOT_VALID);
879 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000880 else
881 {
Bram Moolenaar165641d2010-02-17 16:23:09 +0100882 rb_raise(rb_eIndexError, "line number %ld out of range", (long)n);
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000883#ifndef __GNUC__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 return Qnil; /* For stop warning */
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +0000885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 }
887 return str;
888}
889
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000890static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
891{
892 buf_T *buf = get_buf(self);
893
894 if (buf != NULL)
895 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
896 return str;
897}
898
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899static VALUE buffer_delete(VALUE self, VALUE num)
900{
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000901 buf_T *buf = get_buf(self);
902 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000903 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000905 if (n > 0 && n <= buf->b_ml.ml_line_count)
906 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000907 /* set curwin/curbuf for "buf" and save some things */
908 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000909
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 if (u_savedel(n, 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000912
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000913 /* Changes to non-active buffers should properly refresh
914 * SegPhault - 01/09/05 */
915 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000916
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 changed();
918 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000919
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000920 /* restore curwin/curbuf and a few other things */
921 aucmd_restbuf(&aco);
922 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +0000923
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 update_curbuf(NOT_VALID);
925 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000926 else
927 {
Bram Moolenaar165641d2010-02-17 16:23:09 +0100928 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 }
930 return Qnil;
931}
932
933static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
934{
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000935 buf_T *buf = get_buf(self);
Bram Moolenaar165641d2010-02-17 16:23:09 +0100936 char *line = StringValuePtr(str);
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000937 long n = NUM2LONG(num);
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000938 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939
Bram Moolenaar83bac8b2010-02-18 15:53:29 +0100940 if (line == NULL) {
Bram Moolenaar165641d2010-02-17 16:23:09 +0100941 rb_raise(rb_eIndexError, "NULL line");
942 }
943 else if (n >= 0 && n <= buf->b_ml.ml_line_count)
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000944 {
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000945 /* set curwin/curbuf for "buf" and save some things */
946 aucmd_prepbuf(&aco, buf);
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000947
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 if (u_inssub(n + 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000950
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000951 /* Changes to non-active buffers should properly refresh screen
952 * SegPhault - 12/20/04 */
953 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000954
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000955 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000957
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000958 /* restore curwin/curbuf and a few other things */
959 aucmd_restbuf(&aco);
960 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaarf30e74c2006-08-16 17:35:00 +0000961
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 update_curbuf(NOT_VALID);
963 }
964 else {
Bram Moolenaar165641d2010-02-17 16:23:09 +0100965 rb_raise(rb_eIndexError, "line number %ld out of range", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 }
967 return str;
968}
969
970static VALUE window_new(win_T *win)
971{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000972 if (win->w_ruby_ref)
973 {
974 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 }
Bram Moolenaare344bea2005-09-01 20:46:49 +0000976 else
977 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000979 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
981 return obj;
982 }
983}
984
985static win_T *get_win(VALUE obj)
986{
987 win_T *win;
988
989 Data_Get_Struct(obj, win_T, win);
990 if (win == NULL)
991 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
992 return win;
993}
994
995static VALUE window_s_current()
996{
997 return window_new(curwin);
998}
999
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001000/*
1001 * Added line manipulation functions
1002 * SegPhault - 03/07/05
1003 */
1004static VALUE line_s_current()
1005{
1006 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
1007}
1008
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001009static VALUE set_current_line(VALUE self UNUSED, VALUE str)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001010{
1011 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
1012}
1013
1014static VALUE current_line_number()
1015{
1016 return INT2FIX((int)curwin->w_cursor.lnum);
1017}
1018
1019
1020
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021static VALUE window_s_count()
1022{
1023#ifdef FEAT_WINDOWS
1024 win_T *w;
1025 int n = 0;
1026
Bram Moolenaarf740b292006-02-16 22:11:02 +00001027 for (w = firstwin; w != NULL; w = w->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 n++;
1029 return INT2NUM(n);
1030#else
1031 return INT2NUM(1);
1032#endif
1033}
1034
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001035static VALUE window_s_aref(VALUE self UNUSED, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036{
1037 win_T *w;
1038 int n = NUM2INT(num);
1039
1040#ifndef FEAT_WINDOWS
1041 w = curwin;
1042#else
1043 for (w = firstwin; w != NULL; w = w->w_next, --n)
1044#endif
1045 if (n == 0)
1046 return window_new(w);
1047 return Qnil;
1048}
1049
1050static VALUE window_buffer(VALUE self)
1051{
1052 win_T *win = get_win(self);
1053
1054 return buffer_new(win->w_buffer);
1055}
1056
1057static VALUE window_height(VALUE self)
1058{
1059 win_T *win = get_win(self);
1060
1061 return INT2NUM(win->w_height);
1062}
1063
1064static VALUE window_set_height(VALUE self, VALUE height)
1065{
1066 win_T *win = get_win(self);
1067 win_T *savewin = curwin;
1068
1069 curwin = win;
1070 win_setheight(NUM2INT(height));
1071 curwin = savewin;
1072 return height;
1073}
1074
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001075static VALUE window_width(VALUE self)
1076{
1077 win_T *win = get_win(self);
1078
1079 return INT2NUM(win->w_width);
1080}
1081
1082static VALUE window_set_width(VALUE self, VALUE width)
1083{
1084 win_T *win = get_win(self);
1085 win_T *savewin = curwin;
1086
1087 curwin = win;
1088 win_setwidth(NUM2INT(width));
1089 curwin = savewin;
1090 return width;
1091}
1092
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093static VALUE window_cursor(VALUE self)
1094{
1095 win_T *win = get_win(self);
1096
1097 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
1098}
1099
1100static VALUE window_set_cursor(VALUE self, VALUE pos)
1101{
1102 VALUE lnum, col;
1103 win_T *win = get_win(self);
1104
1105 Check_Type(pos, T_ARRAY);
Bram Moolenaar165641d2010-02-17 16:23:09 +01001106 if (RARRAY_LEN(pos) != 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 rb_raise(rb_eArgError, "array length must be 2");
Bram Moolenaar165641d2010-02-17 16:23:09 +01001108 lnum = RARRAY_PTR(pos)[0];
1109 col = RARRAY_PTR(pos)[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 win->w_cursor.lnum = NUM2LONG(lnum);
1111 win->w_cursor.col = NUM2UINT(col);
1112 check_cursor(); /* put cursor on an existing line */
1113 update_screen(NOT_VALID);
1114 return Qnil;
1115}
1116
Bram Moolenaarcd8b20a2009-05-22 16:20:57 +00001117static VALUE f_p(int argc, VALUE *argv, VALUE self UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118{
1119 int i;
1120 VALUE str = rb_str_new("", 0);
1121
1122 for (i = 0; i < argc; i++) {
1123 if (i > 0) rb_str_cat(str, ", ", 2);
1124 rb_str_concat(str, rb_inspect(argv[i]));
1125 }
Bram Moolenaar165641d2010-02-17 16:23:09 +01001126 MSG(RSTRING_PTR(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 return Qnil;
1128}
1129
1130static void ruby_io_init(void)
1131{
1132#ifndef DYNAMIC_RUBY
1133 RUBYEXTERN VALUE rb_stdout;
1134#endif
1135
1136 rb_stdout = rb_obj_alloc(rb_cObject);
1137 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
1138 rb_define_global_function("p", f_p, -1);
1139}
1140
1141static void ruby_vim_init(void)
1142{
1143 objtbl = rb_hash_new();
1144 rb_global_variable(&objtbl);
1145
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001146 /* The Vim module used to be called "VIM", but "Vim" is better. Make an
1147 * alias "VIM" for backwards compatiblity. */
1148 mVIM = rb_define_module("Vim");
1149 rb_define_const(rb_cObject, "VIM", mVIM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
1151 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
1152 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
1153 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
1154 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
1155 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
1156 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
1157 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
1158 rb_define_module_function(mVIM, "message", vim_message, 1);
1159 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
1160 rb_define_module_function(mVIM, "command", vim_command, 1);
1161 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
1162
1163 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
1164 rb_eStandardError);
1165 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
1166 rb_eStandardError);
1167
1168 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
1169 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
1170 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
1171 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
1172 rb_define_method(cBuffer, "name", buffer_name, 0);
1173 rb_define_method(cBuffer, "number", buffer_number, 0);
1174 rb_define_method(cBuffer, "count", buffer_count, 0);
1175 rb_define_method(cBuffer, "length", buffer_count, 0);
1176 rb_define_method(cBuffer, "[]", buffer_aref, 1);
1177 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
1178 rb_define_method(cBuffer, "delete", buffer_delete, 1);
1179 rb_define_method(cBuffer, "append", buffer_append, 2);
1180
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001181 /* Added line manipulation functions
1182 * SegPhault - 03/07/05 */
1183 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1184 rb_define_method(cBuffer, "line", line_s_current, 0);
1185 rb_define_method(cBuffer, "line=", set_current_line, 1);
1186
1187
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1189 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1190 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1191 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1192 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1193 rb_define_method(cVimWindow, "height", window_height, 0);
1194 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001195 rb_define_method(cVimWindow, "width", window_width, 0);
1196 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1198 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1199
1200 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1201 rb_define_virtual_variable("$curwin", window_s_current, 0);
1202}