]> git.mdlowis.com Git - projs/tide.git/commitdiff
fixed point behavior on undo/redo
authorMichael D. Lowis <mike@mdlowis.com>
Sat, 23 Nov 2019 03:46:53 +0000 (22:46 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Sat, 23 Nov 2019 03:46:53 +0000 (22:46 -0500)
inc/edit.h
src/lib/buf.c
src/lib/editlog.c
src/lib/range.c [new file with mode: 0644]

index 651742735c6f1e475d4bd6e2ae9c091cbca78a4d..00d651c03b7731cb7b002573b7d1b4f6f49e4215 100644 (file)
@@ -50,6 +50,9 @@ enum {
     BY_LINE
 };
 
+void range_add(Sel* p_range, size_t off);
+void range_del(Sel* p_range, size_t beg, size_t end);
+
 void gapbuf_init(GapBuf* buf);
 size_t gapbuf_end(GapBuf* buf);
 long gapbuf_save(GapBuf* buf, char* path);
index 05c11f2a55d1355faac54c11b7a1102fec7774fa..252dfa0e373968deea68ff2b7ab46651c5160fb6 100644 (file)
@@ -88,14 +88,7 @@ static void selset(Buf* buf, Sel sel)
 
 static void putb(Buf* buf, char b, Sel* p_sel)
 {
-    if (p_sel->end < buf->point.beg)
-    {
-        buf->point.beg++;
-    }
-    if (p_sel->end <= buf->point.end)
-    {
-        buf->point.end++;
-    }
+    range_add(&(buf->point), p_sel->end);
     gapbuf_putb(&buf->contents, b, p_sel);
 }
 
@@ -366,23 +359,10 @@ void buf_del(Buf* buf)
     size_t nbytes = sel.end - sel.beg;
     if (nbytes > 0)
     {
-        /* adjust the point according to the characters deleted */
-        size_t bpoint = 0;
-        if (sel.beg <= buf->point.beg)
-        {
-            bpoint = min(nbytes, buf->point.beg - sel.beg);
-        }
-        size_t inpoint = 0;
-        if (sel.end >= buf->point.beg)
-        {
-            inpoint = min(sel.end, buf->point.end) - max(sel.beg, buf->point.beg);
-        }
-        buf->point.beg -= bpoint;
-        buf->point.end -= (bpoint + inpoint);
-
         /* perform the delete */
         buf->status = MODIFIED;
         char* str = buf_gets(buf);
+        range_del(&(buf->point), sel.beg, sel.end);
         gapbuf_del(&buf->contents, sel.beg, nbytes);
         sel.end = sel.beg;
         buf->selection = sel;
index 6d7cdc06bf14e2fbdbdd64bbb402cb0efe683517..023edcead1d2b065b3ae090d4d8d411750650154 100644 (file)
@@ -28,6 +28,7 @@ static void log_swap(Buf* buf, Log** src, Log** dest)
             /* reinsert deleted bytes */
             for (char* s = item->data; s && *s; s++, item->end++)
             {
+                range_add(&(buf->point), buf->selection.end);
                 gapbuf_putb(&buf->contents, *s, &(buf->selection));
                 buf->status = MODIFIED;
             }
@@ -41,6 +42,7 @@ static void log_swap(Buf* buf, Log** src, Log** dest)
             /* delete the added bytes */
             Sel sel = buf->selection;
             item->data = buf_gets(buf);
+            range_del(&(buf->point), item->beg, item->end);
             gapbuf_del(&buf->contents, sel.beg, (item->end - item->beg));
             sel.end = sel.beg;
             buf->selection = sel;
diff --git a/src/lib/range.c b/src/lib/range.c
new file mode 100644 (file)
index 0000000..7eecc44
--- /dev/null
@@ -0,0 +1,33 @@
+#include <stdc.h>
+#include <utf.h>
+#include <edit.h>
+
+void range_add(Sel* p_range, size_t off)
+{
+    if (off < p_range->beg)
+    {
+        p_range->beg++;
+    }
+    if (off <= p_range->end)
+    {
+        p_range->end++;
+    }
+}
+
+void range_del(Sel* p_range, size_t beg, size_t end)
+{
+    /* adjust the point according to the characters deleted */
+    size_t nbytes = end - beg;
+    size_t bpoint = 0;
+    if (beg <= p_range->beg)
+    {
+        bpoint = min(nbytes, p_range->beg - beg);
+    }
+    size_t inpoint = 0;
+    if (end >= p_range->beg)
+    {
+        inpoint = min(end, p_range->end) - max(beg, p_range->beg);
+    }
+    p_range->beg -= bpoint;
+    p_range->end -= (bpoint + inpoint);
+}