sim

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

commit 3c2e3773f8e8c263809f44eaf05d892627b506f2
parent 2615c4e01b82bbffabcfa444f618ed3da42ca17e
Author: ssnf <ssnf@ssnf.xyz>
Date:   Fri,  6 Dec 2024 01:29:34 +0000

improve fr_calc()

Diffstat:
Msim.c | 172++++++++++++++++++++++++++++++++++++++++----------------------------------------
1 file changed, 86 insertions(+), 86 deletions(-)

diff --git a/sim.c b/sim.c @@ -73,7 +73,7 @@ typedef struct { typedef struct { Address* a; - ushort dot; + ushort cur; ushort n; ushort size; } Frame; @@ -81,11 +81,8 @@ typedef struct { void die(char*, ...); void* emalloc(ulong); void* erealloc(void*, ulong); -static void blind_reader(Frame* fr, String* s, Posn p0); -static void blind_writer( - String* s, Frame* fr, - ushort line, ushort offset, ushort top, ushort bot -); +static void blind_reader(Frame* fr, Posn p0); +static void blind_writer(ushort line, ushort offset, ushort top, ushort bot); static void buf_add( Buffer* b, String* is, String* ds, uint c, uint arg, uint count, Posn p0 @@ -105,12 +102,13 @@ static void file_init(File* f); static void file_load(File* f); static void file_open(int c); static void file_save(int arg); -static void fr_add(Frame* fr, Posn p0, Posn p1); +static void fr_add(Frame* fr, Address a); +static void fr_calc(void); static void fr_close(Frame* fr); static void fr_init(Frame* fr); -static void fr_insert(Frame* p, Frame* q, ushort n); +static void fr_insert(Frame* p, Frame q, ushort n); static void fr_insure(Frame* fr, ushort n); -static void fr_update(); +static void fr_update(void); static void fr_zero(Frame*); static void gmove(int arg); static void paste(int arg); @@ -187,15 +185,15 @@ erealloc(void* p, ulong n) } static void -blind_reader(Frame* fr, String* s, Posn p0) +blind_reader(Frame* fr, Posn p0) { - Posn p1; + Address a; uint wx; - p1 = p0; + a.p0 = a.p1 = p0; do { - for (wx = 0; wx < w.wx && p1 < s->n;) { - switch (s->s[p1]) { + for (wx = 0; wx < w.wx && a.p1 < f->s->n;) { + switch (f->s->s[a.p1]) { case '\t': wx += w.t - (wx % w.t); break; @@ -205,25 +203,23 @@ blind_reader(Frame* fr, String* s, Posn p0) ++wx; } if (wx < w.wx) - ++p1; + ++a.p1; if (wx >= w.wx) { - if (s->s[p1] == '\t') - ++p1; - if (s->s[p1 + 1] == '\n') - ++p1; + if (f->s->s[a.p1] == '\t') + ++a.p1; + if (f->s->s[a.p1 + 1] == '\n') + ++a.p1; } } endloop: - fr_add(fr, p0, p1); - p0 = ++p1; - } while (p1 <= s->n && s->s[p1 - 1] != '\n'); + fr_add(fr, a); + a.p0 = ++a.p1; + } while (a.p1 <= f->s->n && f->s->s[a.p1 - 1] != '\n'); } static void -blind_writer( - String* s, Frame* fr, - ushort line, ushort offset, ushort top, ushort bot -) { +blind_writer(ushort line, ushort offset, ushort top, ushort bot) +{ ushort i, o; i = o = 0; @@ -234,7 +230,9 @@ blind_writer( else if (fr->n) o = fr->n - 1; printf(CSI "%uH", offset > line ? 0 : line - offset); - fwrite(&s->s[fr->a[i].p0], fr->a[o].p1 - fr->a[i].p0, 1, stdout); + fwrite(&f->s->s[fr->a[i].p0], fr->a[o].p1 - fr->a[i].p0 + , 1, stdout + ); } static void @@ -609,56 +607,56 @@ file_save(int arg) } static void -fr_add(Frame* fr, Posn p0, Posn p1) +fr_add(Frame* fr, Address a) { - fr_insure(fr, fr->n + 1); - fr->a[fr->n].p0 = p0; - fr->a[fr->n].p1 = p1; - ++fr->n; + fr_insure(fr, ++fr->n); + fr->a[fr->n - 1] = a; } static void -fr_calc(Frame* fr) +fr_calc(void) { Frame fr0; - Posn p0, p1; - ushort half; + Posn p0; - half = w.wy >> 1; + for (;f->dot.p1 < fr->a[fr->cur].p0 && fr->cur; --fr->cur); + for (;f->dot.p1 > fr->a[fr->cur].p1 && fr->cur + 1 < fr->n + ; ++fr->cur + ); if (!fr->n || f->dot.p1 != f->dot.p0 || f->dot.p1 < fr->a[0].p0 - || f->dot.p1 > fr->a[fr->n ? fr->n - 1 : 0].p1 - || (fr->dot < half && fr->a[0].p0) - || (fr->dot + half + 1 > fr->n - && fr->a[fr->n ? fr->n - 1 : 0].p1 + 1 < f->s->n + || f->dot.p1 > fr->a[fr->n - 1].p1 + || (fr->cur < w.wy && fr->a[0].p0) + || (fr->cur + w.wy > fr->n + && fr->a[fr->n - 1].p1 + 1 < f->s->n ) ) { + /*dot + bottom addresses*/ fr_zero(fr); - for (p0 = f->dot.p1; p0; --p0) - if (f->s->s[p0 - 1] == '\n') - break; - p1 = p0; - if (p1) { - fr_init(&fr0); - for (--p1; p1 && fr->n < w.wy;) { - for (;p1 && f->s->s[p1 - 1] != '\n'; --p1); - blind_reader(&fr0, f->s, p1); - p1 = fr0.a[0].p0; - if (p1) - --p1; - fr_insert(fr, &fr0, 0); - fr_zero(&fr0); - } - fr_close(&fr0); + for (p0 = f->dot.p1; p0 && f->s->s[p0 - 1] != '\n'; --p0); + for (;p0 < f->s->n && fr->n < w.wy * 2;) { + blind_reader(fr, p0); + p0 = fr->a[fr->n - 1].p1 + 1; } - for (p1 = p0; p1 < f->s->n && fr->n < w.wy * 3;) { - blind_reader(fr, f->s, p1); - p1 = fr->a[fr->n - 1].p1 + 1; + /*top addresses*/ + for (fr_init(&fr0) + ; fr->a[0].p0 && fr->cur < w.wy + ; fr->cur += fr0.n + ) { + for (p0 = fr->a[0].p0 - 1 + ; p0 && f->s->s[p0 - 1] != '\n' + ; --p0 + ); + blind_reader(&fr0, p0); + fr_insert(fr, fr0, 0); + fr_zero(&fr0); } + fr_close(&fr0); + for (; f->dot.p1 > fr->a[fr->cur].p1 && fr->cur < fr->n + ; ++fr->cur + ); } - for (;f->dot.p1 < fr->a[fr->dot].p0 && fr->dot; --fr->dot); - for (;f->dot.p1 > fr->a[fr->dot].p1; ++fr->dot); } static void @@ -671,18 +669,18 @@ static void fr_init(Frame* fr) { fr->a = emalloc(32 * sizeof(*fr->a)); - fr->dot = 0; + fr->cur = 0; fr->n = 0; fr->size = 32; } static void -fr_insert(Frame* p, Frame* q, ushort n) +fr_insert(Frame* p, Frame q, ushort n) { - fr_insure(p, p->n + q->n); - memmove(p->a + n + q->n, p->a + n, (p->n - n) * sizeof(*p->a)); - memmove(p->a + n, q->a, q->n * sizeof(*p->a)); - p->n += q->n; + fr_insure(p, p->n + q.n); + memmove(p->a + n + q.n, p->a + n, (p->n - n) * sizeof(*p->a)); + memmove(p->a + n, q.a, q.n * sizeof(*p->a)); + p->n += q.n; } static void @@ -706,13 +704,13 @@ fr_update() printf(ED); goto status; } - fr_calc(fr); + fr_calc(); fwrite(ED, sizeof(ED), 1, stdout); - blind_writer(f->s, fr, half, fr->dot, half, half + (w.wy % 2)); + blind_writer(half, fr->cur, half, half + (w.wy % 2)); status: printf(CSI "%uH", w.wy); printf(STATUS); - for (p0 = fr->a[fr->dot].p0, p1 = 1; p0 < f->dot.p1; ++p0) { + for (p0 = fr->a[fr->cur].p0, p1 = 1; p0 < f->dot.p1; ++p0) { if (f->s->s[p0] == '\t' && p1 % w.t) p1 += (w.t + 1) - (p1 % w.t); else @@ -726,7 +724,7 @@ static void fr_zero(Frame* fr) { fr->n = 0; - fr->dot = 0; + fr->cur = 0; } static void @@ -742,35 +740,35 @@ move(int arg) ++f->dot.p1; break; case Up: - if (fr->dot) - f->dot.p1 = fr->a[fr->dot - 1].p0; + if (fr->cur) + f->dot.p1 = fr->a[fr->cur - 1].p0; else f->dot.p1 = 0; break; case Down: if (!f->s->n) return; - if (fr->dot < fr->n - 1) - f->dot.p1 = fr->a[fr->dot + 1].p0; + if (fr->cur < fr->n - 1) + f->dot.p1 = fr->a[fr->cur + 1].p0; else - f->dot.p1 = fr->a[fr->dot].p1 - 1; + f->dot.p1 = fr->a[fr->cur].p1 - 1; break; case HalfUp: - if (fr->dot < w.wy/2) + if (fr->cur < w.wy/2) f->dot.p1 = 0; else - f->dot.p1 = fr->a[fr->dot - w.wy/2].p0; + f->dot.p1 = fr->a[fr->cur - w.wy/2].p0; break; case HalfDown: if (!f->s->n) break; - if (fr->n - fr->dot <= w.wy/2) { + if (fr->n - fr->cur <= w.wy/2) { if (fr->a[fr->n - 1].p1 <= f->s->n) f->dot.p1 = fr->a[fr->n - 1].p0; else f->dot.p1 = f->s->n; } else - f->dot.p1 = fr->a[fr->dot + w.wy/2].p0; + f->dot.p1 = fr->a[fr->cur + w.wy/2].p0; break; case Top: f->dot.p1 = 0; @@ -780,12 +778,12 @@ move(int arg) f->dot.p1 = f->s->n - 1; break; case StartLine: - if (fr->dot) - for (;fr->dot > 1 - && f->s->s[fr->a[fr->dot].p0 - 1] != '\n' - ; --fr->dot + if (fr->cur) + for (;fr->cur > 1 + && f->s->s[fr->a[fr->cur].p0 - 1] != '\n' + ; --fr->cur ); - f->dot.p1 = fr->a[fr->dot].p0; + f->dot.p1 = fr->a[fr->cur].p0; break; case EndLine: for (;f->dot.p1 + 1 < f->s->n && f->s->s[f->dot.p1] != '\n' @@ -823,7 +821,7 @@ move(int arg) break; }; f->dot.p0 = f->dot.p1; - fr_calc(fr); + fr_calc(); } static void @@ -1026,6 +1024,7 @@ str_init(String* p) p->s = emalloc(MINSIZE * sizeof(*p->s)); p->n = 0; p->size = MINSIZE; + p->s[p->n] = '\0'; } static void @@ -1093,6 +1092,7 @@ str_delete(String* p, Posn p0, Posn p1) { memmove(p->s + p0, p->s + p1, p->n - p1); p->n -= p1 - p0; + p->s[p->n] = '\0'; } static void