comparison src/if_ruby.c @ 779:fb913578cbf5

updated for version 7.0228
author vimboss
date Sat, 18 Mar 2006 21:30:13 +0000
parents e2f45cbe2b8a
children 8bebcabccc2c
comparison
equal deleted inserted replaced
778:c3f63ed316b6 779:fb913578cbf5
1 /* vi:set ts=8 sts=4 sw=4: 1 /* vi:set ts=8 sts=4 sw=4:
2 * 2 *
3 * VIM - Vi IMproved by Bram Moolenaar 3 * VIM - Vi IMproved by Bram Moolenaar
4 * Ruby interface by Shugo Maeda. 4 *
5 * Ruby interface by Shugo Maeda
6 * with improvements by SegPhault (Ryan Paul)
5 * 7 *
6 * Do ":help uganda" in Vim to read copying and usage conditions. 8 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed. 9 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code. 10 * See README.txt for an overview of the Vim source code.
9 */ 11 */
565 static VALUE buffer_s_count() 567 static VALUE buffer_s_count()
566 { 568 {
567 buf_T *b; 569 buf_T *b;
568 int n = 0; 570 int n = 0;
569 571
570 for (b = firstbuf; b; b = b->b_next) n++; 572 for (b = firstbuf; b != NULL; b = b->b_next)
573 {
574 /* Deleted buffers should not be counted
575 * SegPhault - 01/07/05 */
576 if (b->b_p_bl)
577 n++;
578 }
579
571 return INT2NUM(n); 580 return INT2NUM(n);
572 } 581 }
573 582
574 static VALUE buffer_s_aref(VALUE self, VALUE num) 583 static VALUE buffer_s_aref(VALUE self, VALUE num)
575 { 584 {
576 buf_T *b; 585 buf_T *b;
577 int n = NUM2INT(num); 586 int n = NUM2INT(num);
578 587
579 for (b = firstbuf; b; b = b->b_next, --n) { 588 for (b = firstbuf; b != NULL; b = b->b_next)
580 if (n == 0) 589 {
590 /* Deleted buffers should not be counted
591 * SegPhault - 01/07/05 */
592 if (!b->b_p_bl)
593 continue;
594
595 if (n == 0)
581 return buffer_new(b); 596 return buffer_new(b);
597
598 n--;
582 } 599 }
583 return Qnil; 600 return Qnil;
584 } 601 }
585 602
586 static VALUE buffer_name(VALUE self) 603 static VALUE buffer_name(VALUE self)
602 buf_T *buf = get_buf(self); 619 buf_T *buf = get_buf(self);
603 620
604 return INT2NUM(buf->b_ml.ml_line_count); 621 return INT2NUM(buf->b_ml.ml_line_count);
605 } 622 }
606 623
607 static VALUE buffer_aref(VALUE self, VALUE num) 624 static VALUE get_buffer_line(buf_T *buf, linenr_T n)
608 { 625 {
609 buf_T *buf = get_buf(self); 626 if (n > 0 && n <= buf->b_ml.ml_line_count)
610 long n = NUM2LONG(num); 627 {
611
612 if (n > 0 && n <= buf->b_ml.ml_line_count) {
613 char *line = (char *)ml_get_buf(buf, n, FALSE); 628 char *line = (char *)ml_get_buf(buf, n, FALSE);
614 return line ? rb_str_new2(line) : Qnil; 629 return line ? rb_str_new2(line) : Qnil;
630 }
631 rb_raise(rb_eIndexError, "index %d out of buffer", n);
632 return Qnil; /* For stop warning */
633 }
634
635 static VALUE buffer_aref(VALUE self, VALUE num)
636 {
637 buf_T *buf = get_buf(self);
638
639 if (buf != NULL)
640 return get_buffer_line(buf, (linenr_T)NUM2LONG(num));
641 return Qnil; /* For stop warning */
642 }
643
644 static VALUE set_buffer_line(buf_T *buf, linenr_T n, VALUE str)
645 {
646 buf_T *savebuf = curbuf;
647 char *line = STR2CSTR(str);
648
649 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL) {
650 curbuf = buf;
651 if (u_savesub(n) == OK) {
652 ml_replace(n, (char_u *)line, TRUE);
653 changed();
654 #ifdef SYNTAX_HL
655 syn_changed(n); /* recompute syntax hl. for this line */
656 #endif
657 }
658 curbuf = savebuf;
659 update_curbuf(NOT_VALID);
615 } 660 }
616 else { 661 else {
617 rb_raise(rb_eIndexError, "index %d out of buffer", n); 662 rb_raise(rb_eIndexError, "index %d out of buffer", n);
618 return Qnil; /* For stop warning */ 663 return Qnil; /* For stop warning */
619 } 664 }
665 return str;
620 } 666 }
621 667
622 static VALUE buffer_aset(VALUE self, VALUE num, VALUE str) 668 static VALUE buffer_aset(VALUE self, VALUE num, VALUE str)
669 {
670 buf_T *buf = get_buf(self);
671
672 if (buf != NULL)
673 return set_buffer_line(buf, (linenr_T)NUM2LONG(num), str);
674 return str;
675 }
676
677 static VALUE buffer_delete(VALUE self, VALUE num)
678 {
679 buf_T *buf = get_buf(self);
680 buf_T *savebuf = curbuf;
681 long n = NUM2LONG(num);
682
683 if (n > 0 && n <= buf->b_ml.ml_line_count) {
684 curbuf = buf;
685 if (u_savedel(n, 1) == OK) {
686 ml_delete(n, 0);
687
688 /* Changes to non-active buffers should properly refresh
689 * SegPhault - 01/09/05 */
690 deleted_lines_mark(n, 1L);
691
692 changed();
693 }
694 curbuf = savebuf;
695 update_curbuf(NOT_VALID);
696 }
697 else {
698 rb_raise(rb_eIndexError, "index %d out of buffer", n);
699 }
700 return Qnil;
701 }
702
703 static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
623 { 704 {
624 buf_T *buf = get_buf(self); 705 buf_T *buf = get_buf(self);
625 buf_T *savebuf = curbuf; 706 buf_T *savebuf = curbuf;
626 char *line = STR2CSTR(str); 707 char *line = STR2CSTR(str);
627 long n = NUM2LONG(num); 708 long n = NUM2LONG(num);
628 709
629 if (n > 0 && n <= buf->b_ml.ml_line_count && line != NULL) {
630 curbuf = buf;
631 if (u_savesub(n) == OK) {
632 ml_replace(n, (char_u *) line, TRUE);
633 changed();
634 #ifdef SYNTAX_HL
635 syn_changed(n); /* recompute syntax hl. for this line */
636 #endif
637 }
638 curbuf = savebuf;
639 update_curbuf(NOT_VALID);
640 }
641 else {
642 rb_raise(rb_eIndexError, "index %d out of buffer", n);
643 return Qnil; /* For stop warning */
644 }
645 return str;
646 }
647
648 static VALUE buffer_delete(VALUE self, VALUE num)
649 {
650 buf_T *buf = get_buf(self);
651 buf_T *savebuf = curbuf;
652 long n = NUM2LONG(num);
653
654 if (n > 0 && n <= buf->b_ml.ml_line_count) {
655 curbuf = buf;
656 if (u_savedel(n, 1) == OK) {
657 mark_adjust(n, n, MAXLNUM, -1);
658 ml_delete(n, 0);
659 changed();
660 }
661 curbuf = savebuf;
662 update_curbuf(NOT_VALID);
663 }
664 else {
665 rb_raise(rb_eIndexError, "index %d out of buffer", n);
666 }
667 return Qnil;
668 }
669
670 static VALUE buffer_append(VALUE self, VALUE num, VALUE str)
671 {
672 buf_T *buf = get_buf(self);
673 buf_T *savebuf = curbuf;
674 char *line = STR2CSTR(str);
675 long n = NUM2LONG(num);
676
677 if (n >= 0 && n <= buf->b_ml.ml_line_count && line != NULL) { 710 if (n >= 0 && n <= buf->b_ml.ml_line_count && line != NULL) {
678 curbuf = buf; 711 curbuf = buf;
679 if (u_inssub(n + 1) == OK) { 712 if (u_inssub(n + 1) == OK) {
680 mark_adjust(n + 1, MAXLNUM, 1L, 0L);
681 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE); 713 ml_append(n, (char_u *) line, (colnr_T) 0, FALSE);
682 changed(); 714
715 /* Changes to non-active buffers should properly refresh screen
716 * SegPhault - 12/20/04 */
717 appended_lines_mark(n, 1L);
718
719 changed();
683 } 720 }
684 curbuf = savebuf; 721 curbuf = savebuf;
685 update_curbuf(NOT_VALID); 722 update_curbuf(NOT_VALID);
686 } 723 }
687 else { 724 else {
717 754
718 static VALUE window_s_current() 755 static VALUE window_s_current()
719 { 756 {
720 return window_new(curwin); 757 return window_new(curwin);
721 } 758 }
759
760 /*
761 * Added line manipulation functions
762 * SegPhault - 03/07/05
763 */
764 static VALUE line_s_current()
765 {
766 return get_buffer_line(curbuf, curwin->w_cursor.lnum);
767 }
768
769 static VALUE set_current_line(VALUE str)
770 {
771 return set_buffer_line(curbuf, curwin->w_cursor.lnum, str);
772 }
773
774 static VALUE current_line_number()
775 {
776 return INT2FIX((int)curwin->w_cursor.lnum);
777 }
778
779
722 780
723 static VALUE window_s_count() 781 static VALUE window_s_count()
724 { 782 {
725 #ifdef FEAT_WINDOWS 783 #ifdef FEAT_WINDOWS
726 win_T *w; 784 win_T *w;
875 rb_define_method(cBuffer, "[]", buffer_aref, 1); 933 rb_define_method(cBuffer, "[]", buffer_aref, 1);
876 rb_define_method(cBuffer, "[]=", buffer_aset, 2); 934 rb_define_method(cBuffer, "[]=", buffer_aset, 2);
877 rb_define_method(cBuffer, "delete", buffer_delete, 1); 935 rb_define_method(cBuffer, "delete", buffer_delete, 1);
878 rb_define_method(cBuffer, "append", buffer_append, 2); 936 rb_define_method(cBuffer, "append", buffer_append, 2);
879 937
938 /* Added line manipulation functions
939 * SegPhault - 03/07/05 */
940 rb_define_method(cBuffer, "line_number", current_line_number, 0);
941 rb_define_method(cBuffer, "line", line_s_current, 0);
942 rb_define_method(cBuffer, "line=", set_current_line, 1);
943
944
880 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject); 945 cVimWindow = rb_define_class_under(mVIM, "Window", rb_cObject);
881 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0); 946 rb_define_singleton_method(cVimWindow, "current", window_s_current, 0);
882 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0); 947 rb_define_singleton_method(cVimWindow, "count", window_s_count, 0);
883 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1); 948 rb_define_singleton_method(cVimWindow, "[]", window_s_aref, 1);
884 rb_define_method(cVimWindow, "buffer", window_buffer, 0); 949 rb_define_method(cVimWindow, "buffer", window_buffer, 0);