summaryrefslogtreecommitdiff
path: root/src/if_ruby.c
blob: 112b91e5dba584dc5c871ce158859a4d2a5fe1e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
/* vi:set ts=8 sts=4 sw=4:
 *
 * VIM - Vi IMproved	by Bram Moolenaar
 * Ruby interface by Shugo Maeda.
 *
 * Do ":help uganda"  in Vim to read copying and usage conditions.
 * Do ":help credits" in Vim to see a list of people who contributed.
 * See README.txt for an overview of the Vim source code.
 */

#include <stdio.h>
#include <string.h>

#ifdef _WIN32
# if !defined(DYNAMIC_RUBY_VER) || (DYNAMIC_RUBY_VER < 18)
#   define NT
# endif
# ifndef DYNAMIC_RUBY
#  define IMPORT /* For static dll usage __declspec(dllimport) */
#  define RUBYEXTERN __declspec(dllimport)
# endif
#endif
#ifndef RUBYEXTERN
# define RUBYEXTERN extern
#endif

/*
 * This is tricky.  In ruby.h there is (inline) function rb_class_of()
 * definition.  This function use these variables.  But we want function to
 * use dll_* variables.
 */
#ifdef DYNAMIC_RUBY
# define rb_cFalseClass		(*dll_rb_cFalseClass)
# define rb_cFixnum		(*dll_rb_cFixnum)
# define rb_cNilClass		(*dll_rb_cNilClass)
# define rb_cSymbol		(*dll_rb_cSymbol)
# define rb_cTrueClass		(*dll_rb_cTrueClass)
# if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
/*
 * On ver 1.8, all Ruby functions are exported with "__declspce(dllimport)"
 * in ruby.h.  But it cause trouble for these variables, because it is
 * defined in this file.  When defined this RUBY_EXPORT it modified to
 * "extern" and be able to avoid this problem.
 */
#  define RUBY_EXPORT
# endif
#endif

#include <ruby.h>

#undef EXTERN
#undef _

/* T_DATA defined both by Ruby and Mac header files, hack around it... */
#ifdef FEAT_GUI_MAC
# define __OPENTRANSPORT__
# define __OPENTRANSPORTPROTOCOL__
# define __OPENTRANSPORTPROVIDERS__
#endif

#include "vim.h"
#include "version.h"

#if defined(PROTO) && !defined(FEAT_RUBY)
/* Define these to be able to generate the function prototypes. */
# define VALUE int
# define RUBY_DATA_FUNC int
#endif

static int ruby_initialized = 0;
static VALUE objtbl;

static VALUE mVIM;
static VALUE cBuffer;
static VALUE cVimWindow;
static VALUE eDeletedBufferError;
static VALUE eDeletedWindowError;

static int ensure_ruby_initialized(void);
static void error_print(int);
static void ruby_io_init(void);
static void ruby_vim_init(void);

#if defined(DYNAMIC_RUBY) || defined(PROTO)
#ifdef PROTO
# define HINSTANCE int		/* for generating prototypes */
#endif

/*
 * Wrapper defines
 */
