]> git.mdlowis.com Git - projs/tide.git/commitdiff
fixed handling of reverse selections
authorMichael D. Lowis <mike@mdlowis.com>
Thu, 5 Apr 2018 00:45:25 +0000 (20:45 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Thu, 5 Apr 2018 00:45:25 +0000 (20:45 -0400)
lib/buf.c

index bbb8d381f45fa0b4887653aff95c90e8c0c11873..b8b65eeee5f5a07f6bb5ee6e2e1567773a07fa61 100644 (file)
--- a/lib/buf.c
+++ b/lib/buf.c
@@ -71,7 +71,6 @@ void buf_reload(Buf* buf) {
 
 void buf_save(Buf* buf) {
     if (0 == buf_end(buf)) return;
-
     char* wptr;
     long fd, nwrite = 0, towrite = 0;
     if (buf->path && (fd = open(buf->path, O_WRONLY|O_CREAT, 0644)) >= 0) {
@@ -135,18 +134,14 @@ static void buf_resize(Buf* buf, size_t sz) {
 
 /******************************************************************************/
 
-static Sel getsel(Buf* buf, Sel* p_sel) {
+static Sel getsel(Buf* buf) {
     size_t temp;
-    Sel sel = (p_sel ? *p_sel : buf->selection);
+    Sel sel = buf->selection;
     if (sel.end < sel.beg)
         temp = sel.beg, sel.beg = sel.end, sel.end = temp;
     return sel;
 }
 
-static void setsel(Buf* buf, Sel* p_sel, Sel* p_newsel) {
-    buf->selection = *p_newsel;
-}
-
 static void putb(Buf* buf, char b, Sel* p_sel) {
     syncgap(buf, p_sel->end);
     *(buf->gapstart++) = b;
@@ -178,18 +173,15 @@ void buf_putc(Buf* buf, int c) {
 
 void buf_puts(Buf* buf, char* s) {
     buf_del(buf);
-    Sel sel = getsel(buf, NULL);
-    while (s && *s) putb(buf, *(s++), &sel);
-    setsel(buf, NULL, &sel);
+    while (s && *s) putb(buf, *(s++), &(buf->selection));
 }
 
 int buf_getc(Buf* buf) {
-    Sel sel = getsel(buf, NULL);
-    return buf_getrat(buf, sel.end);
+    return buf_getrat(buf, buf->selection.end);
 }
 
 char* buf_gets(Buf* buf) {
-    Sel sel = getsel(buf, NULL);
+    Sel sel = getsel(buf);
     size_t nbytes = sel.end - sel.beg;
     char* str = malloc(nbytes+1);
     for (size_t i = 0; i < nbytes; i++)
@@ -199,14 +191,14 @@ char* buf_gets(Buf* buf) {
 }
 
 void buf_del(Buf* buf) {
-    Sel sel = getsel(buf, NULL);
+    Sel sel = getsel(buf);
     size_t nbytes = sel.end - sel.beg;
     if (nbytes > 0) {
         //char* str = buf_gets(buf, &sel);
         syncgap(buf, sel.beg);
         buf->gapend += nbytes;
         sel.end = sel.beg;
-        setsel(buf, NULL, &sel);
+        buf->selection = sel;
         // update log here
         // free(str);
     }
@@ -226,9 +218,9 @@ void buf_logclear(Buf* buf) {
 }
 
 void buf_lastins(Buf* buf) {
-    Sel sel = getsel(buf, NULL);
+    Sel sel = getsel(buf);
     // Set selection to last inserted text
-    setsel(buf, NULL, &sel);
+    buf->selection = sel;
 }
 
 static void log_clear(Log** list) {
@@ -269,22 +261,22 @@ size_t buf_eow(Buf* buf, size_t off) {
 }
 
 void buf_selline(Buf* buf) {
-    Sel sel = getsel(buf, NULL);
+    Sel sel = getsel(buf);
     sel.beg = buf_bol(buf, sel.end);
     sel.end = buf_eol(buf, sel.end);
     sel.end = buf_byrune(buf, sel.end, RIGHT);
-    setsel(buf, NULL, &sel);
+    buf->selection = sel;
 }
 
 void buf_selword(Buf* buf, bool (*isword)(Rune)) {
-    Sel sel = getsel(buf, NULL);
+    Sel sel = getsel(buf);
     for (; isword(buf_getrat(buf, sel.beg-1)); sel.beg--);
     for (; isword(buf_getrat(buf, sel.end));   sel.end++);
-    setsel(buf, NULL, &sel);
+    buf->selection = sel;
 }
 
 void buf_selblock(Buf* buf, Rune first, Rune last) {
-    Sel sel = getsel(buf, NULL);
+    Sel sel = getsel(buf);
     int balance = 0, dir;
     size_t beg, end = sel.end;
 
@@ -314,19 +306,15 @@ void buf_selblock(Buf* buf, Rune first, Rune last) {
 
     /* update the passed in selection */
     if (end > beg) beg++; else end++;
-    sel.beg = beg, sel.end = end;
-    setsel(buf, NULL, &sel);
+    buf->selection.beg = beg, buf->selection.end = end;
 }
 
 void buf_selall(Buf* buf) {
-    Sel sel = getsel(buf, NULL);
-    sel = (Sel){ .beg = 0, .end = buf_end(buf) };
-    setsel(buf, NULL, &sel);
+    buf->selection = (Sel){ .beg = 0, .end = buf_end(buf) };
 }
 
 void buf_selctx(Buf* buf, bool (*isword)(Rune)) {
-    Sel sel = getsel(buf, NULL);
-    size_t bol = buf_bol(buf, sel.end);
+    size_t bol = buf_bol(buf, buf->selection.end);
     Rune r = buf_getc(buf);
     if (r == '(' || r == ')')
         buf_selblock(buf, '(', ')');
@@ -334,7 +322,7 @@ void buf_selctx(Buf* buf, bool (*isword)(Rune)) {
         buf_selblock(buf, '[', ']');
     else if (r == '{' || r == '}')
         buf_selblock(buf, '{', '}');
-    else if (sel.end == bol || r == '\n')
+    else if (buf->selection.end == bol || r == '\n')
         buf_selline(buf);
     else if (risword(r))
         buf_selword(buf, isword);
@@ -346,13 +334,9 @@ void buf_selctx(Buf* buf, bool (*isword)(Rune)) {
 size_t buf_byrune(Buf* buf, size_t pos, int count) {
     int move = (count < 0 ? -1 : 1);
     count *= move; // remove the sign if there is one
-    for (; count > 0; count--) {
-        if (move < 0) {
-            if (pos > 0) pos--;
-        } else {
-            if (pos < buf_end(buf)) pos++;
-        }
-    }
+    for (; count > 0; count--)
+        if (pos > 0 || pos < buf_end(buf))
+            pos += move;
     return pos;
 }
 
@@ -380,9 +364,9 @@ size_t buf_byline(Buf* buf, size_t pos, int count) {
     for (; count > 0; count--) {
         if (move < 0) {
             if (pos > buf_eol(buf, 0))
-                pos = buf_bol(buf, buf_bol(buf, pos)-1);
+                pos = buf_bol(buf, (buf_bol(buf, pos) - 1));
         } else {
-            size_t next = buf_eol(buf, pos)+1;
+            size_t next = (buf_eol(buf, pos) + 1);
             if (next < buf_end(buf))
                 pos = next;
         }
@@ -429,15 +413,15 @@ void buf_setln(Buf* buf, size_t line) {
 }
 
 void buf_getcol(Buf* buf) {
-    Sel sel = getsel(buf, NULL);
+    Sel sel = buf->selection; //getsel(buf, NULL);
     size_t pos = sel.end, curr = buf_bol(buf, pos);
     for (sel.col = 0; curr < pos; curr = buf_byrune(buf, curr, 1))
         sel.col += runewidth(sel.col, buf_getrat(buf, curr));
-    setsel(buf, NULL, &sel);
+    buf->selection = sel;
 }
 
 void buf_setcol(Buf* buf) {
-    Sel sel = getsel(buf, NULL);
+    Sel sel = buf->selection; //getsel(buf, NULL);
     size_t bol = buf_bol(buf, sel.end);
     size_t curr = bol, len = 0, i = 0;
     /* determine the length of the line in columns */
@@ -451,7 +435,7 @@ void buf_setcol(Buf* buf) {
             break;
         i += width;
     }
-    setsel(buf, NULL, &sel);
+    buf->selection = sel;
 }
 
 static Rune nextrune(Buf* buf, size_t off, int move, bool (*testfn)(Rune)) {
@@ -467,18 +451,18 @@ static Rune nextrune(Buf* buf, size_t off, int move, bool (*testfn)(Rune)) {
 /******************************************************************************/
 
 size_t buf_selsz(Buf* buf) {
-    Sel sel = getsel(buf, NULL);
+    Sel sel = getsel(buf);
     return sel.end - sel.beg;
 }
 
 void buf_selclr(Buf* buf, int dir) {
-    Sel sel = getsel(buf, NULL);
+    Sel sel = getsel(buf);
     if (dir > 0) sel.beg = sel.end;
     else sel.end = sel.beg;
-    setsel(buf, NULL, &sel);
+    buf->selection = sel;
 }
 
 bool buf_insel(Buf* buf, size_t off) {
-    Sel sel = getsel(buf, NULL);
+    Sel sel = getsel(buf);
     return (off >= sel.beg && off < sel.end);
 }