/* vi:set sw=4:

    rbtextbuf.c

    Copyright (c) 2000 Minero Aoki <aamine@dp.u-netsurf.ne.jp>

    This program is free software.
    You can distribute/modify this program under the terms of
    the GNU Lesser General Public License version 2 or later.

*/

#include <stdio.h>
#ifdef __STDC__
# include <stdlib.h>
#endif

#include "ruby.h"
#include "re.h"


#include "sftextbuf.h"

VALUE TextBuffer;
VALUE TextBufferMark;


/* buffer type dependent definitions */

#define IS_TEXT_BUFFER

#include "sftextbuf.c"


/* ruby interface */

#define BUFFER_CLASS TextBuffer
#define MARK_CLASS TextBufferMark

#define GET_BUF(v,var) Data_Get_Struct(v, struct sf_textbuf, var)
#define GET_MARK(v, var) Data_Get_Struct(v, struct sf_textbufmark, var)
#define GET_MARK_CHK(v, var) GET_MARK(v, var); alive_check(var)

#define WRAP_BUF(p, c) Data_Wrap_Struct(c, buffer_mark, buffer_free, p)
#define WRAP_MARK(p, c) Data_Wrap_Struct(c, bufmark_mark, bufmark_free, p)

#include "rbbufcommon.c"


/*
========================================================================
                                init
========================================================================
*/

void Init_string_buffer();
void Init_text_buffer();

void
Init_textbuf()
{
    Init_string_buffer();
    Init_text_buffer();
}

void
Init_text_buffer()
{
    TextBuffer = rb_define_class("TextBuffer", rb_cObject);
    rb_define_singleton_method(TextBuffer, "new", buffer_s_new,    0);
    rb_define_method(TextBuffer, "new_mark",     buffer_new_mark,     -1);
    rb_define_method(TextBuffer, "head",         buffer_head,          0);
    rb_define_method(TextBuffer, "tail",         buffer_tail,          0);
    rb_define_method(TextBuffer, "size",         buffer_clen,          0);
    rb_define_method(TextBuffer, "length",       buffer_clen,          0);
    rb_define_method(TextBuffer, "byte_size",    buffer_len,           0);
    rb_define_method(TextBuffer, "byte_length",  buffer_len,           0);
    rb_define_method(TextBuffer, "empty?",       buffer_empty_p,       0);
    rb_define_method(TextBuffer, "substr",       buffer_substr,        2);
    rb_define_method(TextBuffer, "[]",           buffer_substr,        2);
    rb_define_method(TextBuffer, "each",         buffer_each_line,     0);
    rb_define_method(TextBuffer, "each_line",    buffer_each_line,     0);
    rb_define_method(TextBuffer, "reverse_each", buffer_rev_each_line, 0);
    rb_define_method(TextBuffer, "string",       buffer_string,        0);
    rb_define_method(TextBuffer, "to_s",         buffer_string,        0);
    rb_define_method(TextBuffer, "concat",       buffer_concat,        1);
    rb_define_method(TextBuffer, "clear",        buffer_clear,         0);
    rb_define_method(TextBuffer, "<<",           buffer_concat,        1);
    rb_define_method(TextBuffer, "index",        buffer_index,        -1);
    rb_define_method(TextBuffer, "rindex",       buffer_rindex,       -1);
    rb_define_method(TextBuffer, "match_to",     buffer_match_to,     -1);
    rb_define_method(TextBuffer, "=~",           buffer_match_to,     -1);
    rb_define_method(TextBuffer, "inspect",      buffer_inspect,       0);

    TextBufferMark = rb_define_class("TextBufferMark", rb_cData);
    rb_define_method(TextBufferMark, "buffer",       bufmark_buffer,      0);
    rb_define_method(TextBufferMark, "dup",          bufmark_dup,        -1);
    rb_define_method(TextBufferMark, "clone",        bufmark_clone,       0);
    rb_define_method(TextBufferMark, "alive?",       bufmark_alive_p,     0);
    rb_define_method(TextBufferMark, "discard",      bufmark_discard,     0);
    rb_define_method(TextBufferMark, "insert",       bufmark_insert,      1);
/* rb_define_method(TextBufferMark, "copy_of",      bufmark_copyof,      2); */
    rb_define_method(TextBufferMark, "delete",       bufmark_delete,      1);
    rb_define_method(TextBufferMark, "[]",           bufmark_substr,      1);
    rb_define_method(TextBufferMark, "substr",       bufmark_substr,      1);
    rb_define_method(TextBufferMark, "substr_ro",    bufmark_rosubstr,    1);
    rb_define_method(TextBufferMark, "getline",      bufmark_getline,     0);
    rb_define_method(TextBufferMark, "each_line_ro", bufmark_roeach,      0);
    rb_define_method(TextBufferMark, "index",        bufmark_char_index,  0);
    rb_define_method(TextBufferMark, "to_i",         bufmark_char_index,  0);
    rb_define_method(TextBufferMark, "byte_index",   bufmark_byte_index,  0);
    rb_define_method(TextBufferMark, "index=",       bufmark_setidx,      1);
    rb_define_method(TextBufferMark, "move_to",      bufmark_setidx,      1);
    rb_define_method(TextBufferMark, "move",         bufmark_fwd,         1);
    rb_define_method(TextBufferMark, "forward",      bufmark_fwd,         1);
    rb_define_method(TextBufferMark, "back",         bufmark_back,        1);
    rb_define_method(TextBufferMark, "skip",         bufmark_skip,       -1);
    rb_define_method(TextBufferMark, "head",         bufmark_go_beg,      0);
    rb_define_method(TextBufferMark, "tail",         bufmark_go_end,      0);
    rb_define_method(TextBufferMark, "+",            bufmark_plus,        1);
    rb_define_method(TextBufferMark, "-",            bufmark_minus,       1);

    rb_define_method(TextBufferMark, "line",         bufmark_line,        0);
    rb_define_method(TextBufferMark, "y",            bufmark_line,        0);
    rb_define_method(TextBufferMark, "line=",        bufmark_setline,     1);
    rb_define_method(TextBufferMark, "y=",           bufmark_setline,     1);
    rb_define_method(TextBufferMark, "column",       bufmark_col,         0);
    rb_define_method(TextBufferMark, "x",            bufmark_col,         0);
    rb_define_method(TextBufferMark, "column=",      bufmark_setcol,      1);
    rb_define_method(TextBufferMark, "x=",           bufmark_setcol,      1);
    rb_define_method(TextBufferMark, "byte_column",  bufmark_bytecol,     0);
    rb_define_method(TextBufferMark, "next_line",    bufmark_next_line,   0);
    rb_define_method(TextBufferMark, "prev_line",    bufmark_prev_line,   0);
    rb_define_method(TextBufferMark, "line_head",    bufmark_line_head,   0);
    rb_define_method(TextBufferMark, "line_tail",    bufmark_line_tail,   0);

    rb_define_method(TextBufferMark, "inspect",      bufmark_inspect,     0);


    rb_define_singleton_method(TextBuffer, "debug_on",  debugon,  0);
    rb_define_singleton_method(TextBuffer, "debug_off", debugoff, 0);
}
