sim

the sim text editor
git clone git://ssnf.xyz/sim
Log | Files | Refs | README

commit ed4cdcd4fac64fcf4582838a2885b723fa1662d6
parent ca2f499cdc740ad9d13cdf7e7d36622a4e759252
Author: ssnf <ssnf@ssnf.xyz>
Date:   Sun, 15 Aug 2021 19:24:36 +0000

added the ability to open and close files

Diffstat:
Mconfig.def.h | 6++++--
Msim.c | 66+++++++++++++++++++++++++++++++++++++++++++++++-------------------
Msim.h | 1+
3 files changed, 52 insertions(+), 21 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -1,5 +1,4 @@ -Key keys[] = { - { 'q', quit }, +Key normal[] = { { 'h', move, Left }, { 'j', move, Down }, { 'k', move, Up }, @@ -10,4 +9,7 @@ Key keys[] = { { 'g', move, Bottom }, { '0', move, StartLine }, { '$', move, EndLine }, + { 'o', file_open }, + { 'q', file_close }, + { 'Q', quit } }; diff --git a/sim.c b/sim.c @@ -23,7 +23,9 @@ enum { Bottom, StartLine, EndLine, - Ctrl = -0x60 + Ctrl = -0x60, + Esc = 0x1b, + Del = 0x7f }; typedef struct { @@ -61,9 +63,10 @@ void* erealloc(void*, ulong); static void blind_reader(Frame* fr, Posn p0); static void blind_writer(ushort line, ushort offset, ushort top, ushort bot); static void init(); -static void file_close(File* f); +static void file_close(int arg); static void file_init(File* f); -static void file_load(File* f); +static void file_load(char* name); +static void file_open(int arg); static void file_save(File* f); static void fr_add(Frame* fr, Posn p0, Posn p1); static void fr_close(Frame* fr); @@ -197,14 +200,11 @@ init() } static void -file_close(File* f) +file_close(int arg) { str_zero(f->s); str_zero(f->name); - str_close(f->s); - str_close(f->name); - free(f->s); - free(f->name); + f->dot.p0 = f->dot.p1 = 0; } static void @@ -218,11 +218,13 @@ file_init(File* f) } static void -file_load(File* f) +file_load(char* name) { FILE* disk; - if (!(disk = fopen(f->name->s, "r"))) + str_zero(f->name); + str_adds(f->name, name); + if (!(disk = fopen(name, "r"))) return; fseek(disk, 0, SEEK_END); f->s->n = ftell(disk); @@ -233,6 +235,33 @@ file_load(File* f) } static void +file_open(int c) +{ + String s; + + str_init(&s); + str_adds(&s, f->name->s); + printf(CSI "%uH" EL CSI "32m$" CSI "0m %s", w.wy/2, s.s); + for (;c = fgetc(stdin), c != '\n';) { + switch (c) { + case Esc: + str_close(&s); + return; + case '\t': + continue; + case Del: + str_delc(&s); + break; + default: + str_addc(&s, c); + } + printf(CSI "%uH" EL CSI "32m$" CSI "0m %s", w.wy/2, s.s); + } + file_load(s.s); + str_close(&s); +} + +static void file_save(File* f) { FILE* disk; @@ -279,7 +308,7 @@ static void fr_insure(Frame* fr, ushort n) { if (n > fr->size) { - fr->size += n + (32 - (n % 32)); + fr->size += n + 32; fr->a = erealloc(fr->a, fr->size * sizeof(*fr->a)); } } @@ -435,6 +464,7 @@ str_zero(String* p) p->size = MAXEMPTY; } p->n = 0; + memset(p->s, 0, p->size); } static void @@ -474,7 +504,7 @@ static void str_delc(String* p) { if (p->n) - p->s[p->n--] = 0; + p->s[--p->n] = 0; } static void @@ -500,17 +530,15 @@ main(int argc, char* argv[]) uchar c; init(); - if (argv[1]) { - str_adds(f->name, argv[1]); - file_load(f); - } + if (argv[1]) + file_load(argv[1]); for (;;) { win_query(&w); fr_update(); c = fgetc(stdin); - for (i = 0; i < LENGTH(keys); ++i) { - if (keys[i].key == c) { - keys[i].func(keys[i].value); + for (i = 0; i < LENGTH(normal); ++i) { + if (normal[i].key == c) { + normal[i].func(normal[i].value); break; } } diff --git a/sim.h b/sim.h @@ -1,4 +1,5 @@ #define ED "\x1b[2J" +#define EL "\x1b[2K" #define CSI "\x1b[" typedef unsigned char uchar;