]> git.mdlowis.com Git - projs/tide.git/commitdiff
moved more selection functionality from view to buffer
authorMichael D. Lowis <mike.lowis@gentex.com>
Tue, 3 Apr 2018 18:14:51 +0000 (14:14 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Tue, 3 Apr 2018 18:14:51 +0000 (14:14 -0400)
inc/edit.h
lib/buf.c
lib/view.c

index bbd6c4d526f62a962df86e4ee13d34b1d5985bd1..c0643fb2a17cf5636b800f652282d636c54b478d 100644 (file)
@@ -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 {
index db1f5b451743c478f8636eaaa21c0a00dd580fc8..6e364a65069e4eddfb46fc52391009b1f1eed1f7 100644 (file)
--- 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);
+}
index 606f121e0fcbbf37a3d7ca4df2d7640caa0fe3bf..da63780c3f1bbd1136d867a4d851db927d956b6c 100644 (file)
@@ -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)) {