#define rb_assoc_new			dll_rb_assoc_new
#define rb_cObject			(*dll_rb_cObject)
#define rb_check_type			dll_rb_check_type
#define rb_class_path			dll_rb_class_path
#define rb_data_object_alloc		dll_rb_data_object_alloc
#define rb_define_class_under		dll_rb_define_class_under
#define rb_define_const			dll_rb_define_const
#define rb_define_global_function	dll_rb_define_global_function
#define rb_define_method		dll_rb_define_method
#define rb_define_module		dll_rb_define_module
#define rb_define_module_function	dll_rb_define_module_function
#define rb_define_singleton_method	dll_rb_define_singleton_method
#define rb_define_virtual_variable	dll_rb_define_virtual_variable
#define rb_stdout			(*dll_rb_stdout)
#define rb_eArgError			(*dll_rb_eArgError)
#define rb_eIndexError			(*dll_rb_eIndexError)
#define rb_eRuntimeError		(*dll_rb_eRuntimeError)
#define rb_eStandardError		(*dll_rb_eStandardError)
#define rb_eval_string_protect		dll_rb_eval_string_protect
#define rb_global_variable		dll_rb_global_variable
#define rb_hash_aset			dll_rb_hash_aset
#define rb_hash_new			dll_rb_hash_new
#define rb_inspect			dll_rb_inspect
#define rb_int2inum			dll_rb_int2inum
#define rb_lastline_get			dll_rb_lastline_get
#define rb_lastline_set			dll_rb_lastline_set
#define rb_load_protect			dll_rb_load_protect
#define rb_num2long			dll_rb_num2long
#define rb_num2ulong			dll_rb_num2ulong
#define rb_obj_alloc			dll_rb_obj_alloc
#define rb_obj_as_string		dll_rb_obj_as_string
#define rb_obj_id			dll_rb_obj_id
#define rb_raise			dll_rb_raise
#define rb_str2cstr			dll_rb_str2cstr
#define rb_str_cat			dll_rb_str_cat
#define rb_str_concat			dll_rb_str_concat
#define rb_str_new			dll_rb_str_new
#define rb_str_new2			dll_rb_str_new2
#define ruby_errinfo			(*dll_ruby_errinfo)
#define ruby_init			dll_ruby_init
#define ruby_init_loadpath		dll_ruby_init_loadpath
#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
# define rb_w32_snprintf		dll_rb_w32_snprintf
#endif

/*
 * Pointers for dynamic link
 */
static VALUE (*dll_rb_assoc_new) (VALUE, VALUE);
static VALUE *dll_rb_cFalseClass;
static VALUE *dll_rb_cFixnum;
static VALUE *dll_rb_cNilClass;
static VALUE *dll_rb_cObject;
static VALUE *dll_rb_cSymbol;
static VALUE *dll_rb_cTrueClass;
static void (*dll_rb_check_type) (VALUE,int);
static VALUE (*dll_rb_class_path) (VALUE);
static VALUE (*dll_rb_data_object_alloc) (VALUE, void*, RUBY_DATA_FUNC, RUBY_DATA_FUNC);
static VALUE (*dll_rb_define_class_under) (VALUE, const char*, VALUE);
static void (*dll_rb_define_const) (VALUE,const char*,VALUE);
static void (*dll_rb_define_global_function) (const char*,VALUE(*)(),int);
static void (*dll_rb_define_method) (VALUE,const char*,VALUE(*)(),int);
static VALUE (*dll_rb_define_module) (const char*);
static void (*dll_rb_define_module_function) (VALUE,const char*,VALUE(*)(),int);
static void (*dll_rb_define_singleton_method) (VALUE,const char*,VALUE(*)(),int);
static void (*dll_rb_define_virtual_variable) (const char*,VALUE(*)(),void(*)());
static VALUE *dll_rb_stdout;
static VALUE *dll_rb_eArgError;
static VALUE *dll_rb_eIndexError;
static VALUE *dll_rb_eRuntimeError;
static VALUE *dll_rb_eStandardError;
static VALUE (*dll_rb_eval_string_protect) (const char*, int*);
static void (*dll_rb_global_variable) (VALUE*);
static VALUE (*dll_rb_hash_aset) (VALUE, VALUE, VALUE);
static VALUE (*dll_rb_hash_new) (void);
static VALUE (*dll_rb_inspect) (VALUE);
static VALUE (*dll_rb_int2inum) (long);
static VALUE (*dll_rb_int2inum) (long);
static VALUE (*dll_rb_lastline_get) (void);
static void (*dll_rb_lastline_set) (VALUE);
static void (*dll_rb_load_protect) (VALUE, int, int*);
static long (*dll_rb_num2long) (VALUE);
static unsigned long (*dll_rb_num2ulong) (VALUE);
static VALUE (*dll_rb_obj_alloc) (VALUE);
static VALUE (*dll_rb_obj_as_string) (VALUE);
static VALUE (*dll_rb_obj_id) (VALUE);
static void (*dll_rb_raise) (VALUE, const char*, ...);
static char *(*dll_rb_str2cstr) (VALUE,int*);
static VALUE (*dll_rb_str_cat) (VALUE, const char*, long);
static VALUE (*dll_rb_str_concat) (VALUE, VALUE);
static VALUE (*dll_rb_str_new) (const char*, long);
static VALUE (*dll_rb_str_new2) (const char*);
static VALUE *dll_ruby_errinfo;
static void (*dll_ruby_init) (void);
static void (*dll_ruby_init_loadpath) (void);
#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
static int (*dll_rb_w32_snprintf)(char*, size_t, const char*, ...);
#endif

