sim

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

commit c1e9c1b2de5baf65430b02e3da6022b64af47d6e
parent b759e7242bcc4d5467c48672662a06b0af431d35
Author: ssnf <ssnf@ssnf.xyz>
Date:   Wed, 18 May 2022 13:33:17 +0000

Implemented gmove(), detached frame calculation from fr_update()

Diffstat:
Mconfig.def.h | 22+++++++++++-----------
Msim.c | 114++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
2 files changed, 81 insertions(+), 55 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -22,25 +22,25 @@ Key keys[] = { { 'E', file_open }, { 'q', file_close, -1 }, { 'S', file_save, -1 }, + { 'h', gmove, Left }, + { 'j', gmove, Down }, + { 'k', gmove, Up }, + { 'l', gmove, Right }, + { Ctrl + 'd', gmove, HalfDown }, + { Ctrl + 'u', gmove, HalfUp }, + { 'g', gmove, Top }, + { 'w', gmove, Word }, + { 'e', gmove, EndWord }, + { 'b', gmove, PrevWord }, { 'i', insert }, { 'I', insert , StartLine}, { 'A', insert , EndLine }, { 'a', insert , Right }, { 'o', insert , Down }, { 'O', insert , Up }, - { '!', pline }, - { 'h', move, Left }, - { 'j', move, Down }, - { 'k', move, Up }, - { 'l', move, Right }, - { Ctrl + 'd', move, HalfDown }, - { Ctrl + 'u', move, HalfUp }, + { '@', pline }, { '$', move, EndLine }, { 'G', move, Bottom }, - { 'g', move, Top }, - { 'w', move, Word }, - { 'e', move, EndWord }, - { 'b', move, PrevWord }, { 'p', paste }, { 'Q', quit }, { '/', search, '/' }, diff --git a/sim.c b/sim.c @@ -106,6 +106,7 @@ 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_zero(Frame*); +static void gmove(int arg); static void paste(int arg); static void pline(); static void search(int arg); @@ -591,6 +592,48 @@ fr_add(Frame* fr, Posn p0, Posn p1) } static void +fr_calc(Frame* fr) +{ + Frame fr0; + Posn p0, p1; + ushort half; + + half = w.wy >> 1; + if (!fr->n || f->dot.p1 < fr->a[fr->dot].p0 + || f->dot.p1 > fr->a[fr->n ? fr->n - 1 : 0].p1 + || (((fr->dot < half && fr->a[0].p0 && (fr->n > w.wy * 3)) + || (fr->dot + half + 1 > fr->n)) + && (fr->a[fr->n ? fr->n - 1 : 0].p1 != f->s->n) + ) + ) { + 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 (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; + } + } + 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 fr_close(Frame* fr) { free(fr->a); @@ -626,9 +669,8 @@ fr_insure(Frame* fr, ushort n) static void fr_update() { - Frame fr0; - Posn p0, p1; - ushort half; + Posn p0, p1; + uint half; half = w.wy >> 1; if (!f->s->n) { @@ -636,46 +678,17 @@ fr_update() printf(ED); goto status; } - if (1 || !fr->n || f->dot.p1 < fr->a[fr->dot].p0 - || f->dot.p1 > fr->a[fr->n ? fr->n - 1 : 0].p1 - || (((fr->dot < half && fr->a[0].p0 && (fr->n > w.wy * 3)) - || (fr->dot + half + 1 > fr->n)) - && (fr->a[fr->n ? fr->n - 1 : 0].p1 != f->s->n) - ) - ) { - 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); - if ((p1 = fr0.a[0].p0)) - --p1; - fr_insert(fr, &fr0, 0); - fr_zero(&fr0); - } - fr_close(&fr0); - } - 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; - } - } - for (;f->dot.p1 < fr->a[fr->dot].p0 && fr->dot; --fr->dot); - for (;f->dot.p1 > fr->a[fr->dot].p1; ++fr->dot); - printf(ED); + fr_calc(fr); + fwrite(ED, sizeof(ED), 1, stdout); blind_writer(f->s, fr, half, fr->dot, 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) { - if (f->s->s[p0] == '\t' && p1 % 4) { + if (f->s->s[p0] == '\t' && p1 % 4) p1 += 5 - (p1 % 4); - } else ++p1; + else + ++p1; } printf(CSI "%u;%luH", half, p1); return; @@ -705,20 +718,23 @@ move(int arg) f->dot.p1 = fr->a[fr->dot - 1].p0; else f->dot.p1 = 0; + fr_calc(fr); break; case Down: if (!f->s->n) return; - else if (fr->dot < fr->n - 1) + if (fr->dot < fr->n - 1) f->dot.p1 = fr->a[fr->dot + 1].p0; else f->dot.p1 = fr->a[fr->dot].p1 - 1; + fr_calc(fr); break; case HalfUp: if (fr->dot < w.wy/2) f->dot.p1 = 0; else f->dot.p1 = fr->a[fr->dot - w.wy/2].p0; + fr_calc(fr); break; case HalfDown: if (!f->s->n) @@ -730,14 +746,10 @@ move(int arg) f->dot.p1 = f->s->n; } else f->dot.p1 = fr->a[fr->dot + w.wy/2].p0; + fr_calc(fr); break; case Top: f->dot.p1 = 0; - if (counter) - for (counter ? --counter : 0; counter; --counter) { - move(EndLine); - move(Right); - } break; case Bottom: if (f->s->n) @@ -784,13 +796,27 @@ quit(int arg) } static void +gmove(int arg) +{ + if (arg == Top) { + move(Top); + for (counter ? --counter : 0; counter; --counter) { + move(EndLine); + move(Right); + } + return; + } + for (!counter ? ++counter : 0; counter; --counter) + move(arg); +} + +static void paste(int arg) { str_insert(f->s, &istr, f->dot.p0); buf_add(buf, &istr, NULL, Insert, 0, 1, f->dot.p0); } - static void pline() {