sim

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

commit d3fcbd4ed5bc7b59ae782e213ed184fcb3d8d90f
parent a6b5939f5c5806a8211137e8f5ba21cbd8784014
Author: ssnf <ssnf@ssnf.xyz>
Date:   Wed,  5 Feb 2025 00:17:40 +0000

reimplement Buffer as a linked list in File

Diffstat:
Mconfig.def.h | 2+-
Msim.c | 135++++++++++++++++++++++++++++++++++++++++++-------------------------------------
2 files changed, 73 insertions(+), 64 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -3,7 +3,7 @@ , curpos() \ , f->s.s[f->dot.p1] \ , f->name.n ? f->name.s : "-unnamed-" \ - , (f->di == f->i) ? "" : "*" \ + , (f->bd == f->b) ? "" : "*" \ , f - file \ , counter diff --git a/sim.c b/sim.c @@ -49,22 +49,25 @@ typedef struct { ulong size; } String; -typedef struct { - String is[2048]; - String ds[2048]; - uint c[2048]; - uint arg[2048]; - uint count[2048]; - Posn p0[2048]; - short n; -} Buffer; +typedef struct Buffer Buffer; + +struct Buffer { + String is; + String ds; + uint c; + uint arg; + uint count; + Posn p0; + Buffer* next; + Buffer* prev; +}; typedef struct { String s; String name; Address dot; - short i; /*buffer index*/ - short di; /*disk buffer index*/ + Buffer* b; /*buffer ll*/ + Buffer* bd; /*disk buffer pointer*/ } File; typedef struct { @@ -88,6 +91,7 @@ static void blind_writer(ushort line, ushort offset, ushort top, ushort bot); static void buf_add( String* is, String* ds, uint c, uint arg, uint count ); +static void buf_free(Buffer* p); static void change(int arg); static uint charsiz(char c, ulong wx); static void count(int arg); @@ -137,7 +141,6 @@ static void yank(int arg); static Frame* fr, frame[FILECOUNT]; static File* f, file[FILECOUNT]; -static Buffer* buf, buffer[FILECOUNT]; static String istr, srch; static Window w; static uint counter; @@ -236,21 +239,33 @@ blind_writer(ushort line, ushort offset, ushort top, ushort bot) static void buf_add(String* is, String* ds, uint c, uint arg, uint count) { - buf->n = ++f->i + 1; - str_zero(&buf->is[f->i]); - str_zero(&buf->ds[f->i]); + if (f->b->next) + buf_free(f->b->next); + f->b->next = emalloc(sizeof(*f->b->next)); + f->b->next->prev = f->b; + f->b = f->b->next; if (is != NULL) - str_insert(&buf->is[f->i], is, 0); + str_insert(&f->b->is, is, 0); if (ds != NULL) - str_insert(&buf->ds[f->i], ds, 0); - buf->c[f->i] = c; - buf->arg[f->i] = arg; - buf->count[f->i] = count; - buf->p0[f->i] = f->dot.p0; + str_insert(&f->b->ds, ds, 0); + f->b->c = c; + f->b->arg = arg; + f->b->count = count; + f->b->p0 = f->dot.p0; fr_zero(fr); } static void +buf_free(Buffer* p) +{ + if (p->next != NULL) + buf_free(p->next); + str_close(&p->is); + str_close(&p->ds); + free(p); +} + +static void change(int arg) { String s; @@ -279,11 +294,11 @@ change(int arg) fr_zero(fr); insert(0); if (s.n) - str_insert(&buf->ds[f->i], &s, 0); + str_insert(&f->b->ds, &s, 0); + f->b->c = Change; + f->b->arg = arg; + f->b->count = count; str_close(&s); - buf->c[f->i] = Change; - buf->arg[f->i] = arg; - buf->count[f->i] = count; fr_update(); } @@ -364,25 +379,25 @@ dot(int arg) { String ds; - if (f->i < 0) + if (f->b->prev == NULL) return; - arg = buf->arg[f->i]; - counter = buf->count[f->i]; - switch (buf->c[f->i]) { + arg = f->b->arg; + counter = f->b->count; + switch (f->b->c) { case Insert: if (arg == Down) move(EndLine); else move(arg); - str_insert(&f->s, &buf->is[f->i], f->dot.p0); - buf_add(&buf->is[f->i], NULL, Insert, arg, counter); + str_insert(&f->s, &f->b->is, f->dot.p0); + buf_add(&f->b->is, NULL, Insert, arg, counter); break; case Delete: delete(arg); break; case Change: str_init(&ds); - if (buf->ds[f->i].n) { + if (f->b->ds.n) { selection(arg); if (arg == Word || arg == Letter || arg > 0x7f) ++f->dot.p1; @@ -391,11 +406,9 @@ dot(int arg) ); str_delete(&f->s, f->dot.p0, f->dot.p1); } - if (buf->is[f->i].n) - str_insert(&f->s, &buf->is[f->i], f->dot.p0); - buf_add( - &buf->is[f->i], &ds, Change, arg, buf->count[f->i] - ); + if (f->b->is.n) + str_insert(&f->s, &f->b->is, f->dot.p0); + buf_add(&f->b->is, &ds, Change, arg, f->b->count); str_close(&ds); f->dot.p1 = f->dot.p0; break; @@ -406,20 +419,19 @@ dot(int arg) static void redo(int arg) { - if (f->i == buf->n - 1) + if (f->b->next == NULL) return; if (arg) { - for (;f->i != buf->n - 1;) + for (;f->b->next != NULL;) redo(0); return; } - if (buf->ds[++f->i].n) - str_delete( - &f->s, buf->p0[f->i], buf->p0[f->i] + buf->ds[f->i].n - ); - if (buf->is[f->i].n) - str_insert(&f->s, &buf->is[f->i], buf->p0[f->i]); - f->dot.p0 = f->dot.p1 = buf->p0[f->i]; + f->b = f->b->next; + if (f->b->ds.n) + str_delete(&f->s, f->b->p0, f->b->p0 + f->b->ds.n); + if (f->b->is.n) + str_insert(&f->s, &f->b->is, f->b->p0); + f->dot.p0 = f->dot.p1 = f->b->p0; fr_zero(fr); fr_update(); } @@ -433,7 +445,6 @@ escape(int c) --c; f = &file[c]; fr = &frame[c]; - buf = &buffer[c]; return; } ungetc(c + 0x30, stdin); @@ -543,7 +554,6 @@ init(void) } f = &file[0]; fr = &frame[0]; - buf = &buffer[0]; str_init(&srch); str_init(&istr); } @@ -553,7 +563,7 @@ file_close(int arg) { if (arg != -1) f = &file[arg]; - if (f->di != f->i) { + if (f->bd != f->b) { msg(w.wy / 2, RED("Save %s?") " [y/n]" , f->name.n ? f->name.s : "-unnamed-" ); @@ -562,8 +572,9 @@ file_close(int arg) } str_close(&f->s); str_close(&f->name); + for (;f->b->prev != NULL; f->b = f->b->prev); + buf_free(f->b); file_init(f); - buffer[f - file].n = 0; fr_zero(fr); } @@ -574,8 +585,8 @@ file_init(File* f) str_init(&f->name); f->dot.p0 = 0; f->dot.p1 = 0; - f->i = -1; - f->di = -1; + f->b = emalloc(sizeof(*f->b)); + f->bd = f->b; } static void @@ -593,7 +604,6 @@ file_load(File* f) } fclose(disk); f->dot.p0 = f->dot.p1 = 0; - f->i = -1; fr_zero(fr); } @@ -620,7 +630,7 @@ file_save(int arg) disk = fopen(file[arg].name.s, "w"); fwrite(file[arg].s.s, file[arg].s.n, 1, disk); fclose(disk); - file[arg].di = file[arg].i; + file[arg].bd = file[arg].b; } static void @@ -1114,20 +1124,19 @@ str_delete(String* p, Posn p0, Posn p1) static void undo(int arg) { - if (f->i < 0) + if (f->b->prev == NULL) return; if (arg) { - for (;f->i != -1;) + for (;f->b->prev != NULL;) undo(0); return; } - if (buf->is[f->i].n) - str_delete( - &f->s, buf->p0[f->i], buf->p0[f->i] + buf->is[f->i].n - ); - if (buf->ds[f->i].n) - str_insert(&f->s, &buf->ds[f->i], buf->p0[f->i]); - f->dot.p0 = f->dot.p1 = buf->p0[f->i--]; + if (f->b->is.n) + str_delete(&f->s, f->b->p0, f->b->p0 + f->b->is.n); + if (f->b->ds.n) + str_insert(&f->s, &f->b->ds, f->b->p0); + f->dot.p0 = f->dot.p1 = f->b->p0; + f->b = f->b->prev; fr_zero(fr); fr_update(); }