static HINSTANCE hinstRuby = 0; /* Instance of ruby.dll */

/*
 * Table of name to function pointer of python.
 */
#define RUBY_PROC FARPROC
static struct
{
    char *name;
    RUBY_PROC *ptr;
} ruby_funcname_table[] =
{
    {"rb_assoc_new", (RUBY_PROC*)&dll_rb_assoc_new},
    {"rb_cFalseClass", (RUBY_PROC*)&dll_rb_cFalseClass},
    {"rb_cFixnum", (RUBY_PROC*)&dll_rb_cFixnum},
    {"rb_cNilClass", (RUBY_PROC*)&dll_rb_cNilClass},
    {"rb_cObject", (RUBY_PROC*)&dll_rb_cObject},
    {"rb_cSymbol", (RUBY_PROC*)&dll_rb_cSymbol},
    {"rb_cTrueClass", (RUBY_PROC*)&dll_rb_cTrueClass},
    {"rb_check_type", (RUBY_PROC*)&dll_rb_check_type},
    {"rb_class_path", (RUBY_PROC*)&dll_rb_class_path},
    {"rb_data_object_alloc", (RUBY_PROC*)&dll_rb_data_object_alloc},
    {"rb_define_class_under", (RUBY_PROC*)&dll_rb_define_class_under},
    {"rb_define_const", (RUBY_PROC*)&dll_rb_define_const},
    {"rb_define_global_function", (RUBY_PROC*)&dll_rb_define_global_function},
    {"rb_define_method", (RUBY_PROC*)&dll_rb_define_method},
    {"rb_define_module", (RUBY_PROC*)&dll_rb_define_module},
    {"rb_define_module_function", (RUBY_PROC*)&dll_rb_define_module_function},
    {"rb_define_singleton_method", (RUBY_PROC*)&dll_rb_define_singleton_method},
    {"rb_define_virtual_variable", (RUBY_PROC*)&dll_rb_define_virtual_variable},
    {"rb_stdout", (RUBY_PROC*)&dll_rb_stdout},
    {"rb_eArgError", (RUBY_PROC*)&dll_rb_eArgError},
    {"rb_eIndexError", (RUBY_PROC*)&dll_rb_eIndexError},
    {"rb_eRuntimeError", (RUBY_PROC*)&dll_rb_eRuntimeError},
    {"rb_eStandardError", (RUBY_PROC*)&dll_rb_eStandardError},
    {"rb_eval_string_protect", (RUBY_PROC*)&dll_rb_eval_string_protect},
    {"rb_global_variable", (RUBY_PROC*)&dll_rb_global_variable},
    {"rb_hash_aset", (RUBY_PROC*)&dll_rb_hash_aset},
    {"rb_hash_new", (RUBY_PROC*)&dll_rb_hash_new},
    {"rb_inspect", (RUBY_PROC*)&dll_rb_inspect},
    {"rb_int2inum", (RUBY_PROC*)&dll_rb_int2inum},
    {"rb_lastline_get", (RUBY_PROC*)&dll_rb_lastline_get},
    {"rb_lastline_set", (RUBY_PROC*)&dll_rb_lastline_set},
    {"rb_load_protect", (RUBY_PROC*)&dll_rb_load_protect},
    {"rb_num2long", (RUBY_PROC*)&dll_rb_num2long},
    {"rb_num2ulong", (RUBY_PROC*)&dll_rb_num2ulong},
    {"rb_obj_alloc", (RUBY_PROC*)&dll_rb_obj_alloc},
    {"rb_obj_as_string", (RUBY_PROC*)&dll_rb_obj_as_string},
    {"rb_obj_id", (RUBY_PROC*)&dll_rb_obj_id},
    {"rb_raise", (RUBY_PROC*)&dll_rb_raise},
    {"rb_str2cstr", (RUBY_PROC*)&dll_rb_str2cstr},
    {"rb_str_cat", (RUBY_PROC*)&dll_rb_str_cat},
    {"rb_str_concat", (RUBY_PROC*)&dll_rb_str_concat},
    {"rb_str_new", (RUBY_PROC*)&dll_rb_str_new},
    {"rb_str_new2", (RUBY_PROC*)&dll_rb_str_new2},
    {"ruby_errinfo", (RUBY_PROC*)&dll_ruby_errinfo},
    {"ruby_init", (RUBY_PROC*)&dll_ruby_init},
    {"ruby_init_loadpath", (RUBY_PROC*)&dll_ruby_init_loadpath},
#if defined(DYNAMIC_RUBY_VER) && DYNAMIC_RUBY_VER >= 18
    {"rb_w32_snprintf", (RUBY_PROC*)&dll_rb_w32_snprintf},
#endif
    {"", NULL},
};

