diff options
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 63 |
1 files changed, 63 insertions, 0 deletions
@@ -22,12 +22,75 @@ #include "utf8.h" #include "str.h" +#define ED_BUF_MAX 1024 + +typedef enum { + ED_BUF_SCRATCH, + ED_BUF_FILE +} EditBufType; + +typedef struct { + EditBufType type; + Arena arena; + Str path; + Txt txt; + TxtLoc cur; +} EditBuf; + +typedef enum { + MODE_NORMAL, + MODE_INSERT +} EditMode; + +typedef struct { + Arena scratch; + EditBuf buf[ED_BUF_MAX]; + u32 bufn, bufi; + EditMode mode; + u32 count; +} Editor; + Arena scratch = { 0 }; Txt txt = { 0 }; TxtLoc cur = { 0 }; int mode = 0; u32 count = 0; +int ed_buf_new(Editor *e, const char *path) { + if (e->bufn == ED_BUF_MAX) return -1; + u32 i = e->bufn; + EditBuf b = { 0 }; + b.arena = arena_init(1L << 30); + if (path) { + b.path = str_dup(str_from_cstr(path), &b.arena); + b.type = ED_BUF_FILE; + txt_load(&b.txt, path); + } else { + b.path = S("*scratch*"); + b.type = ED_BUF_SCRATCH; + txt_load_empty(&b.txt); + } + b.cur = txt_end(&b.txt); + return i; +} + +void ed_buf_free(EditBuf *b) { + txt_free(&b->txt); + arena_free(&b->arena); +} + +void ed_init(Editor *e) { + memset(e, 0, sizeof(Editor)); + e->scratch = arena_init(1L << 30), + e->bufi = ed_buf_new(e, NULL); +} + +void ed_fini(Editor *e) { + for (u32 i = 0; i < e->bufn; i++) { + ed_buf_free(&e->buf[i]); + } +} + static inline int is_space(u32 c) { return c <= 0x20 && c != 0; } |
