From: Michael D. Lowis Date: Sat, 23 Nov 2019 03:46:53 +0000 (-0500) Subject: fixed point behavior on undo/redo X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=8571c34214202287145bd0726d533a68844b8c11;p=projs%2Ftide.git fixed point behavior on undo/redo --- diff --git a/inc/edit.h b/inc/edit.h index 6517427..00d651c 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -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); diff --git a/src/lib/buf.c b/src/lib/buf.c index 05c11f2..252dfa0 100644 --- a/src/lib/buf.c +++ b/src/lib/buf.c @@ -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; diff --git a/src/lib/editlog.c b/src/lib/editlog.c index 6d7cdc0..023edce 100644 --- a/src/lib/editlog.c +++ b/src/lib/editlog.c @@ -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 index 0000000..7eecc44 --- /dev/null +++ b/src/lib/range.c @@ -0,0 +1,33 @@ +#include +#include +#include + +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); +}