sim

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

commit 4aded4c51538b9e66fd23f5a29a80b04b7d223d4
parent db616ce8f7959e758d1c433bc4dafa37472d8702
Author: ssnf <ssnf@ssnf.xyz>
Date:   Sat, 21 Aug 2021 19:14:42 +0000

added word movement

Diffstat:
Mconfig.def.h | 5++++-
Msim.c | 67++++++++++++++++++++++++++++++++++++++++++++++++++++---------------
2 files changed, 56 insertions(+), 16 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -9,6 +9,9 @@ Key keys[] = { { '0', move, StartLine }, { 'G', move, Bottom }, { 'g', move, Top }, + { 'w', move, Word }, + { 'e', move, EndWord }, + { 'b', move, PrevWord }, { 'i', insert }, { 'I', insert , StartLine}, { 'A', insert , EndLine }, @@ -17,7 +20,7 @@ Key keys[] = { { 'O', insert , Up }, { 'd', delete }, { 'x', delete , 1 }, - { 'e', file_open }, + { 'E', file_open }, { 'q', file_close, -1 }, { 's', file_save, -1 }, { 'Q', quit }, diff --git a/sim.c b/sim.c @@ -23,6 +23,9 @@ enum { Bottom, StartLine, EndLine, + Word, + EndWord, + PrevWord, Ctrl = -0x60, Esc = 0x1b, Del = 0x7f @@ -68,6 +71,7 @@ static void escape(int c); static void init(); static void input(String* s, char* msg); static void insert(int arg); +static int isword(uchar c); static void file_close(int arg); static void file_init(File* f); static void file_load(File* f); @@ -266,6 +270,21 @@ input(String* s, char* msg) } } +static int +isword(uchar c) +{ + switch (c) { + case ' ': + case '\t': + case '\n': + case '.': + case '(': + case ')': + return 0; + } + return 1; +} + static void insert(int arg) { @@ -523,55 +542,73 @@ move(int arg) switch (arg) { case Left: if (f->dot.p1) - f->dot.p0 = --f->dot.p1; + --f->dot.p1; break; case Right: if (f->dot.p1 + 1 < f->s->n) - f->dot.p0 = ++f->dot.p1; + ++f->dot.p1; break; case Up: if (fr->dot) - f->dot.p0 = f->dot.p1 = fr->a[fr->dot - 1].p0; + f->dot.p1 = fr->a[fr->dot - 1].p0; else - f->dot.p0 = f->dot.p1 = 0; + f->dot.p1 = 0; break; case Down: if (fr->dot < fr->n - 1) - f->dot.p0 = f->dot.p1 = fr->a[fr->dot + 1].p0; + f->dot.p1 = fr->a[fr->dot + 1].p0; else - f->dot.p0 = f->dot.p1 = fr->a[fr->dot].p1; + f->dot.p1 = fr->a[fr->dot].p1; break; case HalfUp: if (fr->dot < w.wy/2) - f->dot.p0 = f->dot.p1 = 0; + f->dot.p1 = 0; else - f->dot.p0 = f->dot.p1 = fr->a[fr->dot - w.wy/2].p0; + 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.p0 = f->dot.p1 = fr->a[fr->n - 1].p0; + f->dot.p1 = fr->a[fr->n - 1].p0; else - f->dot.p0 = f->dot.p1 = f->s->n; + f->dot.p1 = f->s->n; } else - f->dot.p0 = f->dot.p1 = fr->a[fr->dot + w.wy/2].p0; + f->dot.p1 = fr->a[fr->dot + w.wy/2].p0; break; case Top: - f->dot.p0 = f->dot.p1 = 0; + f->dot.p1 = 0; break; case Bottom: if (f->s->n) - f->dot.p0 = f->dot.p1 = f->s->n - 1; + f->dot.p1 = f->s->n - 1; break; case StartLine: - f->dot.p0 = f->dot.p1 = fr->a[fr->dot].p0; + f->dot.p1 = fr->a[fr->dot].p0; break; case EndLine: - f->dot.p0 = f->dot.p1 = fr->a[fr->dot].p1; + f->dot.p1 = fr->a[fr->dot].p1; + break; + case Word: + 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); + 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); break; }; + f->dot.p0 = f->dot.p1; } static void