/*
 * Free ruby.dll
 */
    static void
end_dynamic_ruby()
{
    if (hinstRuby)
    {
	FreeLibrary(hinstRuby);
	hinstRuby = 0;
    }
}

/*
 * Load library and get all pointers.
 * Parameter 'libname' provides name of DLL.
 * Return OK or FAIL.
 */
    static int
ruby_runtime_link_init(char *libname, int verbose)
{
    int i;

    if (hinstRuby)
	return OK;
    hinstRuby = LoadLibrary(libname);
    if (!hinstRuby)
    {
	if (verbose)
	    EMSG2(_(e_loadlib), libname);
	return FAIL;
    }

    for (i = 0; ruby_funcname_table[i].ptr; ++i)
    {
	if (!(*ruby_funcname_table[i].ptr = GetProcAddress(hinstRuby,
			ruby_funcname_table[i].name)))
	{
	    FreeLibrary(hinstRuby);
	    hinstRuby = 0;
	    if (verbose)
		EMSG2(_(e_loadfunc), ruby_funcname_table[i].name);
	    return FAIL;
	}
    }
    return OK;
}

/*
 * If ruby is enabled (there is installed ruby on Windows system) return TRUE,
 * else FALSE.
 */
    int
ruby_enabled(verbose)
    int		verbose;
{
    return ruby_runtime_link_init(DYNAMIC_RUBY_DLL, verbose) == OK;
}
#endif /* defined(DYNAMIC_RUBY) || defined(PROTO) */

    void
ruby_end()
{
#ifdef DYNAMIC_RUBY
    end_dynamic_ruby();
#endif
}

void ex_ruby(exarg_T *eap)
{
    int state;
    char *script = NULL;

    script = script_get(eap, eap->arg);
    if (!eap->skip && ensure_ruby_initialized())
    {
	if (script == NULL)
	    rb_eval_string_protect((char *)eap->arg, &state);
	else
	    rb_eval_string_protect(script, &state);
	if (state)
	    error_print(state);
    }
    vim_free(script);
}

