From 2d7bff3462fa18c821cecb34bc1813523fa687d3 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Thu, 11 Oct 2018 22:53:52 -0400 Subject: [PATCH] refactored selection handling to handle cursor --- TODO.md | 3 +++ inc/edit.h | 1 + src/lib/buf.c | 22 +++++++++++----------- src/tide.c | 1 - 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/TODO.md b/TODO.md index d8c172c..2346079 100644 --- a/TODO.md +++ b/TODO.md @@ -7,6 +7,9 @@ * implement tctl command * gap buffer does not handle UTF-8 currently * Line - Get the current line number(s) containing the selection + +## UNCERTAIN + * refactor selection handling to avoid swapping manually (use buf_selbeg and buf_selend) * encode EOL setting in log entries? * centering logic in view.c seems slightly broken diff --git a/inc/edit.h b/inc/edit.h index 1780a16..1e838c8 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -75,6 +75,7 @@ void buf_getcol(Buf* buf); void buf_setcol(Buf* buf); size_t buf_selbeg(Buf* buf); +size_t buf_selend(Buf* buf); size_t buf_selsz(Buf* buf); void buf_selln(Buf* buf); void buf_selclr(Buf* buf, int dir); diff --git a/src/lib/buf.c b/src/lib/buf.c index 735f63d..32a4669 100644 --- a/src/lib/buf.c +++ b/src/lib/buf.c @@ -644,25 +644,25 @@ size_t buf_selbeg(Buf* buf) { return (sel.beg < sel.end ? sel.beg : sel.end); } +size_t buf_selend(Buf* buf) { + Sel sel = buf_getsel(buf); + return (sel.beg < sel.end ? sel.end : sel.beg); +} + size_t buf_selsz(Buf* buf) { Sel sel = buf_getsel(buf); return sel.end - sel.beg; } void buf_selln(Buf* buf) { - /* swap the direction of the selection so beg < end */ - if (buf->selection.beg > buf->selection.end) { - size_t off = buf->selection.beg; - buf->selection.beg = buf->selection.end; - buf->selection.end = off; - } - /* Expand the selection to completely select the lines covered */ - buf->selection.beg = buf_bol(buf, buf->selection.beg); - if (!buf_iseol(buf, buf->selection.end-1) || buf->selection.end == buf->selection.beg) { - buf->selection.end = buf_eol(buf, buf->selection.end); - buf->selection.end = buf_byrune(buf, buf->selection.end, RIGHT); + Sel sel = (Sel){ .beg = buf_selbeg(buf), .end = buf_selend(buf) }; + sel.beg = buf_bol(buf, sel.beg); + if (!buf_iseol(buf, sel.end-1) || sel.end == sel.beg) { + sel.end = buf_eol(buf, sel.end); + sel.end = buf_byrune(buf, sel.end, RIGHT); } + buf->selection = sel; } void buf_selclr(Buf* buf, int dir) { diff --git a/src/tide.c b/src/tide.c index e324ba8..30de94f 100644 --- a/src/tide.c +++ b/src/tide.c @@ -339,7 +339,6 @@ static void goto_ctag(char* arg) { char* str = view_getctx(win_view(FOCUSED)); jump_to(str); free(str); - } static void tabs(char* arg) { -- 2.51.0