/* vi:set sw=4:

    textbuf.h

    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.

*/

#ifndef TEXTBUF_H_DEFINED
#define TEXTBUF_H_DEFINED


/* flags */

#define TEXT_BUFFER   (1UL << 0)
#define STRING_BUFFER (1UL << 1)
#define WCHAR_BUFFER  (1UL << 2)

#define ALIVE_FLAG    (1UL << 8)  /* mark has m->buf */
#define ALIVE_P(m)    (m->flags & ALIVE_FLAG)

#define INSERT_BEFORE (1UL << 9)  /* insert text at "left" of mark */
#define INSERT_AFTER  0UL         /* insert text at "right" of mark */

#define TEXTBUF_EXTERN_FLAG_BIT 9


/* ---------- text buffer --------- */


extern VALUE TextBuffer;
extern VALUE TextBufferMark;

struct sf_textbuf {
    unsigned long flags;

    char *ptr;                     /* string */
    long len;                      /* capacity of buffer (byte) */
    long gap;                      /* byte index of gap */
    long gaplen;                   /* byte length of gap */

    struct sf_textbufmark *marks;  /* d-linked-list of mark */

    struct sf_textbufmark *point;  /* edit point (internal use only) */
    struct sf_textbufmark *head;   /* head of string */
    struct sf_textbufmark *tail;   /* tail of string */
    VALUE vhead;
    VALUE vtail;
};

struct sf_textbufmark {
    unsigned long flags;
    struct sf_textbuf *buf;
    struct sf_textbufmark *prev;
    struct sf_textbufmark *next;
    long idx;
    long cidx;
    long line;
    struct re_registers *regi;
    VALUE vbuf;
};


/* buffer */
struct sf_textbuf* sf_tbuf_new  _((void));
void               sf_tbuf_free _((struct sf_textbuf *buf));
long               sf_tbuf_len  _((struct sf_textbuf *buf));
long               sf_tbuf_clen _((struct sf_textbuf *buf));

/* mark */
struct sf_textbufmark* sf_tmark_new _((struct sf_textbuf *buf,
                                       long idx, long cidx, long line,
                                       unsigned long flags));
struct sf_textbufmark* sf_tmark_char_new _((struct sf_textbuf *buf, long c,
                                            unsigned long flags));
void  sf_tmark_free    _((struct sf_textbufmark *mark));
void  sf_tmark_alive_p _((struct sf_textbufmark *mark));
void  sf_tmark_insert  _((struct sf_textbufmark *mark, char *str, long len));
void  sf_tmark_delete  _((struct sf_textbufmark *mark, long len));
VALUE sf_tmark_substr  _((struct sf_textbufmark *mark, long len));
void  sf_tmark_move    _((struct sf_textbufmark *mark, long len));
void  sf_tmark_goto    _((struct sf_textbufmark *mark, long c));
void  sf_tmark_moveto  _((struct sf_textbufmark *mark,
                          struct sf_textbufmark *m));
void  sf_tmark_nextline _((struct sf_textbufmark *mark));
void  sf_tmark_prevline _((struct sf_textbufmark *mark));
void  sf_tmark_linetail _((struct sf_textbufmark *mark));
void  sf_tmark_linehead _((struct sf_textbufmark *mark));
long  sf_tmark_col      _((struct sf_textbufmark *mark));
long  sf_tmark_bcol     _((struct sf_textbufmark *mark));
void  sf_tmark_setcol   _((struct sf_textbufmark *mark, long i));


/* ---------- string buffer --------- */

extern VALUE StringBuffer;
extern VALUE StringBufferMark;

struct sf_strbuf {
    unsigned long flags;

    char *ptr;
    long len;
    long gap;
    long gaplen;

    struct sf_strbufmark *marks;

    struct sf_strbufmark *point;
    struct sf_strbufmark *head;
    struct sf_strbufmark *tail;
    VALUE vhead;
    VALUE vtail;
};

struct sf_strbufmark {
    unsigned long flags;
    struct sf_strbuf *buf;
    struct sf_strbufmark *prev;
    struct sf_strbufmark *next;
    long idx;
    struct re_registers *regi;
    VALUE vbuf;
};


/* buffer */
struct sf_strbuf* sf_strbuf_new  _((void));
void              sf_strbuf_free _((struct sf_strbuf *buf));
long              sf_strbuf_len  _((struct sf_strbuf *buf));

/* mark */
struct sf_strbufmark* sf_smark_new _((struct sf_strbuf *buf, long i,
                                      unsigned long flag));
struct sf_strbufmark* sf_smark_char_new _((struct sf_strbuf *buf, long c,
                                           unsigned long flags));
void  sf_smark_free    _((struct sf_strbufmark *mark));
void  sf_smark_alive_p _((struct sf_strbufmark *mark));
void  sf_smark_insert  _((struct sf_strbufmark *mark, char *str, long len));
void  sf_smark_delete  _((struct sf_strbufmark *mark, long len));
VALUE sf_smark_substr  _((struct sf_strbufmark *mark, long len));
void  sf_smark_move    _((struct sf_strbufmark *mark, long len));
void  sf_smark_goto    _((struct sf_strbufmark *mark, long idx));
void  sf_smark_moveto  _((struct sf_strbufmark *mark,
                          struct sf_strbufmark *to));
void  sf_smark_nextline _((struct sf_strbufmark *mark));
void  sf_smark_prevline _((struct sf_strbufmark *mark));
void  sf_smark_linetail _((struct sf_strbufmark *mark));
void  sf_smark_linehead _((struct sf_strbufmark *mark));
long  sf_smark_col      _((struct sf_strbufmark *mark));
long  sf_smark_bcol     _((struct sf_strbufmark *mark));
void  sf_smark_setcol   _((struct sf_strbufmark *mark, long i));


#if 0
/* ---------- wchar buffer --------- */

extern VALUE WCharBuffer;
extern VALUE WCharBufferMark;

struct sf_wcharbuf {
    unsigned long flags;

    char *ptr;
    long len;
    long gap;
    long gaplen;

    struct sf_wcharbufmark *marks;

    struct sf_wcharbufmark *point;
    struct sf_wcharbufmark *head;
    struct sf_wcharbufmark *tail;
    VALUE vhead;
    VALUE vtail;
};

struct sf_wcharbufmark {
    unsigned long flags;
    struct sf_wcharbuf *buf;
    struct sf_wcharbufmark *prev;
    struct sf_wcharbufmark *next;
    long idx;
    VALUE vbuf;
};


/* buffer */
struct sf_wcharbuf*     sf_wcharbuf_new      _((void));
void                    sf_wcharbuf_free     _((struct sf_wcharbuf *buf));
long                    sf_wcharbuf_len      _((struct sf_wcharbuf *buf));
struct sf_wcharbufmark* sf_wcharbuf_new_mark _((struct sf_wcharbuf *buf,
                                                long idx));

/* mark */
void  sf_wmark_free    _((struct sf_wcharbufmark *mark));
void  sf_wmark_move    _((struct sf_wcharbufmark *mark, long offset));
void  sf_wmark_insert  _((struct sf_wcharbufmark *mark, char *str, long len));
void  sf_wmark_delete  _((struct sf_wcharbufmark *mark, long len));

void  sf_wmark_alive_p _((struct sf_wcharbufmark *mark));
void  sf_wmark_insert  _((struct sf_wcharbufmark *mark, char *str, long len));
void  sf_wmark_delete  _((struct sf_wcharbufmark *mark, long len));
VALUE sf_wmark_substr  _((struct sf_wcharbufmark *mark, long len));
#endif   /* 0 */


#endif
