sim

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

commit b59977b168ed18b1d21746fb0a23260e64225f67
parent e6cb2cb70b44fad1b65f8ac9f48ab53d9f998c6f
Author: ssnf <ssnf@ssnf.xyz>
Date:   Fri, 25 Nov 2022 18:46:17 +0000

reformat code

- remove unnecessary variable assignment in blind_writer()
- cases have the same indentation of their switch statement
- limit of 79 characters per line

Diffstat:
Msim.c | 516+++++++++++++++++++++++++++++++++++++++++++------------------------------------
1 file changed, 284 insertions(+), 232 deletions(-)

diff --git a/sim.c b/sim.c @@ -82,8 +82,14 @@ 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 buf_add(Buffer* b, String* is, String* ds, uint c, uint arg, uint count, Posn p0); +static void blind_writer( + String* s, Frame* fr, + 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 +); static void change(int arg); static void count(int arg); static void delete(int arg); @@ -190,13 +196,13 @@ blind_reader(Frame* fr, String* s, Posn p0) do { for (wx = 0; wx < w.wx && p1 < s->n;) { switch (s->s[p1]) { - case '\t': - wx += w.t - (wx % w.t); - break; - case '\n': - goto endloop; - default: - ++wx; + case '\t': + wx += w.t - (wx % w.t); + break; + case '\n': + goto endloop; + default: + ++wx; } if (wx < w.wx) ++p1; @@ -214,8 +220,10 @@ blind_reader(Frame* fr, String* s, Posn p0) } static void -blind_writer(String* s, Frame* fr, ushort line, ushort offset, ushort top, ushort bot) -{ +blind_writer( + String* s, Frame* fr, + ushort line, ushort offset, ushort top, ushort bot +) { ushort i, o; i = o = 0; @@ -225,14 +233,15 @@ blind_writer(String* s, Frame* fr, ushort line, ushort offset, ushort top, ushor o = offset + bot - 1; else if (fr->n) o = fr->n - 1; - line = offset > line ? 0 : line - offset; - printf(CSI "%uH", line); + 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); } static void -buf_add(Buffer* b, String* is, String* ds, uint c, uint arg, uint count, Posn p0) -{ +buf_add( + Buffer* b, String* is, String* ds, + uint c, uint arg, uint count, Posn p0 +) { b->n = ++f->i + 1; str_zero(&b->is[f->i]); str_zero(&b->ds[f->i]); @@ -257,12 +266,12 @@ change(int arg) if (!arg) arg = fgetc(stdin); switch (arg) { - case 'x': arg = Letter; break; - case 'c': arg = Line; break; - case 'G': arg = Bottom; break; - case 'g': arg = Top; break; - case 'w': arg = Word; break; - case 't': arg = Till; break; + case 'x': arg = Letter; break; + case 'c': arg = Line; break; + case 'G': arg = Bottom; break; + case 'g': arg = Top; break; + case 'w': arg = Word; break; + case 't': arg = Till; break; } count = counter; arg = selection(arg); @@ -306,14 +315,14 @@ delete(int arg) return; if (!arg) { switch (arg = fgetc(stdin)) { - case 'x': arg = Letter; break; - case 'd': arg = Line; break; - case 'G': arg = Bottom; break; - case 'g': arg = Top; break; - case 'w': arg = Word; break; - case 't': arg = Till; break; - default: - return; + case 'x': arg = Letter; break; + case 'd': arg = Line; break; + case 'G': arg = Bottom; break; + case 'g': arg = Top; break; + case 'w': arg = Word; break; + case 't': arg = Till; break; + default: + return; } } count = counter; @@ -342,32 +351,37 @@ dot(int arg) arg = buf->arg[f->i]; counter = buf->count[f->i]; switch (buf->c[f->i]) { - case Insert: - if (arg == Down) - move(EndLine); - else - move(arg); - str_insert(f->s, &istr, f->dot.p0); - buf_add(buf, &istr, NULL, Insert, arg, counter, f->dot.p0); - break; - case Delete: - delete(arg); - break; - case Change: - str_init(&ds); - if (buf->ds[f->i].n) { - selection(arg); - if (arg == Word || arg == Letter || arg > 0x7f) - ++f->dot.p1; - str_adds(&ds, f->s->s + f->dot.p0, f->dot.p1 - f->dot.p0); - 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, &buf->is[f->i], &ds, Change, arg, buf->count[f->i], f->dot.p0); - str_close(&ds); - f->dot.p1 = f->dot.p0; - break; + case Insert: + if (arg == Down) + move(EndLine); + else + move(arg); + str_insert(f->s, &istr, f->dot.p0); + buf_add(buf, &istr, NULL, Insert, arg, counter, f->dot.p0); + break; + case Delete: + delete(arg); + break; + case Change: + str_init(&ds); + if (buf->ds[f->i].n) { + selection(arg); + if (arg == Word || arg == Letter || arg > 0x7f) + ++f->dot.p1; + str_adds( + &ds, f->s->s + f->dot.p0, f->dot.p1 - f->dot.p0 + ); + 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, &buf->is[f->i], &ds, Change, arg, + buf->count[f->i], f->dot.p0 + ); + str_close(&ds); + f->dot.p1 = f->dot.p0; + break; } fr_update(); } @@ -378,7 +392,9 @@ redo(int arg) if (f->i == buf->n - 1) return; if (buf->ds[++f->i].n) - str_delete(f->s, buf->p0[f->i], buf->p0[f->i] + 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]; @@ -410,15 +426,15 @@ input(String* s, ushort line, char* msg) for (;;) { printf(CSI "%uH" EL "%s%s", line, msg, s->s); switch (c = fgetc(stdin)) { - case Esc: - str_zero(s); - case '\n': - return; - case Del: - str_delc(s); - break; - default: - str_addc(s, c); + case Esc: + str_zero(s); + case '\n': + return; + case Del: + str_delc(s); + break; + default: + str_addc(s, c); } } } @@ -427,14 +443,14 @@ static int isword(uchar c) { switch (c) { - case ' ': case '\t': case '\n': case '.': - case '(': case ')': case '{': case '}': - case '[': case ']': case ':': case ';': - case ',': case '<': case '>': case '#': - case '*': case '+': case '-': case '!': - case '%': case '\\': case '/': case '"': - case '=': - return 0; + case ' ': case '\t': case '\n': case '.': + case '(': case ')': case '{': case '}': + case '[': case ']': case ':': case ';': + case ',': case '<': case '>': case '#': + case '*': case '+': case '-': case '!': + case '%': case '\\': case '/': case '"': + case '=': + return 0; } return 1; } @@ -451,34 +467,34 @@ insert(int arg) str_init(&s), str_init(&c); str_addc(&c, '\0'); switch (arg) { - case StartLine: - case EndLine: - case Right: - move(arg); - break; - case Down: - move(EndLine); - str_addc(&s, '\n'); - str_insert(f->s, &s, f->dot.p0); - ++f->dot.p1; - break; + case StartLine: + case EndLine: + case Right: + move(arg); + break; + case Down: + move(EndLine); + str_addc(&s, '\n'); + str_insert(f->s, &s, f->dot.p0); + ++f->dot.p1; + break; } for (;;) { resize(); fr_update(); switch (c.s[0] = fgetc(stdin)) { - case Esc: - goto endmode; - case Del: - if (f->dot.p1 != f->dot.p0) { - str_delc(&s); - str_delete(f->s, f->dot.p1 - 1, f->dot.p1); - --f->dot.p1; - } - break; - default: - str_addc(&s, c.s[0]); - str_insert(f->s, &c, f->dot.p1); + case Esc: + goto endmode; + case Del: + if (f->dot.p1 != f->dot.p0) { + str_delc(&s); + str_delete(f->s, f->dot.p1 - 1, f->dot.p1); + --f->dot.p1; + } + break; + default: + str_addc(&s, c.s[0]); + str_insert(f->s, &c, f->dot.p1); } f->dot.p1 = f->dot.p0 + s.n; } @@ -519,7 +535,10 @@ file_close(int arg) if (arg == -1) arg = f - file; if (file[arg].dirty) { - printf(CSI "%uH" EL CSI "31mSave %s?" CSI "0m [y/n]", w.wy/2, file[arg].name->n ? file[arg].name->s : "-unnamed-"); + printf( + CSI "%uH" EL CSI "31mSave %s?" CSI "0m [y/n]", w.wy/2, + file[arg].name->n ? file[arg].name->s : "-unnamed-" + ); if (fgetc(stdin) == 'y') file_save(arg); } @@ -562,7 +581,10 @@ static void file_open(int c) { if (f->dirty) { - printf(CSI "%uH" EL CSI "31mSave %s?" CSI "0m [y/n]", w.wy/2, f->name->s); + printf( + CSI "%uH" EL CSI "31mSave %s?" CSI "0m [y/n]", w.wy/2, + f->name->s + ); if (fgetc(stdin) == 'y') file_save(f - file); } @@ -610,7 +632,9 @@ fr_calc(Frame* fr) || 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) + || (fr->dot + half + 1 > fr->n + && fr->a[fr->n ? fr->n - 1 : 0].p1 + 1 < f->s->n + ) ) { fr_zero(fr); for (p0 = f->dot.p1; p0; --p0) @@ -711,77 +735,94 @@ static void move(int arg) { switch (arg) { - case Left: - if (f->dot.p1) - --f->dot.p1; - break; - case Right: - if (f->dot.p1 + 1 < f->s->n) - ++f->dot.p1; - break; - case Up: - if (fr->dot) - f->dot.p1 = fr->a[fr->dot - 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; - else - f->dot.p1 = fr->a[fr->dot].p1 - 1; - 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; - break; - case HalfDown: - if (!f->s->n) - break; - if (fr->n - fr->dot <= 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; - break; - case Top: + case Left: + if (f->dot.p1) + --f->dot.p1; + break; + case Right: + if (f->dot.p1 + 1 < f->s->n) + ++f->dot.p1; + break; + case Up: + if (fr->dot) + f->dot.p1 = fr->a[fr->dot - 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; + else + f->dot.p1 = fr->a[fr->dot].p1 - 1; + 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; + break; + case HalfDown: + if (!f->s->n) break; - case Bottom: - if (f->s->n) - 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); - f->dot.p1 = fr->a[fr->dot].p0; - break; - case EndLine: - for (;f->dot.p1 + 1 < f->s->n && f->s->s[f->dot.p1] != '\n'; ++f->dot.p1); - break; - case Word: - for (;f->dot.p1 + 1 < f->s->n && isword(f->s->s[f->dot.p1]); ++f->dot.p1); - for (;f->dot.p1 + 1 < f->s->n && !isword(f->s->s[f->dot.p1]); ++f->dot.p1); - break; - case EndWord: + if (fr->n - fr->dot <= 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; + break; + case Top: + f->dot.p1 = 0; + break; + case Bottom: + if (f->s->n) + 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 + ); + f->dot.p1 = fr->a[fr->dot].p0; + break; + case EndLine: + for (;f->dot.p1 + 1 < f->s->n && f->s->s[f->dot.p1] != '\n' + ; ++f->dot.p1 + ); + break; + case Word: + for (;f->dot.p1 + 1 < f->s->n && isword(f->s->s[f->dot.p1]) + ; ++f->dot.p1 + ); + for (;f->dot.p1 + 1 < f->s->n && !isword(f->s->s[f->dot.p1]) + ; ++f->dot.p1 + ); + break; + case EndWord: + move(Right); + for (;f->dot.p1 < f->s->n && !isword(f->s->s[f->dot.p1]) + ; ++f->dot.p1 + ); + for (;f->dot.p1 < f->s->n && isword(f->s->s[f->dot.p1]) + ; ++f->dot.p1 + ); + move(Left); + break; + case PrevWord: + move(Left); + for (;f->dot.p1 > 0 && !isword(f->s->s[f->dot.p1]) + ; --f->dot.p1 + ); + for (;f->dot.p1 > 0 && isword(f->s->s[f->dot.p1]) + ; --f->dot.p1 + ); + if (f->dot.p1) move(Right); - for (;f->dot.p1 < f->s->n && !isword(f->s->s[f->dot.p1]); ++f->dot.p1); - for (;f->dot.p1 < f->s->n && isword(f->s->s[f->dot.p1]); ++f->dot.p1); - move(Left); - break; - case PrevWord: - move(Left); - for (;f->dot.p1 > 0 && !isword(f->s->s[f->dot.p1]); --f->dot.p1); - for (;f->dot.p1 > 0 && isword(f->s->s[f->dot.p1]); --f->dot.p1); - if (f->dot.p1) - move(Right); - break; + break; }; f->dot.p0 = f->dot.p1; fr_calc(fr); @@ -826,7 +867,9 @@ pline() ulong i, j, c, l; for (l = f->dot.p1 = 0; f->dot.p1 < f->dot.p0; ++l) { - for (;f->dot.p1 + 1 < f->s->n && f->s->s[f->dot.p1] != '\n'; ++f->dot.p1); + for (;f->dot.p1 + 1 < f->s->n && f->s->s[f->dot.p1] != '\n'; + ++f->dot.p1 + ); if (f->dot.p1 + 1 < f->s->n) ++f->dot.p1; } @@ -838,18 +881,18 @@ pline() for (i = 0; i < j * counter && i < w.wy/2 ; ++i) printf(CSI "%luH" EL "%lu", w.wy/2 + i, i ? i : l); switch (c = fgetc(stdin)) { - case '!': - ++j; - break; - case '+': - ++counter; - break; - case '-': - if (counter > 1) - --counter; - default: - counter = 0; - return; + case '!': + ++j; + break; + case '+': + ++counter; + break; + case '-': + if (counter > 1) + --counter; + default: + counter = 0; + return; } } } @@ -893,7 +936,10 @@ search(int arg) if (srch.s[0] == '\n' && srch.s[1] != '\n') move(Left); for (;;) { - for (;move(Left), f->dot.p1 && f->s->s[f->dot.p1] != srch.s[0];); + for (;move(Left), f->dot.p1 + && f->s->s[f->dot.p1] != srch.s[0] + ; + ); if (!strncmp(f->s->s + f->dot.p1, srch.s, srch.n)) break; if (!f->dot.p1) { @@ -922,50 +968,54 @@ selection(int arg) } p0 = f->dot.p1 = f->dot.p0; switch (arg) { - case Letter: - p0 = f->dot.p0; - for (;counter > 1; --counter) - move(Right); - break; - case Line: - move(StartLine); - p0 = f->dot.p0; - for (;counter; --counter) { - move(EndLine); - move(Right); - } - if (f->dot.p1 + 1 < f->s->n) - move(Left); - break; - case Bottom: - move(StartLine); - move(Left); - p0 = f->dot.p0; - move(Bottom); - break; - case Top: - p0 = 0; + case Letter: + p0 = f->dot.p0; + for (;counter > 1; --counter) + move(Right); + break; + case Line: + move(StartLine); + p0 = f->dot.p0; + for (;counter; --counter) { move(EndLine); - break; - case Word: - p0 = f->dot.p0; - for (;counter; --counter) - move(EndWord); - break; - case Till: - arg = fgetc(stdin); - if (arg == Esc) - return 0; - till: - p0 = f->dot.p0; - for (;counter && f->dot.p1 + 1 < f->s->n; --counter) - for (++f->dot.p1; f->dot.p1 + 1 < f->s->n && f->s->s[f->dot.p1 + 1] != arg; ++f->dot.p1); - if (f->s->s[f->dot.p1 + 1] != arg) { - f->dot.p1 = f->dot.p0; - return -1; - } - arg += 0x7f; - break; + move(Right); + } + if (f->dot.p1 + 1 < f->s->n) + move(Left); + break; + case Bottom: + move(StartLine); + move(Left); + p0 = f->dot.p0; + move(Bottom); + break; + case Top: + p0 = 0; + move(EndLine); + break; + case Word: + p0 = f->dot.p0; + for (;counter; --counter) + move(EndWord); + break; + case Till: + arg = fgetc(stdin); + if (arg == Esc) + return 0; + till: + p0 = f->dot.p0; + for (;counter && f->dot.p1 + 1 < f->s->n; --counter) + for (++f->dot.p1 + ; f->dot.p1 + 1 < f->s->n + && f->s->s[f->dot.p1 + 1] != arg + ; ++f->dot.p1 + ); + if (f->s->s[f->dot.p1 + 1] != arg) { + f->dot.p1 = f->dot.p0; + return -1; + } + arg += 0x7f; + break; } f->dot.p0 = p0; counter = 0; @@ -1051,7 +1101,9 @@ undo(int arg) if (f->i < 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); + 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--]; @@ -1067,13 +1119,13 @@ yank(int arg) return; if (!arg) { switch (arg = fgetc(stdin)) { - case 'y': arg = Line; break; - case 'G': arg = Bottom; break; - case 'g': arg = Top; break; - case 'w': arg = Word; break; - case 't': arg = Till; break; - default: - return; + case 'y': arg = Line; break; + case 'G': arg = Bottom; break; + case 'g': arg = Top; break; + case 'w': arg = Word; break; + case 't': arg = Till; break; + default: + return; } } if ((arg = selection(arg)) < 0)