blob: 3685db0bc0c6788e75bd81f4519c81d85ab8d16d [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
51#include <ruby.h>
52
53#undef EXTERN
54#undef _
55
56/* T_DATA defined both by Ruby and Mac header files, hack around it... */
57#ifdef FEAT_GUI_MAC
58# define __OPENTRANSPORT__
59# define __OPENTRANSPORTPROTOCOL__
60# define __OPENTRANSPORTPROVIDERS__
61#endif
62
63#include "vim.h"
64#include "version.h"
65
66#if defined(PROTO) && !defined(FEAT_RUBY)
67/* Define these to be able to generate the function prototypes. */
68# define VALUE int
69# define RUBY_DATA_FUNC int
70#endif
71
72static int ruby_initialized = 0;
73static VALUE objtbl;
74
75static VALUE mVIM;
76static VALUE cBuffer;
77static VALUE cVimWindow;
78static VALUE eDeletedBufferError;
79static VALUE eDeletedWindowError;
80
81static int ensure_ruby_initialized(void);
82static void error_print(int);
83static void ruby_io_init(void);
84static void ruby_vim_init(void);
85
86#if defined(DYNAMIC_RUBY) || defined(PROTO)
87#ifdef PROTO
88# define HINSTANCE int /* for generating prototypes */
89#endif
90
91/*
92 * Wrapper defines
93 */
94#define rb_assoc_new dll_rb_assoc_new
95#define rb_cObject (*dll_rb_cObject)
96#define rb_check_type dll_rb_check_type
97#define rb_class_path dll_rb_class_path
98#define rb_data_object_alloc dll_rb_data_object_alloc
99#define rb_define_class_under dll_rb_define_class_under
100#define rb_define_const dll_rb_define_const
101#define rb_define_global_function dll_rb_define_global_function
102#define rb_define_method dll_rb_define_method
103#define rb_define_module dll_rb_define_module
104#define rb_define_module_function dll_rb_define_module_function
105#define rb_define_singleton_method dll_rb_define_singleton_method
106#define rb_define_virtual_variable dll_rb_define_virtual_variable
107#define rb_stdout (*dll_rb_stdout)
108#define rb_eArgError (*dll_rb_eArgError)
109#define rb_eIndexError (*dll_rb_eIndexError)
110#define rb_eRuntimeError (*dll_rb_eRuntimeError)
111#define rb_eStandardError (*dll_rb_eStandardError)
112#define rb_eval_string_protect dll_rb_eval_string_protect
113#define rb_global_variable dll_rb_global_variable
114#define rb_hash_aset dll_rb_hash_aset
115#define rb_hash_new dll_rb_hash_new
116#define rb_inspect dll_rb_inspect
117#define rb_int2inum dll_rb_int2inum
118#define rb_lastline_get dll_rb_lastline_get
119#define rb_lastline_set dll_rb_lastline_set
120#define rb_load_protect dll_rb_load_protect
121#define rb_num2long dll_rb_num2long
122#define rb_num2ulong dll_rb_num2ulong
123#define rb_obj_alloc dll_rb_obj_alloc
124#define rb_obj_as_string dll_rb_obj_as_string
125#define rb_obj_id dll_rb_obj_id
126#define rb_raise dll_rb_raise
127#define rb_str2cstr dll_rb_str2cstr
128#define rb_str_cat dll_rb_str_cat
129#define rb_str_concat dll_rb_str_concat
130#define rb_str_new dll_rb_str_new
131#define rb_str_new2 dll_rb_str_new2
132#define ruby_errinfo (*dll_ruby_errinfo)
133#define ruby_init dll_ruby_init
134#define ruby_init_loadpath dll_ruby_init_loadpath
135#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
136# define rb_w32_snprintf dll_rb_w32_snprintf
137#endif
138
139/*
140 * Pointers for dynamic link
141 */
142static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
143static VALUE *dll_rb_cFalseClass;
144static VALUE *dll_rb_cFixnum;
145static VALUE *dll_rb_cNilClass;
146static VALUE *dll_rb_cObject;
147static VALUE *dll_rb_cSymbol;
148static VALUE *dll_rb_cTrueClass;
149static void (*dll_rb_check_type) (VALUE,int);
150static VALUE (*dll_rb_class_path) (VALUE);
151static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
152static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
153static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
154static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
155static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
156static VALUE (*dll_rb_define_module) (const char*);
157static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
158static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
159static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
160static VALUE *dll_rb_stdout;
161static VALUE *dll_rb_eArgError;
162static VALUE *dll_rb_eIndexError;
163static VALUE *dll_rb_eRuntimeError;
164static VALUE *dll_rb_eStandardError;
165static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
166static void (*dll_rb_global_variable) (VALUE*);
167static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
168static VALUE (*dll_rb_hash_new) (void);
169static VALUE (*dll_rb_inspect) (VALUE);
170static VALUE (*dll_rb_int2inum) (long);
171static VALUE (*dll_rb_int2inum) (long);
172static VALUE (*dll_rb_lastline_get) (void);
173static void (*dll_rb_lastline_set) (VALUE);
174static void (*dll_rb_load_protect) (VALUE, int, int*);
175static long (*dll_rb_num2long) (VALUE);
176static unsigned long (*dll_rb_num2ulong) (VALUE);
177static VALUE (*dll_rb_obj_alloc) (VALUE);
178static VALUE (*dll_rb_obj_as_string) (VALUE);
179static VALUE (*dll_rb_obj_id) (VALUE);
180static void (*dll_rb_raise) (VALUE, const char*, ...);
181static char *(*dll_rb_str2cstr) (VALUE,int*);
182static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
183static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
184static VALUE (*dll_rb_str_new) (const char*, long);
185static VALUE (*dll_rb_str_new2) (const char*);
186static VALUE *dll_ruby_errinfo;
187static void (*dll_ruby_init) (void);
188static void (*dll_ruby_init_loadpath) (void);
189#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
190static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
191#endif
192
193static HINSTANCE hinstRuby = 0; /* Instance of ruby.dll */
194
195/*
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000196 * Table of name to function pointer of ruby.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 */
198#define RUBY_PROC FARPROC
199static struct
200{
201 char *name;
202 RUBY_PROC *ptr;
203} ruby_funcname_table[] =
204{
205 {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
206 {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
207 {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
208 {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
209 {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
210 {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
211 {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
212 {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
213 {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
214 {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
215 {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
216 {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
217 {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
218 {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
219 {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
220 {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
221 {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
222 {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
223 {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
224 {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
225 {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
226 {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
227 {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
228 {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
229 {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
230 {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
231 {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
232 {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
233 {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
234 {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
235 {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
236 {"rb_load_protect", (RUBY_PROC*)&dll_rb_load_protect},
237 {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
238 {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
239 {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
240 {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
241 {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
242 {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
243 {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
244 {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
245 {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
246 {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
247 {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
248 {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
249 {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
250 {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
251#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
252 {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
253#endif
254 {"", NULL},
255};
256
257/*
258 * Free ruby.dll
259 */
260 static void
261end_dynamic_ruby()
262{
263 if (hinstRuby)
264 {
265 FreeLibrary(hinstRuby);
266 hinstRuby = 0;
267 }
268}
269
270/*
271 * Load library and get all pointers.
272 * Parameter 'libname' provides name of DLL.
273 * Return OK or FAIL.
274 */
275 static int
276ruby_runtime_link_init(char *libname, int verbose)
277{
278 int i;
279
280 if (hinstRuby)
281 return OK;
282 hinstRuby = LoadLibrary(libname);
283 if (!hinstRuby)
284 {
285 if (verbose)
286 EMSG2(_(e_loadlib), libname);
287 return FAIL;
288 }
289
290 for (i = 0; ruby_funcname_table[i].ptr; ++i)
291 {
292 if (!(*ruby_funcname_table[i].ptr = GetProcAddress(hinstRuby,
293 ruby_funcname_table[i].name)))
294 {
295 FreeLibrary(hinstRuby);
296 hinstRuby = 0;
297 if (verbose)
298 EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
299 return FAIL;
300 }
301 }
302 return OK;
303}
304
305/*
306 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
307 * else FALSE.
308 */
309 int
310ruby_enabled(verbose)
311 int verbose;
312{
313 return ruby_runtime_link_init(DYNAMIC_RUBY_DLL, verbose) == OK;
314}
315#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */
316
317 void
318ruby_end()
319{
320#ifdef DYNAMIC_RUBY
321 end_dynamic_ruby();
322#endif
323}
324
325void ex_ruby(exarg_T *eap)
326{
327 int state;
328 char *script = NULL;
329
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000330 script = (char *)script_get(eap, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 if (!eap->skip && ensure_ruby_initialized())
332 {
333 if (script == NULL)
334 rb_eval_string_protect((char *)eap->arg, &state);
335 else
336 rb_eval_string_protect(script, &state);
337 if (state)
338 error_print(state);
339 }
340 vim_free(script);
341}
342
343void ex_rubydo(exarg_T *eap)
344{
345 int state;
346 linenr_T i;
347
348 if (ensure_ruby_initialized())
349 {
350 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
351 return;
352 for (i = eap->line1; i <= eap->line2; i++) {
353 VALUE line, oldline;
354
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000355 line = oldline = rb_str_new2((char *)ml_get(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 rb_lastline_set(line);
357 rb_eval_string_protect((char *) eap->arg, &state);
358 if (state) {
359 error_print(state);
360 break;
361 }
362 line = rb_lastline_get();
363 if (!NIL_P(line)) {
364 if (TYPE(line) != T_STRING) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000365 EMSG(_("E265: $_ must be an instance of String"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 return;
367 }
368 ml_replace(i, (char_u *) STR2CSTR(line), 1);
369 changed();
370#ifdef SYNTAX_HL
371 syn_changed(i); /* recompute syntax hl. for this line */
372#endif
373 }
374 }
375 check_cursor();
376 update_curbuf(NOT_VALID);
377 }
378}
379
380void ex_rubyfile(exarg_T *eap)
381{
382 int state;
383
384 if (ensure_ruby_initialized())
385 {
386 rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
387 if (state) error_print(state);
388 }
389}
390
391void ruby_buffer_free(buf_T *buf)
392{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000393 if (buf->b_ruby_ref)
394 {
395 rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil);
396 RDATA(buf->b_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 }
398}
399
400void ruby_window_free(win_T *win)
401{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000402 if (win->w_ruby_ref)
403 {
404 rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil);
405 RDATA(win->w_ruby_ref)->data = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 }
407}
408
409static int ensure_ruby_initialized(void)
410{
411 if (!ruby_initialized)
412 {
413#ifdef DYNAMIC_RUBY
414 if (ruby_enabled(TRUE))
415 {
416#endif
417 ruby_init();
418 ruby_init_loadpath();
419 ruby_io_init();
420 ruby_vim_init();
421 ruby_initialized = 1;
422#ifdef DYNAMIC_RUBY
423 }
424 else
425 {
426 EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
427 return 0;
428 }
429#endif
430 }
431 return ruby_initialized;
432}
433
434static void error_print(int state)
435{
436#ifndef DYNAMIC_RUBY
437 RUBYEXTERN VALUE ruby_errinfo;
438#endif
439 VALUE eclass;
440 VALUE einfo;
441 char buff[BUFSIZ];
442
443#define TAG_RETURN 0x1
444#define TAG_BREAK 0x2
445#define TAG_NEXT 0x3
446#define TAG_RETRY 0x4
447#define TAG_REDO 0x5
448#define TAG_RAISE 0x6
449#define TAG_THROW 0x7
450#define TAG_FATAL 0x8
451#define TAG_MASK 0xf
452
453 switch (state) {
454 case TAG_RETURN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000455 EMSG(_("E267: unexpected return"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 break;
457 case TAG_NEXT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000458 EMSG(_("E268: unexpected next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 break;
460 case TAG_BREAK:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000461 EMSG(_("E269: unexpected break"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 break;
463 case TAG_REDO:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000464 EMSG(_("E270: unexpected redo"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 break;
466 case TAG_RETRY:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000467 EMSG(_("E271: retry outside of rescue clause"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 break;
469 case TAG_RAISE:
470 case TAG_FATAL:
471 eclass = CLASS_OF(ruby_errinfo);
472 einfo = rb_obj_as_string(ruby_errinfo);
473 if (eclass == rb_eRuntimeError && RSTRING(einfo)->len == 0) {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000474 EMSG(_("E272: unhandled exception"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 }
476 else {
477 VALUE epath;
478 char *p;
479
480 epath = rb_class_path(eclass);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000481 vim_snprintf(buff, BUFSIZ, "%s: %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 RSTRING(epath)->ptr, RSTRING(einfo)->ptr);
483 p = strchr(buff, '\n');
484 if (p) *p = '\0';
485 EMSG(buff);
486 }
487 break;
488 default:
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000489 vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 EMSG(buff);
491 break;
492 }
493}
494
495static VALUE vim_message(VALUE self, VALUE str)
496{
497 char *buff, *p;
498
499 str = rb_obj_as_string(str);
500 buff = ALLOCA_N(char, RSTRING(str)->len);
501 strcpy(buff, RSTRING(str)->ptr);
502 p = strchr(buff, '\n');
503 if (p) *p = '\0';
504 MSG(buff);
505 return Qnil;
506}
507
508static VALUE vim_set_option(VALUE self, VALUE str)
509{
510 do_set((char_u *)STR2CSTR(str), 0);
511 update_screen(NOT_VALID);
512 return Qnil;
513}
514
515static VALUE vim_command(VALUE self, VALUE str)
516{
517 do_cmdline_cmd((char_u *)STR2CSTR(str));
518 return Qnil;
519}
520
521static VALUE vim_evaluate(VALUE self, VALUE str)
522{
523#ifdef FEAT_EVAL
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000524 char_u *value = eval_to_string((char_u *)STR2CSTR(str), NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000526 if (value != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 {
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000528 VALUE val = rb_str_new2((char *)value);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 vim_free(value);
530 return val;
531 }
532 else
533#endif
534 return Qnil;
535}
536
537static VALUE buffer_new(buf_T *buf)
538{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000539 if (buf->b_ruby_ref)
540 {
541 return (VALUE) buf->b_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 }
Bram Moolenaare344bea2005-09-01 20:46:49 +0000543 else
544 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000546 buf->b_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
548 return obj;
549 }
550}
551
552static buf_T *get_buf(VALUE obj)
553{
554 buf_T *buf;
555
556 Data_Get_Struct(obj, buf_T, buf);
557 if (buf == NULL)
558 rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
559 return buf;
560}
561
562static VALUE buffer_s_current()
563{
564 return buffer_new(curbuf);
565}
566
567static VALUE buffer_s_count()
568{
569 buf_T *b;
570 int n = 0;
571
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000572 for (b = firstbuf; b != NULL; b = b->b_next)
573 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000574 /* Deleted buffers should not be counted
575 * SegPhault - 01/07/05 */
576 if (b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000577 n++;
578 }
579
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 return INT2NUM(n);
581}
582
583static VALUE buffer_s_aref(VALUE self, VALUE num)
584{
585 buf_T *b;
586 int n = NUM2INT(num);
587
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000588 for (b = firstbuf; b != NULL; b = b->b_next)
589 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000590 /* Deleted buffers should not be counted
591 * SegPhault - 01/07/05 */
592 if (!b->b_p_bl)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000593 continue;
594
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000595 if (n == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 return buffer_new(b);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000597
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000598 n--;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 }
600 return Qnil;
601}
602
603static VALUE buffer_name(VALUE self)
604{
605 buf_T *buf = get_buf(self);
606
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000607 return buf->b_ffname ? rb_str_new2((char *)buf->b_ffname) : Qnil;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608}
609
610static VALUE buffer_number(VALUE self)
611{
612 buf_T *buf = get_buf(self);
613
614 return INT2NUM(buf->b_fnum);
615}
616
617static VALUE buffer_count(VALUE self)
618{
619 buf_T *buf = get_buf(self);
620
621 return INT2NUM(buf->b_ml.ml_line_count);
622}
623
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000624static VALUE get_buffer_line(buf_T *buf, linenr_T n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625{
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000626 if (n > 0 && n <= buf->b_ml.ml_line_count)
627 {
Bram Moolenaar35a2e192006-03-13 22:07:11 +0000628 char *line = (char *)ml_get_buf(buf, n, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 return line ? rb_str_new2(line) : Qnil;
630 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000631 rb_raise(rb_eIndexError, "index %d out of buffer", n);
632 return Qnil; /* For stop warning */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633}
634
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000635static VALUE buffer_aref(VALUE self, VALUE num)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636{
637 buf_T *buf = get_buf(self);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000638
639 if (buf != NULL)
640 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
641 return Qnil; /* For stop warning */
642}
643
644static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
645{
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000646 char *line = STR2CSTR(str);
647#ifdef FEAT_AUTOCMD
648 aco_save_T aco;
649#else
650 buf_T *save_curbuf = curbuf;
651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000653 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL)
654 {
655#ifdef FEAT_AUTOCMD
656 /* set curwin/curbuf for "buf" and save some things */
657 aucmd_prepbuf(&aco, buf);
658#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 curbuf = buf;
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000660 curwin->w_buffer = buf;
661#endif
662
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 if (u_savesub(n) == OK) {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000664 ml_replace(n, (char_u *)line, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 changed();
666#ifdef SYNTAX_HL
667 syn_changed(n); /* recompute syntax hl. for this line */
668#endif
669 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000670
671#ifdef FEAT_AUTOCMD
672 /* restore curwin/curbuf and a few other things */
673 aucmd_restbuf(&aco);
674 /* Careful: autocommands may have made "buf" invalid! */
675#else
676 curwin->w_buffer = save_curbuf;
677 curbuf = save_curbuf;
678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 update_curbuf(NOT_VALID);
680 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000681 else
682 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 rb_raise(rb_eIndexError, "index %d out of buffer", n);
684 return Qnil; /* For stop warning */
685 }
686 return str;
687}
688
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000689static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
690{
691 buf_T *buf = get_buf(self);
692
693 if (buf != NULL)
694 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
695 return str;
696}
697
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698static VALUE buffer_delete(VALUE self, VALUE num)
699{
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000700 buf_T *buf = get_buf(self);
701 long n = NUM2LONG(num);
702#ifdef FEAT_AUTOCMD
703 aco_save_T aco;
704#else
705 buf_T *save_curbuf = curbuf;
706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000708 if (n > 0 && n <= buf->b_ml.ml_line_count)
709 {
710#ifdef FEAT_AUTOCMD
711 /* set curwin/curbuf for "buf" and save some things */
712 aucmd_prepbuf(&aco, buf);
713#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 curbuf = buf;
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000715 curwin->w_buffer = buf;
716#endif
717
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 if (u_savedel(n, 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 ml_delete(n, 0);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000720
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000721 /* Changes to non-active buffers should properly refresh
722 * SegPhault - 01/09/05 */
723 deleted_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000724
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 changed();
726 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000727
728#ifdef FEAT_AUTOCMD
729 /* restore curwin/curbuf and a few other things */
730 aucmd_restbuf(&aco);
731 /* Careful: autocommands may have made "buf" invalid! */
732#else
733 curwin->w_buffer = save_curbuf;
734 curbuf = save_curbuf;
735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 update_curbuf(NOT_VALID);
737 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000738 else
739 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 rb_raise(rb_eIndexError, "index %d out of buffer", n);
741 }
742 return Qnil;
743}
744
745static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
746{
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000747 buf_T *buf = get_buf(self);
748 char *line = STR2CSTR(str);
749 long n = NUM2LONG(num);
750#ifdef FEAT_AUTOCMD
751 aco_save_T aco;
752#else
753 buf_T *save_curbuf = curbuf;
754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000756 if (n >= 0 && n <= buf->b_ml.ml_line_count && line != NULL)
757 {
758#ifdef FEAT_AUTOCMD
759 /* set curwin/curbuf for "buf" and save some things */
760 aucmd_prepbuf(&aco, buf);
761#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 curbuf = buf;
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000763 curwin->w_buffer = buf;
764#endif
765
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 if (u_inssub(n + 1) == OK) {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000768
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000769 /* Changes to non-active buffers should properly refresh screen
770 * SegPhault - 12/20/04 */
771 appended_lines_mark(n, 1L);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000772
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000773 changed();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 }
Bram Moolenaar20ff7922006-06-20 19:10:43 +0000775
776#ifdef FEAT_AUTOCMD
777 /* restore curwin/curbuf and a few other things */
778 aucmd_restbuf(&aco);
779 /* Careful: autocommands may have made "buf" invalid! */
780#else
781 curwin->w_buffer = save_curbuf;
782 curbuf = save_curbuf;
783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 update_curbuf(NOT_VALID);
785 }
786 else {
787 rb_raise(rb_eIndexError, "index %d out of buffer", n);
788 }
789 return str;
790}
791
792static VALUE window_new(win_T *win)
793{
Bram Moolenaare344bea2005-09-01 20:46:49 +0000794 if (win->w_ruby_ref)
795 {
796 return (VALUE) win->w_ruby_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 }
Bram Moolenaare344bea2005-09-01 20:46:49 +0000798 else
799 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
Bram Moolenaare344bea2005-09-01 20:46:49 +0000801 win->w_ruby_ref = (void *) obj;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 rb_hash_aset(objtbl, rb_obj_id(obj), obj);
803 return obj;
804 }
805}
806
807static win_T *get_win(VALUE obj)
808{
809 win_T *win;
810
811 Data_Get_Struct(obj, win_T, win);
812 if (win == NULL)
813 rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
814 return win;
815}
816
817static VALUE window_s_current()
818{
819 return window_new(curwin);
820}
821
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000822/*
823 * Added line manipulation functions
824 * SegPhault - 03/07/05
825 */
826static VALUE line_s_current()
827{
828 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
829}
830
831static VALUE set_current_line(VALUE str)
832{
833 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
834}
835
836static VALUE current_line_number()
837{
838 return INT2FIX((int)curwin->w_cursor.lnum);
839}
840
841
842
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843static VALUE window_s_count()
844{
845#ifdef FEAT_WINDOWS
846 win_T *w;
847 int n = 0;
848
Bram Moolenaarf740b292006-02-16 22:11:02 +0000849 for (w = firstwin; w != NULL; w = w->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 n++;
851 return INT2NUM(n);
852#else
853 return INT2NUM(1);
854#endif
855}
856
857static VALUE window_s_aref(VALUE self, VALUE num)
858{
859 win_T *w;
860 int n = NUM2INT(num);
861
862#ifndef FEAT_WINDOWS
863 w = curwin;
864#else
865 for (w = firstwin; w != NULL; w = w->w_next, --n)
866#endif
867 if (n == 0)
868 return window_new(w);
869 return Qnil;
870}
871
872static VALUE window_buffer(VALUE self)
873{
874 win_T *win = get_win(self);
875
876 return buffer_new(win->w_buffer);
877}
878
879static VALUE window_height(VALUE self)
880{
881 win_T *win = get_win(self);
882
883 return INT2NUM(win->w_height);
884}
885
886static VALUE window_set_height(VALUE self, VALUE height)
887{
888 win_T *win = get_win(self);
889 win_T *savewin = curwin;
890
891 curwin = win;
892 win_setheight(NUM2INT(height));
893 curwin = savewin;
894 return height;
895}
896
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000897static VALUE window_width(VALUE self)
898{
899 win_T *win = get_win(self);
900
901 return INT2NUM(win->w_width);
902}
903
904static VALUE window_set_width(VALUE self, VALUE width)
905{
906 win_T *win = get_win(self);
907 win_T *savewin = curwin;
908
909 curwin = win;
910 win_setwidth(NUM2INT(width));
911 curwin = savewin;
912 return width;
913}
914
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915static VALUE window_cursor(VALUE self)
916{
917 win_T *win = get_win(self);
918
919 return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
920}
921
922static VALUE window_set_cursor(VALUE self, VALUE pos)
923{
924 VALUE lnum, col;
925 win_T *win = get_win(self);
926
927 Check_Type(pos, T_ARRAY);
928 if (RARRAY(pos)->len != 2)
929 rb_raise(rb_eArgError, "array length must be 2");
930 lnum = RARRAY(pos)->ptr[0];
931 col = RARRAY(pos)->ptr[1];
932 win->w_cursor.lnum = NUM2LONG(lnum);
933 win->w_cursor.col = NUM2UINT(col);
934 check_cursor(); /* put cursor on an existing line */
935 update_screen(NOT_VALID);
936 return Qnil;
937}
938
939static VALUE f_p(int argc, VALUE *argv, VALUE self)
940{
941 int i;
942 VALUE str = rb_str_new("", 0);
943
944 for (i = 0; i < argc; i++) {
945 if (i > 0) rb_str_cat(str, ", ", 2);
946 rb_str_concat(str, rb_inspect(argv[i]));
947 }
948 MSG(RSTRING(str)->ptr);
949 return Qnil;
950}
951
952static void ruby_io_init(void)
953{
954#ifndef DYNAMIC_RUBY
955 RUBYEXTERN VALUE rb_stdout;
956#endif
957
958 rb_stdout = rb_obj_alloc(rb_cObject);
959 rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
960 rb_define_global_function("p", f_p, -1);
961}
962
963static void ruby_vim_init(void)
964{
965 objtbl = rb_hash_new();
966 rb_global_variable(&objtbl);
967
968 mVIM = rb_define_module("VIM");
969 rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
970 rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
971 rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
972 rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
973 rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
974 rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
975 rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
976 rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
977 rb_define_module_function(mVIM, "message", vim_message, 1);
978 rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
979 rb_define_module_function(mVIM, "command", vim_command, 1);
980 rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);
981
982 eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
983 rb_eStandardError);
984 eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
985 rb_eStandardError);
986
987 cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
988 rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
989 rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
990 rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
991 rb_define_method(cBuffer, "name", buffer_name, 0);
992 rb_define_method(cBuffer, "number", buffer_number, 0);
993 rb_define_method(cBuffer, "count", buffer_count, 0);
994 rb_define_method(cBuffer, "length", buffer_count, 0);
995 rb_define_method(cBuffer, "[]", buffer_aref, 1);
996 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
997 rb_define_method(cBuffer, "delete", buffer_delete, 1);
998 rb_define_method(cBuffer, "append", buffer_append, 2);
999
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001000 /* Added line manipulation functions
1001 * SegPhault - 03/07/05 */
1002 rb_define_method(cBuffer, "line_number", current_line_number, 0);
1003 rb_define_method(cBuffer, "line", line_s_current, 0);
1004 rb_define_method(cBuffer, "line=", set_current_line, 1);
1005
1006
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
1008 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
1009 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
1010 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
1011 rb_define_method(cVimWindow, "buffer", window_buffer, 0);
1012 rb_define_method(cVimWindow, "height", window_height, 0);
1013 rb_define_method(cVimWindow, "height=", window_set_height, 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001014 rb_define_method(cVimWindow, "width", window_width, 0);
1015 rb_define_method(cVimWindow, "width=", window_set_width, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 rb_define_method(cVimWindow, "cursor", window_cursor, 0);
1017 rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);
1018
1019 rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
1020 rb_define_virtual_variable("$curwin", window_s_current, 0);
1021}