void ex_rubydo(exarg_T *eap)
{
    int state;
    linenr_T i;

    if (ensure_ruby_initialized())
    {
	if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
	    return;
	for (i = eap->line1; i <= eap->line2; i++) {
	    VALUE line, oldline;

	    line = oldline = rb_str_new2(ml_get(i));
	    rb_lastline_set(line);
	    rb_eval_string_protect((char *) eap->arg, &state);
	    if (state) {
		error_print(state);
		break;
	    }
	    line = rb_lastline_get();
	    if (!NIL_P(line)) {
		if (TYPE(line) != T_STRING) {
		    EMSG("E265: $_ must be an instance of String");
		    return;
		}
		ml_replace(i, (char_u *) STR2CSTR(line), 1);
		changed();
#ifdef SYNTAX_HL
		syn_changed(i); /* recompute syntax hl. for this line */
#endif
	    }
	}
	check_cursor();
	update_curbuf(NOT_VALID);
    }
}

void ex_rubyfile(exarg_T *eap)
{
    int state;

    if (ensure_ruby_initialized())
    {
	rb_load_protect(rb_str_new2((char *) eap->arg), 0, &state);
	if (state) error_print(state);
    }
}

void ruby_buffer_free(buf_T *buf)
{
    if (buf->ruby_ref) {
	rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->ruby_ref), Qnil);
	RDATA(buf->ruby_ref)->data = NULL;
    }
}

void ruby_window_free(win_T *win)
{
    if (win->ruby_ref) {
	rb_hash_aset(objtbl, rb_obj_id((VALUE) win->ruby_ref), Qnil);
	RDATA(win->ruby_ref)->data = NULL;
    }
}

static int ensure_ruby_initialized(void)
{
    if (!ruby_initialized)
    {
#ifdef DYNAMIC_RUBY
	if (ruby_enabled(TRUE))
	{
#endif
	    ruby_init();
	    ruby_init_loadpath();
	    ruby_io_init();
	    ruby_vim_init();
	    ruby_initialized = 1;
#ifdef DYNAMIC_RUBY
	}
	else
	{
	    EMSG(_("E266: Sorry, this command is disabled, the Ruby library could not be loaded."));
	    return 0;
	}
#endif
    }
    return ruby_initialized;
}

static void error_print(int state)
{
#ifndef DYNAMIC_RUBY
    RUBYEXTERN VALUE ruby_errinfo;
#endif
    VALUE eclass;
    VALUE einfo;
    char buff[BUFSIZ];

#define TAG_RETURN	0x1
#define TAG_BREAK	0x2
#define TAG_NEXT	0x3
#define TAG_RETRY	0x4
#define TAG_REDO	0x5
#define TAG_RAISE	0x6
#define TAG_THROW	0x7
#define TAG_FATAL	0x8
#define TAG_MASK	0xf

    switch (state) {
    case TAG_RETURN:
	EMSG("E267: unexpected return");
	break;
    case TAG_NEXT:
	EMSG("E268: unexpected next");
	break;
    case TAG_BREAK:
	EMSG("E269: unexpected break");
	break;
    case TAG_REDO:
	EMSG("E270: unexpected redo");
	break;
    case TAG_RETRY:
	EMSG("E271: retry outside of rescue clause");
	break;
    case TAG_RAISE:
    case TAG_FATAL:
	eclass = CLASS_OF(ruby_errinfo);
	einfo = rb_obj_as_string(ruby_errinfo);
	if (eclass == rb_eRuntimeError && RSTRING(einfo)->len == 0) {
	    EMSG("E272: unhandled exception");
	}
	else {
	    VALUE epath;
	    char *p;

	    epath = rb_class_path(eclass);
	    vim_snprintf(buff, BUFSIZ, "%s: %s",
		     RSTRING(epath)->ptr, RSTRING(einfo)->ptr);
	    p = strchr(buff, '\n');
	    if (p) *p = '\0';
	    EMSG(buff);
	}
	break;
    default:
	vim_snprintf(buff, BUFSIZ, _("E273: unknown longjmp status %d"), state);
	EMSG(buff);
	break;
    }
}

