sim

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

commit 8a7cc7b412399d329790f80d496921b933bb4edc
parent 5748a3213cae3a913003c120840346f01f2441bc
Author: ssnf <ssnf@ssnf.xyz>
Date:   Thu, 12 Aug 2021 15:02:37 +0000

added multi-file support, improved str_adds()

Diffstat:
Msim.c | 38+++++++++++++++++++++++---------------
Msim.h | 3++-
Mstring.c | 5++++-
3 files changed, 29 insertions(+), 17 deletions(-)

diff --git a/sim.c b/sim.c @@ -11,20 +11,20 @@ void* erealloc(void*, ulong); void move(int); void quit(int); -File f; +File file[FILECOUNT]; +File* f; Window w; -uchar refresh; +uchar refresh; #include "config.h" void die(char* fmt, ...) { + uint i; va_list ap; win_end(); - str_adds(f.name, ".swap", 5); - file_save(&f); va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); @@ -32,7 +32,13 @@ die(char* fmt, ...) perror(NULL); else fputc('\n', stderr); - fprintf(stderr, "sim crashed. file saved to sim-crash.swap\n"); + for (i = 0; i < FILECOUNT; ++i) { + if (!file[i].s->n) + continue; + str_adds(file[i].name, ".swap"); + file_save(&file[i]); + fprintf(stderr, "file saved to %s\n", file[i].name->s); + } *fmt = *(char*)0; } @@ -62,18 +68,18 @@ move(int arg) { switch (arg) { case Left: - if (f.dot.p0) - f.dot.p1 = --f.dot.p0; + if (f->dot.p0) + f->dot.p1 = --f->dot.p0; break; case Right: - if (f.dot.p0 < f.s->n) - f.dot.p1 = ++f.dot.p0; + if (f->dot.p0 < f->s->n) + f->dot.p1 = ++f->dot.p0; break; case Up: - for (;f.dot.p0 && f.s->s[f.dot.p0 - 1] != '\n'; f.dot.p1 = --f.dot.p0); + for (;f->dot.p0 && f->s->s[f->dot.p0 - 1] != '\n'; f->dot.p1 = --f->dot.p0); break; case Down: - for (;f.dot.p0 < f.s->n && f.s->s[f.dot.p0] != '\n'; f.dot.p1 = ++f.dot.p0); + for (;f->dot.p0 < f->s->n && f->s->s[f->dot.p0] != '\n'; f->dot.p1 = ++f->dot.p0); break; }; } @@ -88,14 +94,16 @@ quit(int arg) int main(int argc, char* argv[]) { - int i; + uint i; uchar c; win_init(); - file_init(&f); + for (i = 0; i < FILECOUNT; ++i) + file_init(&file[i]); + f = &file[0]; if (argv[1]) { - str_adds(f.name, argv[1], strlen(argv[1])); - file_load(&f); + str_adds(f->name, argv[1]); + file_load(f); } for (;;) { win_query(&w); diff --git a/sim.h b/sim.h @@ -1,6 +1,7 @@ #define ED "\x1b[2J" #define CSI "\x1b[" #define LENGTH(a) (sizeof(a)/sizeof(a[0])) +#define FILECOUNT 8 typedef long Posn; typedef unsigned char uchar; @@ -54,7 +55,7 @@ void file_close(File* f); void file_init(File* f); void file_load(File* f); void file_save(File* f); -void str_adds(String* p, char* s, ulong n); +void str_adds(String* p, char* s); void str_close(String* s); void str_init(String* s); void str_insure(String* s, ulong n); diff --git a/string.c b/string.c @@ -53,8 +53,11 @@ str_addc(String* p, int c) } void -str_adds(String* p, char* s, ulong n) +str_adds(String* p, char* s) { + ulong n; + + n = strlen(s); str_insure(p, p->n + n); memmove(p->s + p->n, s, n); p->n += n;