#ifndef TXT_H #define TXT_H #include "dynarr.h" typedef enum : u8 { TXT_SRC, TXT_ADD } TxtBufIdx; typedef struct TxtPiece { TxtBufIdx buf; u32 ofs, n; } TxtPiece; typedef struct { char *s; u32 n, c; } TxtBuf; typedef struct { DYNARR(TxtPiece) ptbl; TxtBuf buf[2]; u32 len; } Txt; typedef struct { Txt *t; u32 p, i; } TxtLoc; /* text buffer manipulation */ u32 txt_split_piece(Txt *b, u32 pi, u32 i); void txt_remove_piece(Txt *b, u32 pi); void txt_insert_piece(Txt *b, u32 pi, TxtBufIdx buf, u32 ofs, u32 n); int txt_load(Txt *b, const char *path); int txt_save(Txt *b, const char *path); void txt_free(Txt *b); /* insertion & deletion */ TxtLoc txt_insert(TxtLoc l, const char *s, u32 n); TxtLoc txt_delete(TxtLoc l, u32 n); TxtLoc txt_delete_range(TxtLoc lo, TxtLoc hi); TxtLoc txt_insert_c(TxtLoc l, u32 ch); TxtLoc txt_delete_c(TxtLoc l); /* navigation */ TxtLoc txt_at(Txt *b, u32 ofs); u32 txt_ofs(TxtLoc l); int txt_before(TxtLoc a, TxtLoc b); int txt_after(TxtLoc a, TxtLoc b); TxtLoc bnext(TxtLoc l); TxtLoc bprev(TxtLoc l); TxtLoc cnext(TxtLoc l); TxtLoc cprev(TxtLoc l); int at_start(TxtLoc l); int at_end(TxtLoc l); TxtLoc next_newline(TxtLoc l); TxtLoc prev_newline(TxtLoc l); TxtLoc start_of_line(TxtLoc l); TxtLoc end_of_line(TxtLoc l); TxtLoc prev_line(TxtLoc l); TxtLoc next_line(TxtLoc l); TxtLoc at_col(TxtLoc l, u32 col); u32 get_col(TxtLoc l); /* reading */ u32 txt_chr(TxtLoc l); u8 txt_byte(TxtLoc l); u32 txt_chr_next(TxtLoc *l); #endif