static VALUE vim_message(VALUE self, VALUE str)
{
    char *buff, *p;

    str = rb_obj_as_string(str);
    buff = ALLOCA_N(char, RSTRING(str)->len);
    strcpy(buff, RSTRING(str)->ptr);
    p = strchr(buff, '\n');
    if (p) *p = '\0';
    MSG(buff);
    return Qnil;
}

static VALUE vim_set_option(VALUE self, VALUE str)
{
    do_set((char_u *)STR2CSTR(str), 0);
    update_screen(NOT_VALID);
    return Qnil;
}

static VALUE vim_command(VALUE self, VALUE str)
{
    do_cmdline_cmd((char_u *)STR2CSTR(str));
    return Qnil;
}

static VALUE vim_evaluate(VALUE self, VALUE str)
{
#ifdef FEAT_EVAL
    char_u *value = eval_to_string((char_u *)STR2CSTR(str), NULL);

    if (value)
    {
	VALUE val = rb_str_new2(value);
	vim_free(value);
	return val;
    }
    else
#endif
	return Qnil;
}

static VALUE buffer_new(buf_T *buf)
{
    if (buf->ruby_ref) {
	return (VALUE) buf->ruby_ref;
    }
    else {
	VALUE obj = Data_Wrap_Struct(cBuffer, 0, 0, buf);
	buf->ruby_ref = (void *) obj;
	rb_hash_aset(objtbl, rb_obj_id(obj), obj);
	return obj;
    }
}

static buf_T *get_buf(VALUE obj)
{
    buf_T *buf;

    Data_Get_Struct(obj, buf_T, buf);
    if (buf == NULL)
	rb_raise(eDeletedBufferError, "attempt to refer to deleted buffer");
    return buf;
}

static VALUE buffer_s_current()
{
    return buffer_new(curbuf);
}

static VALUE buffer_s_count()
{
    buf_T *b;
    int n = 0;

    for (b = firstbuf; b; b = b->b_next) n++;
    return INT2NUM(n);
}

static VALUE buffer_s_aref(VALUE self, VALUE num)
{
    buf_T *b;
    int n = NUM2INT(num);

    for (b = firstbuf; b; b = b->b_next, --n) {
	if (n == 0)
	    return buffer_new(b);
    }
    return Qnil;
}

static VALUE buffer_name(VALUE self)
{
    buf_T *buf = get_buf(self);

    return buf->b_ffname ? rb_str_new2(buf->b_ffname) : Qnil;
}

static VALUE buffer_number(VALUE self)
{
    buf_T *buf = get_buf(self);

    return INT2NUM(buf->b_fnum);
}

static VALUE buffer_count(VALUE self)
{
    buf_T *buf = get_buf(self);

    return INT2NUM(buf->b_ml.ml_line_count);
}

static VALUE buffer_aref(VALUE self, VALUE num)
{
    buf_T *buf = get_buf(self);
    long n = NUM2LONG(num);

    if (n > 0 && n <= buf->b_ml.ml_line_count) {
	char *line = ml_get_buf(buf, n, FALSE);
	return line ? rb_str_new2(line) : Qnil;
    }
    else {
	rb_raise(rb_eIndexError, "index %d out of buffer", n);
	return Qnil; /* For stop warning */
    }
}

static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
{
    buf_T *buf = get_buf(self);
    buf_T *savebuf = curbuf;
    char *line = STR2CSTR(str);
    long n = NUM2LONG(num);

    if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL) {
	curbuf = buf;
	if (u_savesub(n) == OK) {
	    ml_replace(n, (char_u *) line, TRUE);
	    changed();
#ifdef SYNTAX_HL
	    syn_changed(n); /* recompute syntax hl. for this line */
#endif
	}
	curbuf = savebuf;
	update_curbuf(NOT_VALID);
    }
    else {
	rb_raise(rb_eIndexError, "index %d out of buffer", n);
	return Qnil; /* For stop warning */
    }
    return str;
}

