]> git.mdlowis.com Git - projs/tide.git/commitdiff
removed selection parameters from buffer api. Always work with the internal selection
authorMichael D. Lowis <mike.lowis@gentex.com>
Wed, 4 Apr 2018 17:38:32 +0000 (13:38 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Wed, 4 Apr 2018 17:38:32 +0000 (13:38 -0400)
inc/edit.h
lib/buf.c
lib/view.c

index f24bbc981977b38f86bb26ce67c752f9a4640c3e..ee9edb19c387bca2344e7c94125254acd2b599b3 100644 (file)
@@ -45,17 +45,17 @@ void buf_save(Buf* buf);
 size_t buf_end(Buf* buf);
 
 int buf_getrat(Buf* buf, size_t off);
-void buf_putc(Buf* buf, int c, Sel* sel);
-void buf_puts(Buf* buf, char* s, Sel* sel);
-int buf_getc(Buf* buf, Sel* sel);
-char* buf_gets(Buf* buf, Sel* sel);
-void buf_del(Buf* buf, Sel* sel);
-
-void buf_undo(Buf* buf, Sel* sel);
-void buf_redo(Buf* buf, Sel* sel);
+void buf_putc(Buf* buf, int c);
+void buf_puts(Buf* buf, char* s);
+int buf_getc(Buf* buf);
+char* buf_gets(Buf* buf);
+void buf_del(Buf* buf);
+
+void buf_undo(Buf* buf);
+void buf_redo(Buf* buf);
 void buf_loglock(Buf* buf);
 void buf_logclear(Buf* buf);
-void buf_lastins(Buf* buf, Sel* p_sel);
+void buf_lastins(Buf* buf);
 
 bool buf_iseol(Buf* buf, size_t pos);
 size_t buf_bol(Buf* buf, size_t pos);
@@ -63,24 +63,24 @@ size_t buf_eol(Buf* buf, size_t pos);
 size_t buf_bow(Buf* buf, size_t pos);
 size_t buf_eow(Buf* buf, size_t pos);
 
-void buf_selline(Buf* buf, Sel* sel);
-void buf_selword(Buf* buf, bool (*isword)(Rune), Sel* sel);
-void buf_selblock(Buf* buf, Rune beg, Rune end, Sel* sel);
-void buf_selall(Buf* buf, Sel* sel);
-void buf_selctx(Buf* buf, bool (*isword)(Rune), Sel* p_sel);
+void buf_selline(Buf* buf);
+void buf_selword(Buf* buf, bool (*isword)(Rune));
+void buf_selblock(Buf* buf, Rune beg, Rune end);
+void buf_selall(Buf* buf);
+void buf_selctx(Buf* buf, bool (*isword)(Rune));
 
 size_t buf_byrune(Buf* buf, size_t pos, int count);
 size_t buf_byword(Buf* buf, size_t pos, int count);
 size_t buf_byline(Buf* buf, size_t pos, int count);
-bool buf_findstr(Buf* buf, Sel* sel, int dir, char* str);
+bool buf_findstr(Buf* buf, int dir, char* str);
 
-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);
+void buf_setln(Buf* buf, size_t line);
+void buf_getcol(Buf* buf);
+void buf_setcol(Buf* buf);
 
-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);
+size_t buf_selsz(Buf* buf);
+void buf_selclr(Buf* buf, int dir);
+bool buf_insel(Buf* buf, size_t off);
 
 /* Screen management functions
  *****************************************************************************/
