From ecebaf9c1136a7a5e0dd6216ad4c627c63ec6eb7 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Thu, 1 Jun 2017 08:28:50 -0400 Subject: [PATCH] refactored selection movement code to share common logic --- lib/view.c | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/lib/view.c b/lib/view.c index a614904..46874c6 100644 --- a/lib/view.c +++ b/lib/view.c @@ -4,6 +4,9 @@ #include #include +typedef size_t (*movefn_t)(Buf* buf, size_t pos, int count); + +static void move_selection(View* view, bool extsel, Sel* sel, int move, movefn_t bything); static void selcontext(View* view, bool (*isword)(Rune), Sel* sel); static void clearrow(View* view, size_t row); static size_t setcell(View* view, size_t row, size_t col, uint32_t attr, Rune r); @@ -94,37 +97,17 @@ Row* view_getrow(View* view, size_t row) { } void view_byrune(View* view, int move, bool extsel) { - Sel sel = view->selection; - if (view_selsize(view) && !extsel) { - selswap(&sel); - if (move == RIGHT) - sel.beg = sel.end; - else - sel.end = sel.beg; - } else { - sel.end = buf_byrune(&(view->buffer), sel.end, move); - if (!extsel) sel.beg = sel.end; - } - sel.col = buf_getcol(&(view->buffer), sel.end); - view->selection = sel; + move_selection(view, extsel, &(view->selection), move, buf_byrune); view->sync_needed = true; } void view_byword(View* view, int move, bool extsel) { - Sel sel = view->selection; - sel.end = buf_byword(&(view->buffer), sel.end, move); - if (!extsel) sel.beg = sel.end; - sel.col = buf_getcol(&(view->buffer), sel.end); - view->selection = sel; + move_selection(view, extsel, &(view->selection), move, buf_byword); view->sync_needed = true; } void view_byline(View* view, int move, bool extsel) { - Sel sel = view->selection; - sel.end = buf_byline(&(view->buffer), sel.end, move); - sel.end = buf_setcol(&(view->buffer), sel.end, sel.col); - if (!extsel) sel.beg = sel.end; - view->selection = sel; + move_selection(view, extsel, &(view->selection), move, buf_byline); view->sync_needed = true; } @@ -442,6 +425,22 @@ void view_scrollto(View* view, size_t csr) { } } +static void move_selection(View* view, bool extsel, Sel* sel, int move, movefn_t bything) { + if (num_selected(*sel) && !extsel) { + selswap(sel); + if (move == RIGHT) + sel->beg = sel->end; + else + sel->end = sel->beg; + } else { + sel->end = bything(&(view->buffer), sel->end, move); + if (bything == buf_byline) + sel->end = buf_setcol(&(view->buffer), sel->end, sel->col); + if (!extsel) sel->beg = sel->end; + } + sel->col = buf_getcol(&(view->buffer), sel->end); +} + static void selcontext(View* view, bool (*isword)(Rune), Sel* sel) { Buf* buf = &(view->buffer); size_t bol = buf_bol(buf, sel->end); -- 2.52.0