static VALUE buffer_delete(VALUE self, VALUE num)
{
    buf_T *buf = get_buf(self);
    buf_T *savebuf = curbuf;
    long n = NUM2LONG(num);

    if (n > 0 && n <= buf->b_ml.ml_line_count) {
	curbuf = buf;
	if (u_savedel(n, 1) == OK) {
	    mark_adjust(n, n, MAXLNUM, -1);
	    ml_delete(n, 0);
	    changed();
	}
	curbuf = savebuf;
	update_curbuf(NOT_VALID);
    }
    else {
	rb_raise(rb_eIndexError, "index %d out of buffer", n);
    }
    return Qnil;
}

static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
{
    buf_T *buf = get_buf(self);
    buf_T *savebuf = curbuf;
    char *line = STR2CSTR(str);
    long n = NUM2LONG(num);

    if (n >= 0 && n <= buf->b_ml.ml_line_count && line != NULL) {
	curbuf = buf;
	if (u_inssub(n + 1) == OK) {
	    mark_adjust(n + 1, MAXLNUM, 1L, 0L);
	    ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
	    changed();
	}
	curbuf = savebuf;
	update_curbuf(NOT_VALID);
    }
    else {
	rb_raise(rb_eIndexError, "index %d out of buffer", n);
    }
    return str;
}

static VALUE window_new(win_T *win)
{
    if (win->ruby_ref) {
	return (VALUE) win->ruby_ref;
    }
    else {
	VALUE obj = Data_Wrap_Struct(cVimWindow, 0, 0, win);
	win->ruby_ref = (void *) obj;
	rb_hash_aset(objtbl, rb_obj_id(obj), obj);
	return obj;
    }
}

static win_T *get_win(VALUE obj)
{
    win_T *win;

    Data_Get_Struct(obj, win_T, win);
    if (win == NULL)
	rb_raise(eDeletedWindowError, "attempt to refer to deleted window");
    return win;
}

static VALUE window_s_current()
{
    return window_new(curwin);
}

static VALUE window_s_count()
{
#ifdef FEAT_WINDOWS
    win_T	*w;
    int n = 0;

    for (w = firstwin; w; w = w->w_next)
	n++;
    return INT2NUM(n);
#else
    return INT2NUM(1);
#endif
}

static VALUE window_s_aref(VALUE self, VALUE num)
{
    win_T *w;
    int n = NUM2INT(num);

#ifndef FEAT_WINDOWS
    w = curwin;
#else
    for (w = firstwin; w != NULL; w = w->w_next, --n)
#endif
	if (n == 0)
	    return window_new(w);
    return Qnil;
}

static VALUE window_buffer(VALUE self)
{
    win_T *win = get_win(self);

    return buffer_new(win->w_buffer);
}

static VALUE window_height(VALUE self)
{
    win_T *win = get_win(self);

    return INT2NUM(win->w_height);
}

static VALUE window_set_height(VALUE self, VALUE height)
{
    win_T *win = get_win(self);
    win_T *savewin = curwin;

    curwin = win;
    win_setheight(NUM2INT(height));
    curwin = savewin;
    return height;
}

static VALUE window_cursor(VALUE self)
{
    win_T *win = get_win(self);

    return rb_assoc_new(INT2NUM(win->w_cursor.lnum), INT2NUM(win->w_cursor.col));
}

static VALUE window_set_cursor(VALUE self, VALUE pos)
{
    VALUE lnum, col;
    win_T *win = get_win(self);

    Check_Type(pos, T_ARRAY);
    if (RARRAY(pos)->len != 2)
	rb_raise(rb_eArgError, "array length must be 2");
    lnum = RARRAY(pos)->ptr[0];
    col = RARRAY(pos)->ptr[1];
    win->w_cursor.lnum = NUM2LONG(lnum);
    win->w_cursor.col = NUM2UINT(col);
    check_cursor();		    /* put cursor on an existing line */
    update_screen(NOT_VALID);
    return Qnil;
}