index 130f336ec75eb25aa730c8f7fc16e19fd153f6f3..bbb8d381f45fa0b4887653aff95c90e8c0c11873 100644 (file)
--- a/lib/buf.c
+++ b/lib/buf.c
@@ -71,10 +71,6 @@ void buf_reload(Buf* buf) {
 
 void buf_save(Buf* buf) {
     if (0 == buf_end(buf)) return;
-    /* text files should  always end in a new line. If we detected a
-       binary file or at least a non-utf8 file, skip this part. */
-    if (!buf_iseol(buf, buf_end(buf)-1))
-        buf_putc(buf, '\n', &(Sel){ .end = buf_end(buf)-1 });
 
     char* wptr;
     long fd, nwrite = 0, towrite = 0;
@@ -148,10 +144,7 @@ static Sel getsel(Buf* buf, Sel* p_sel) {
 }
 
 static void setsel(Buf* buf, Sel* p_sel, Sel* p_newsel) {
-    if (p_sel)
-        *p_sel = *p_newsel;
-    else
-        buf->selection = *p_newsel;
+    buf->selection = *p_newsel;
 }
 
 static void putb(Buf* buf, char b, Sel* p_sel) {
@@ -177,26 +170,26 @@ int buf_getrat(Buf* buf, size_t off) {
     return rune;
 }
 
-void buf_putc(Buf* buf, int c, Sel* p_sel) {
+void buf_putc(Buf* buf, int c) {
     char utf8buf[UTF_MAX+1] = {0};
     (void)utf8encode(utf8buf, c);
-    buf_puts(buf, utf8buf, p_sel);
+    buf_puts(buf, utf8buf);
 }
 
-void buf_puts(Buf* buf, char* s, Sel* p_sel) {
-    buf_del(buf, p_sel);
-    Sel sel = getsel(buf, p_sel);
+void buf_puts(Buf* buf, char* s) {
+    buf_del(buf);
+    Sel sel = getsel(buf, NULL);
     while (s && *s) putb(buf, *(s++), &sel);
-    setsel(buf, p_sel, &sel);
+    setsel(buf, NULL, &sel);
 }
 
-int buf_getc(Buf* buf, Sel* p_sel) {
-    Sel sel = getsel(buf, p_sel);
+int buf_getc(Buf* buf) {
+    Sel sel = getsel(buf, NULL);
     return buf_getrat(buf, sel.end);
 }
 
-char* buf_gets(Buf* buf, Sel* p_sel) {
-    Sel sel = getsel(buf, p_sel);
+char* buf_gets(Buf* buf) {
+    Sel sel = getsel(buf, NULL);
     size_t nbytes = sel.end - sel.beg;
     char* str = malloc(nbytes+1);
     for (size_t i = 0; i < nbytes; i++)
@@ -205,15 +198,15 @@ char* buf_gets(Buf* buf, Sel* p_sel) {
     return str;
 }
 
-void buf_del(Buf* buf, Sel* p_sel) {
-    Sel sel = getsel(buf, p_sel);
+void buf_del(Buf* buf) {
+    Sel sel = getsel(buf, NULL);
     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, p_sel, &sel);
+        setsel(buf, NULL, &sel);
         // update log here
         // free(str);
     }
@@ -221,10 +214,10 @@ void buf_del(Buf* buf, Sel* p_sel) {
 
 /******************************************************************************/
 
-void buf_undo(Buf* buf, Sel* sel) {
+void buf_undo(Buf* buf) {
 }
 
-void buf_redo(Buf* buf, Sel* sel) {
+void buf_redo(Buf* buf) {
 }
 
 void buf_logclear(Buf* buf) {
@@ -232,10 +225,10 @@ void buf_logclear(Buf* buf) {
     log_clear(&(buf->undo));
 }
 
-void buf_lastins(Buf* buf, Sel* p_sel) {
-    Sel sel = getsel(buf, p_sel);
+void buf_lastins(Buf* buf) {
+    Sel sel = getsel(buf, NULL);
     // Set selection to last inserted text
-    setsel(buf, p_sel, &sel);
+    setsel(buf, NULL, &sel);
 }
 
 static void log_clear(Log** list) {
@@ -275,23 +268,23 @@ size_t buf_eow(Buf* buf, size_t off) {
     return off;
 }
 
-void buf_selline(Buf* buf, Sel* p_sel) {
-    Sel sel = getsel(buf, p_sel);
+void buf_selline(Buf* buf) {
+    Sel sel = getsel(buf, NULL);
     sel.beg = buf_bol(buf, sel.end);
     sel.end = buf_eol(buf, sel.end);
     sel.end = buf_byrune(buf, sel.end, RIGHT);
-    setsel(buf, p_sel, &sel);
+    setsel(buf, NULL, &sel);
 }
 
-void buf_selword(Buf* buf, bool (*isword)(Rune), Sel* p_sel) {
-    Sel sel = getsel(buf, p_sel);
+void buf_selword(Buf* buf, bool (*isword)(Rune)) {
+    Sel sel = getsel(buf, NULL);
     for (; isword(buf_getrat(buf, sel.beg-1)); sel.beg--);
     for (; isword(buf_getrat(buf, sel.end));   sel.end++);
-    setsel(buf, p_sel, &sel);
+    setsel(buf, NULL, &sel);
 }
 
-void buf_selblock(Buf* buf, Rune first, Rune last, Sel* p_sel) {
-    Sel sel = getsel(buf, p_sel);
+void buf_selblock(Buf* buf, Rune first, Rune last) {
+    Sel sel = getsel(buf, NULL);
     int balance = 0, dir;
     size_t beg, end = sel.end;
 
@@ -322,36 +315,34 @@ void buf_selblock(Buf* buf, Rune first, Rune last, Sel* p_sel) {
     /* update the passed in selection */
     if (end > beg) beg++; else end++;
     sel.beg = beg, sel.end = end;
-    setsel(buf, p_sel, &sel);
+    setsel(buf, NULL, &sel);
 }
 
-void buf_selall(Buf* buf, Sel* p_sel) {
-    Sel sel = getsel(buf, p_sel);
+void buf_selall(Buf* buf) {
+    Sel sel = getsel(buf, NULL);
     sel = (Sel){ .beg = 0, .end = buf_end(buf) };
-    setsel(buf, p_sel, &sel);
+    setsel(buf, NULL, &sel);
 }
 
-void buf_selctx(Buf* buf, bool (*isword)(Rune), Sel* p_sel) {
-    Sel sel = getsel(buf, p_sel);
+void buf_selctx(Buf* buf, bool (*isword)(Rune)) {
+    Sel sel = getsel(buf, NULL);
     size_t bol = buf_bol(buf, sel.end);
-    Rune r = buf_getc(buf, &sel);
+    Rune r = buf_getc(buf);
     if (r == '(' || r == ')')
-        buf_selblock(buf, '(', ')', &sel);
+        buf_selblock(buf, '(', ')');
     else if (r == '[' || r == ']')
-        buf_selblock(buf, '[', ']', &sel);
+        buf_selblock(buf, '[', ']');
     else if (r == '{' || r == '}')
-        buf_selblock(buf, '{', '}', &sel);
+        buf_selblock(buf, '{', '}');
     else if (sel.end == bol || r == '\n')
-        buf_selline(buf, &sel);
+        buf_selline(buf);
     else if (risword(r))
-        buf_selword(buf, isword, &sel);
+        buf_selword(buf, isword);
     else
-        buf_selword(buf, risbigword, &sel);
-    buf_getcol(buf, &sel);
-    setsel(buf, p_sel, &sel);
+        buf_selword(buf, risbigword);
+    buf_getcol(buf);
 }
 
-
 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
@@ -399,15 +390,17 @@ size_t buf_byline(Buf* buf, size_t pos, int count) {
     return pos;
 }
 
-bool buf_findstr(Buf* buf, Sel* sel, int dir, char* str) {
+bool buf_findstr(Buf* buf, int dir, char* str) {
     size_t len = strlen(str);
-    size_t start = sel->beg, mbeg = (start + dir), mend = (mbeg + len);
+    size_t start = buf->selection.beg,
+           mbeg = (start + dir),
+           mend = (mbeg + len);
     while (mbeg != start) {
         if ((getb(buf, mbeg) == str[0]) &&
             (getb(buf, mend-1) == str[len-1]) &&
             (0 == bytes_match(buf, mbeg, mend, str)))
         {
-            sel->beg = mbeg, sel->end = mend;
+            buf->selection.beg = mbeg, buf->selection.end = mend;
             return true;
         }
         mbeg += dir, mend += dir;
@@ -425,26 +418,26 @@ static int bytes_match(Buf* buf, size_t mbeg, size_t mend, char* str) {
     return 0;
 }
 
-void buf_setln(Buf* buf, Sel* sel, size_t line) {
+void buf_setln(Buf* buf, size_t line) {
     size_t curr = 0, end = buf_end(buf);
     while (line > 1 && curr < end) {
         size_t next = buf_byline(buf, curr, DOWN);
         if (curr == next) break;
         line--, curr = next;
     }
-    sel->beg = sel->end = curr;
+    buf->selection.beg = buf->selection.end = curr;
 }
 
-void buf_getcol(Buf* buf, Sel* p_sel) {
-    Sel sel = getsel(buf, p_sel);
+void buf_getcol(Buf* buf) {
+    Sel sel = 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, p_sel, &sel);
+    setsel(buf, NULL, &sel);
 }
 
-void buf_setcol(Buf* buf, Sel* p_sel) {
-    Sel sel = getsel(buf, p_sel);
+void buf_setcol(Buf* buf) {
+    Sel sel = 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 */
@@ -458,7 +451,7 @@ void buf_setcol(Buf* buf, Sel* p_sel) {
             break;
         i += width;
     }
-    setsel(buf, p_sel, &sel);
+    setsel(buf, NULL, &sel);
 }
 
 static Rune nextrune(Buf* buf, size_t off, int move, bool (*testfn)(Rune)) {
@@ -473,19 +466,19 @@ static Rune nextrune(Buf* buf, size_t off, int move, bool (*testfn)(Rune)) {
 
 /******************************************************************************/
 
-size_t buf_selsz(Buf* buf, Sel* p_sel) {
-    Sel sel = getsel(buf, p_sel);
+size_t buf_selsz(Buf* buf) {
+    Sel sel = getsel(buf, NULL);
     return sel.end - sel.beg;
 }
 
-void buf_selclr(Buf* buf, Sel* p_sel, int dir) {
-    Sel sel = getsel(buf, p_sel);
+void buf_selclr(Buf* buf, int dir) {
+    Sel sel = getsel(buf, NULL);
     if (dir > 0) sel.beg = sel.end;
     else sel.end = sel.beg;
-    setsel(buf, p_sel, &sel);
+    setsel(buf, NULL, &sel);
 }
 
-bool buf_insel(Buf* buf, Sel* p_sel, size_t off) {
-    Sel sel = getsel(buf, p_sel);
+bool buf_insel(Buf* buf, size_t off) {
+    Sel sel = getsel(buf, NULL);
     return (off >= sel.beg && off < sel.end);
 }
index 4f87f30aadcef195c2607f7de173bb3db865e56d..b6eff56224a90b066d0dc009cc59fd2cb62f5238 100644 (file)
@@ -116,23 +116,23 @@ void view_setcursor(View* view, size_t row, size_t col, bool extsel) {
 void view_selword(View* view, size_t row, size_t col) {
     if (row != SIZE_MAX && col != SIZE_MAX)
         view_setcursor(view, row, col, false);
-    buf_selword(&(view->buffer), risbigword, NULL);
+    buf_selword(&(view->buffer), risbigword);
 }
 
 void view_selprev(View* view) {
     if (!view_selsize(view))
-        buf_lastins(&(view->buffer), NULL);
+        buf_lastins(&(view->buffer));
     else
-        buf_selclr(&(view->buffer), NULL, RIGHT);
+        buf_selclr(&(view->buffer), RIGHT);
 }
 
 void view_select(View* view, size_t row, size_t col) {
     view_setcursor(view, row, col, false);
-    buf_selctx(&(view->buffer), risword, NULL);
+    buf_selctx(&(view->buffer), risword);
 }
 
 size_t view_selsize(View* view) {
-    return buf_selsz(&(view->buffer), NULL);
+    return buf_selsz(&(view->buffer));
 }
 
 char* view_fetch(View* view, size_t row, size_t col, bool (*isword)(Rune)) {
@@ -151,7 +151,7 @@ char* view_fetch(View* view, size_t row, size_t col, bool (*isword)(Rune)) {
 }
 
 bool view_findstr(View* view, int dir, char* str) {
-    bool found = buf_findstr(&(view->buffer), NULL, dir, str);
+    bool found = buf_findstr(&(view->buffer), dir, str);
     view->sync_needed = true;
     view->sync_center = true;
     return found;
@@ -161,14 +161,14 @@ void view_insert(View* view, bool indent, Rune rune) {
     /* ignore non-printable control characters */
     if (!isspace(rune) && (rune >= 0 && rune < 0x20))
         return;
-    buf_putc(&(view->buffer), rune, NULL);
+    buf_putc(&(view->buffer), rune);
     move_to(view, false, getsel(view)->end);
 }
 
 void view_delete(View* view, int dir, bool byword) {
     if (!view_selsize(view))
         (byword ? view_byword : view_byrune)(view, dir, true);
-    buf_del(&(view->buffer), NULL);
+    buf_del(&(view->buffer));
     move_to(view, false, getsel(view)->end);
 }
 
@@ -201,39 +201,39 @@ void view_eof(View* view, bool extsel) {
 }
 
 void view_setln(View* view, size_t line) {
-    buf_setln(&(view->buffer), NULL, line);
+    buf_setln(&(view->buffer), line);
     view->sync_center = true;
 }
 
 void view_undo(View* view) {
-    buf_undo(&(view->buffer), NULL);
+    buf_undo(&(view->buffer));
     view->sync_needed = true;
     view->sync_center = !selection_visible(view);
 }
 
 void view_redo(View* view) {
-    buf_redo(&(view->buffer), NULL);
+    buf_redo(&(view->buffer));
     view->sync_needed = true;
     view->sync_center = !selection_visible(view);
 }
 
 void view_putstr(View* view, char* str) {
-    buf_puts(&(view->buffer), str, NULL);
+    buf_puts(&(view->buffer), str);
 }
 
 char* view_getstr(View* view) {
-    return buf_gets(&(view->buffer), NULL);
+    return buf_gets(&(view->buffer));
 }
 
 char* view_getcmd(View* view) {
     if (!view_selsize(view))
-        buf_selctx(&(view->buffer), riscmd, NULL);
+        buf_selctx(&(view->buffer), riscmd);
     return view_getstr(view);
 }
 
 void view_selctx(View* view) {
     if (!view_selsize(view))
-        buf_selctx(&(view->buffer), risword, NULL);
+        buf_selctx(&(view->buffer), risword);
 }
 
 char* view_getctx(View* view) {
@@ -258,7 +258,7 @@ void view_scrollpage(View* view, int move) {
 }
 
 Rune view_getrune(View* view) {
-    return buf_getc(&(view->buffer), NULL);
+    return buf_getc(&(view->buffer));
 }
 
 void view_scrollto(View* view, size_t csr) {
@@ -277,29 +277,29 @@ void view_scrollto(View* view, size_t csr) {
 }
 
 void view_selectall(View* view) {
-    buf_selall(&(view->buffer), NULL);
+    buf_selall(&(view->buffer));
     view->sync_needed = true;
 }
 
 void view_selectobj(View* view, bool (*istype)(Rune)) {
-    buf_selword(&(view->buffer), istype, NULL);
+    buf_selword(&(view->buffer), istype);
     view->sync_needed = true;
 }
 
 static void move_selection(View* view, bool extsel, int move, movefn_t bything) {
     view->sync_needed = true;
-    if (buf_selsz(&(view->buffer), NULL) && !extsel) {
-        buf_selclr(&(view->buffer), NULL, move);
+    if (buf_selsz(&(view->buffer)) && !extsel) {
+        buf_selclr(&(view->buffer), move);
     } else {
         getsel(view)->end = bything(&(view->buffer), getsel(view)->end, move);
         if (bything == buf_byline)
-            buf_setcol(&(view->buffer), NULL);
+            buf_setcol(&(view->buffer));
         if (!extsel)
-            buf_selclr(&(view->buffer), NULL, move);
+            buf_selclr(&(view->buffer), move);
     }
     /* only update column if not moving vertically */
     if (bything != buf_byline)
-        buf_getcol(&(view->buffer), NULL);
+        buf_getcol(&(view->buffer));
 }
 
 static void move_to(View* view, bool extsel, size_t off) {
@@ -307,7 +307,7 @@ static void move_to(View* view, bool extsel, size_t off) {
     getsel(view)->end = (off > buf_end(buf) ? buf_end(buf) : off);
     if (!extsel)
         getsel(view)->beg = getsel(view)->end;
-    buf_getcol(buf, NULL);
+    buf_getcol(buf);
     view->sync_needed = true;
 }
 
@@ -373,7 +373,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 = (buf_insel(&(view->buffer), NULL, pos) ? view->clrsel : view->clrnor);
+        uint32_t attr = (buf_insel(&(view->buffer), 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)) {