From c33de312430f2e65e74d55ee955d49431193139d Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Tue, 3 Apr 2018 14:14:51 -0400 Subject: [PATCH] moved more selection functionality from view to buffer --- inc/edit.h | 4 ++++ lib/buf.c | 23 ++++++++++++++++++----- lib/view.c | 47 +++++++++++------------------------------------ 3 files changed, 33 insertions(+), 41 deletions(-) diff --git a/inc/edit.h b/inc/edit.h index bbd6c4d..c0643fb 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -75,6 +75,10 @@ void buf_setln(Buf* buf, Sel* sel, size_t line); void buf_getcol(Buf* buf, Sel* p_sel); void buf_setcol(Buf* buf, Sel* p_sel); +size_t buf_selsz(Buf* buf, Sel* p_sel); +void buf_selclr(Buf* buf, Sel* p_sel, int dir); +bool buf_insel(Buf* buf, Sel* p_sel, size_t off); + /* Screen management functions *****************************************************************************/ typedef struct { diff --git a/lib/buf.c b/lib/buf.c index db1f5b4..6e364a6 100644 --- a/lib/buf.c +++ b/lib/buf.c @@ -9,13 +9,8 @@ static void buf_resize(Buf* buf, size_t sz); static void log_clear(Log** list); -static void log_insert(Buf* buf, Log** list, size_t beg, size_t end); -static void log_delete(Buf* buf, Log** list, size_t off, char* r, size_t len); static void syncgap(Buf* buf, size_t off); -static void delete(Buf* buf, size_t off); -static size_t insert(Buf* buf, size_t off, Rune rune); static int bytes_match(Buf* buf, size_t mbeg, size_t mend, char* str); -static void swaplog(Buf* buf, Log** from, Log** to, Sel* sel); static Rune nextrune(Buf* buf, size_t off, int move, bool (*testfn)(Rune)); void buf_init(Buf* buf) { @@ -446,3 +441,21 @@ static Rune nextrune(Buf* buf, size_t off, int move, bool (*testfn)(Rune)) { return ret; } +/******************************************************************************/ + +size_t buf_selsz(Buf* buf, Sel* p_sel) { + Sel sel = getsel(buf, p_sel); + return sel.end - sel.beg; +} + +void buf_selclr(Buf* buf, Sel* p_sel, int dir) { + Sel sel = getsel(buf, p_sel); + if (dir > 0) sel.beg = sel.end; + else sel.end = sel.beg; + setsel(buf, p_sel, &sel); +} + +bool buf_insel(Buf* buf, Sel* p_sel, size_t off) { + Sel sel = getsel(buf, p_sel); + return (off >= sel.beg && off < sel.end); +} diff --git a/lib/view.c b/lib/view.c index 606f121..da63780 100644 --- a/lib/view.c +++ b/lib/view.c @@ -8,9 +8,6 @@ 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 move_to(View* view, bool extsel, size_t off); static void select_context(View* view, bool (*isword)(Rune), Sel* sel); -static void selswap(Sel* sel); -static size_t num_selected(Sel sel); -static bool in_selection(Sel sel, size_t off); static bool selection_visible(View* view); static void find_cursor(View* view, size_t* csrx, size_t* csry); static void clearrow(View* view, size_t row); @@ -125,10 +122,10 @@ void view_selword(View* view, size_t row, size_t col) { } void view_selprev(View* view) { - if (!num_selected( *(getsel(view)) )) + if (!view_selsize(view)) buf_lastins(&(view->buffer), getsel(view)); else - getsel(view)->beg = getsel(view)->end; + buf_selclr(&(view->buffer), NULL, RIGHT); } void view_select(View* view, size_t row, size_t col) { @@ -137,7 +134,7 @@ void view_select(View* view, size_t row, size_t col) { } size_t view_selsize(View* view) { - return num_selected( *(getsel(view)) ); + return buf_selsz(&(view->buffer), NULL); } char* view_fetch(View* view, size_t row, size_t col, bool (*isword)(Rune)) { @@ -145,7 +142,7 @@ char* view_fetch(View* view, size_t row, size_t col, bool (*isword)(Rune)) { size_t off = getoffset(view, row, col); if (off != SIZE_MAX) { Sel sel = { .beg = off, .end = off }; - if (in_selection( *(getsel(view)), off)) { + if (buf_insel(&(view->buffer), NULL, off)) { sel = *(getsel(view)); } else { buf_getword(&(view->buffer), isword, &sel); @@ -172,7 +169,7 @@ void view_insert(View* view, bool indent, Rune rune) { } void view_delete(View* view, int dir, bool byword) { - if (getsel(view)->beg == getsel(view)->end) + if (!view_selsize(view)) (byword ? view_byword : view_byrune)(view, dir, true); buf_del(&(view->buffer), getsel(view)); move_to(view, false, getsel(view)->end); @@ -232,13 +229,13 @@ char* view_getstr(View* view, Sel* range) { } char* view_getcmd(View* view) { - if (!num_selected( *(getsel(view)) )) + if (!view_selsize(view)) select_context(view, riscmd, getsel(view)); return view_getstr(view, NULL); } void view_selctx(View* view) { - if (!num_selected( *(getsel(view)) )) + if (!view_selsize(view)) select_context(view, risword, getsel(view)); } @@ -294,17 +291,13 @@ void view_selectobj(View* view, bool (*istype)(Rune)) { static void move_selection(View* view, bool extsel, Sel* sel, int move, movefn_t bything) { view->sync_needed = true; - if (num_selected(*sel) && !extsel) { - selswap(sel); - if (move == RIGHT || move == DOWN) - sel->beg = sel->end; - else - sel->end = sel->beg; + if (buf_selsz(&(view->buffer), sel) && !extsel) { + buf_selclr(&(view->buffer), sel, move); } else { sel->end = bything(&(view->buffer), sel->end, move); if (bything == buf_byline) buf_setcol(&(view->buffer), getsel(view)); - if (!extsel) sel->beg = sel->end; + if (!extsel) buf_selclr(&(view->buffer), sel, RIGHT); } /* only update column if not moving vertically */ if (bything != buf_byline) @@ -342,24 +335,6 @@ static void select_context(View* view, bool (*isword)(Rune), Sel* sel) { buf_getcol(&(view->buffer), getsel(view)); } -static void selswap(Sel* sel) { - if (sel->end < sel->beg) { - size_t temp = sel->beg; - sel->beg = sel->end; - sel->end = temp; - } -} - -static size_t num_selected(Sel sel) { - selswap(&sel); - return (sel.end - sel.beg); -} - -static bool in_selection(Sel sel, size_t off) { - selswap(&sel); - return (sel.beg <= off && off < sel.end); -} - static bool selection_visible(View* view) { if (!view->nrows) return true; size_t csr = getsel(view)->end; @@ -422,7 +397,7 @@ static size_t fill_row(View* view, unsigned row, size_t pos) { view_getrow(view, row)->off = pos; clearrow(view, row); for (size_t x = 0; x < view->ncols;) { - uint32_t attr = (in_selection(*(getsel(view)), pos) ? view->clrsel : view->clrnor); + uint32_t attr = (buf_insel(&(view->buffer), NULL, pos) ? view->clrsel : view->clrnor); Rune r = buf_getrat(&(view->buffer), pos++); x += setcell(view, row, x, attr, r); if (buf_iseol(&(view->buffer), pos-1)) { -- 2.51.0