static VALUE f_p(int argc, VALUE *argv, VALUE self)
{
    int i;
    VALUE str = rb_str_new("", 0);

    for (i = 0; i < argc; i++) {
	if (i > 0) rb_str_cat(str, ", ", 2);
	rb_str_concat(str, rb_inspect(argv[i]));
    }
    MSG(RSTRING(str)->ptr);
    return Qnil;
}

static void ruby_io_init(void)
{
#ifndef DYNAMIC_RUBY
    RUBYEXTERN VALUE rb_stdout;
#endif

    rb_stdout = rb_obj_alloc(rb_cObject);
    rb_define_singleton_method(rb_stdout, "write", vim_message, 1);
    rb_define_global_function("p", f_p, -1);
}

static void ruby_vim_init(void)
{
    objtbl = rb_hash_new();
    rb_global_variable(&objtbl);

    mVIM = rb_define_module("VIM");
    rb_define_const(mVIM, "VERSION_MAJOR", INT2NUM(VIM_VERSION_MAJOR));
    rb_define_const(mVIM, "VERSION_MINOR", INT2NUM(VIM_VERSION_MINOR));
    rb_define_const(mVIM, "VERSION_BUILD", INT2NUM(VIM_VERSION_BUILD));
    rb_define_const(mVIM, "VERSION_PATCHLEVEL", INT2NUM(VIM_VERSION_PATCHLEVEL));
    rb_define_const(mVIM, "VERSION_SHORT", rb_str_new2(VIM_VERSION_SHORT));
    rb_define_const(mVIM, "VERSION_MEDIUM", rb_str_new2(VIM_VERSION_MEDIUM));
    rb_define_const(mVIM, "VERSION_LONG", rb_str_new2(VIM_VERSION_LONG));
    rb_define_const(mVIM, "VERSION_LONG_DATE", rb_str_new2(VIM_VERSION_LONG_DATE));
    rb_define_module_function(mVIM, "message", vim_message, 1);
    rb_define_module_function(mVIM, "set_option", vim_set_option, 1);
    rb_define_module_function(mVIM, "command", vim_command, 1);
    rb_define_module_function(mVIM, "evaluate", vim_evaluate, 1);

    eDeletedBufferError = rb_define_class_under(mVIM, "DeletedBufferError",
						rb_eStandardError);
    eDeletedWindowError = rb_define_class_under(mVIM, "DeletedWindowError",
						rb_eStandardError);

    cBuffer = rb_define_class_under(mVIM, "Buffer", rb_cObject);
    rb_define_singleton_method(cBuffer, "current", buffer_s_current, 0);
    rb_define_singleton_method(cBuffer, "count", buffer_s_count, 0);
    rb_define_singleton_method(cBuffer, "[]", buffer_s_aref, 1);
    rb_define_method(cBuffer, "name", buffer_name, 0);
    rb_define_method(cBuffer, "number", buffer_number, 0);
    rb_define_method(cBuffer, "count", buffer_count, 0);
    rb_define_method(cBuffer, "length", buffer_count, 0);
    rb_define_method(cBuffer, "[]", buffer_aref, 1);
    rb_define_method(cBuffer, "[]=", buffer_aset, 2);
    rb_define_method(cBuffer, "delete", buffer_delete, 1);
    rb_define_method(cBuffer, "append", buffer_append, 2);

    cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
    rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
    rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
    rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
    rb_define_method(cVimWindow, "buffer", window_buffer, 0);
    rb_define_method(cVimWindow, "height", window_height, 0);
    rb_define_method(cVimWindow, "height=", window_set_height, 1);
    rb_define_method(cVimWindow, "cursor", window_cursor, 0);
    rb_define_method(cVimWindow, "cursor=", window_set_cursor, 1);

    rb_define_virtual_variable("$curbuf", buffer_s_current, 0);
    rb_define_virtual_variable("$curwin", window_s_current, 0);
}