]> git.mdlowis.com Git - projs/tide.git/commitdiff
Added support for command buffers in the gap buffer implementation
authorMichael D. Lowis <mike.lowis@gentex.com>
Fri, 7 Jul 2017 12:34:08 +0000 (08:34 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Fri, 7 Jul 2017 12:34:08 +0000 (08:34 -0400)
inc/edit.h
lib/buf.c
term.c

index 411a0547da2f40711c39250d9c375452343bf890..54e38604790eda2838fb7690dc9732518dae06d4 100644 (file)
@@ -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 */
index 4b1313478960355f9d4318c445256b1ef4652a04..bd21118f8019f17b18083219db043c5f1a61fb3b 100644 (file)
--- 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 5ee6786139fa4b803e01f657bc79d9f61d91aad4..ceeb253f173ee03640ba71787e250f0a4c7430b6 100644 (file)
--- 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--;
         }
     }
 }