From: Michael D. Lowis Date: Fri, 7 Jul 2017 12:34:08 +0000 (-0400) Subject: Added support for command buffers in the gap buffer implementation X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=4b68e9a446a0eef7d1a1d1d105318252d2a6d7b9;p=projs%2Ftide.git Added support for command buffers in the gap buffer implementation --- diff --git a/inc/edit.h b/inc/edit.h index 411a054..54e3860 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -59,7 +59,8 @@ typedef struct buf { bool copy_indent; /* copy the indent level from the previous line on new lines */ uint transid; /* tracks the last used transaction id for log entries */ void (*errfn)(char*); /* callback for error messages */ - size_t nlines; + size_t nlines; /* tracks number of lines in the buffer */ + size_t outpoint; /* tracks the point separating output from input for command buffers */ } Buf; /* cursor/selection representation */ diff --git a/lib/buf.c b/lib/buf.c index 4b13134..bd21118 100644 --- a/lib/buf.c +++ b/lib/buf.c @@ -45,6 +45,7 @@ void buf_init(Buf* buf, void (*errfn)(char*)) { buf->redo = NULL; buf->errfn = errfn; buf->nlines = 0; + buf->outpoint = 0; assert(buf->bufstart); } @@ -531,6 +532,7 @@ static void buf_resize(Buf* buf, size_t sz) { static void delete(Buf* buf, size_t off) { Rune rune = buf_get(buf, off); + if (off < buf->outpoint) buf->outpoint--; if (rune == RUNE_CRLF || rune == '\n') buf->nlines--; syncgap(buf, off); @@ -540,6 +542,7 @@ static void delete(Buf* buf, size_t off) { static size_t insert(Buf* buf, size_t off, Rune rune) { size_t rcount = 1; syncgap(buf, off); + if (off < buf->outpoint) buf->outpoint++; if (rune == '\n') buf->nlines++; if (buf->crlf && rune == '\n' && buf_get(buf, off-1) == '\r') { rcount = 0; diff --git a/term.c b/term.c index 5ee6786..ceeb253 100644 --- a/term.c +++ b/term.c @@ -19,7 +19,6 @@ int CmdFD = -1; char* ShellCmd[] = { NULL, NULL }; -size_t Point = 0; static int SearchDir = DOWN; static char* SearchTerm = NULL; @@ -54,16 +53,13 @@ void onupdate(void) { if (!fdready(CmdFD)) return; if ((n = read(CmdFD, buf, sizeof(buf))) < 0) die("read() subprocess :"); - while (i < n) { Rune rune = 0; size_t length = 0; while (!utf8decode(&rune, &length, buf[i++])); -// buf_insert(win_buf(EDIT), false, Point++, rune), r++; - view_insert(win_view(EDIT), false, rune), r++; + view_insert(win_view(EDIT), false, rune); } -// win_view(EDIT)->selection.beg += r; -// win_view(EDIT)->selection.end += r; + win_buf(EDIT)->outpoint = win_view(EDIT)->selection.end; } void onshutdown(void) { @@ -119,17 +115,13 @@ void onmouseright(WinRegion id, bool pressed, size_t row, size_t col) { static void oninput(Rune rune) { view_insert(win_view(FOCUSED), false, rune); if (win_getregion() == EDIT) { - Point++; - size_t bend = win_view(EDIT)->selection.end; - size_t point = buf_end(win_buf(EDIT)) - Point; - if (rune == '\n' && bend > point) { - Sel range = { .beg = point, .end = bend }; + size_t point = win_buf(EDIT)->outpoint; + size_t pos = win_view(EDIT)->selection.end; + if (rune == '\n' && pos > point) { + Sel range = { .beg = point, .end = pos }; char* str = view_getstr(win_view(EDIT), &range); write(CmdFD, str, strlen(str)-1); free(str); - Point -= (range.end - range.beg); - } else if (bend <= point) { - Point--; } } }