]> git.mdlowis.com Git - projs/tide.git/commitdiff
Implemented backspace and delete with view_delete and enums for direction. Scaffoldin...
authorMichael D. Lowis <mike@mdlowis.com>
Wed, 30 Nov 2016 00:55:07 +0000 (19:55 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Wed, 30 Nov 2016 00:55:07 +0000 (19:55 -0500)
inc/edit.h
libedit/view.c
xedit.c

index e9ca6d7ac1476688e2b6d11e8c5a64fee435a586..30f765c283fb244d7a82eeb8e969744853bdc36f 100644 (file)
@@ -146,6 +146,13 @@ typedef struct {
     Sel selection;    /* range of currently selected text */
 } View;
 
+enum {
+    LEFT  = -1,
+    RIGHT = +1,
+    UP    = -1,
+    DOWN  = +1
+};
+
 void view_init(View* view, char* file);
 size_t view_limitrows(View* view, size_t maxrows, size_t ncols);
 void view_resize(View* view, size_t nrows, size_t ncols);
@@ -162,7 +169,7 @@ char* view_fetch(View* view, size_t row, size_t col);
 void view_find(View* view, size_t row, size_t col);
 void view_findstr(View* view, char* str);
 void view_insert(View* view, Rune rune);
-void view_delete(View* view);
+void view_delete(View* view, int dir, bool byword);
 void view_bol(View* view, bool extsel);
 void view_eol(View* view, bool extsel);
 void view_undo(View* view);
index dd6b2b02b17740f059d22e45469c2736be409963..a8cf6757257043d86fd3e498e5956b0d4b04b5cd 100644 (file)
@@ -358,36 +358,32 @@ void view_findstr(View* view, char* str) {
 }
 
 void view_insert(View* view, Rune rune) {
-    if (rune == '\b') {
-        if (num_selected(view->selection))
-            view_delete(view);
-        else if (view->selection.end > 0)
-            buf_del(&(view->buffer), --view->selection.end);
-    } else {
-        /* ignore non-printable control characters */
-        if (!isspace(rune) && rune < 0x20)
-            return;
-        if (num_selected(view->selection))
-            view_delete(view);
-        buf_ins(&(view->buffer), view->selection.end++, rune);
-    }
+    /* ignore non-printable control characters */
+    if (!isspace(rune) && rune < 0x20)
+        return;
+    if (num_selected(view->selection))
+        view_delete(view, RIGHT, false);
+    buf_ins(&(view->buffer), view->selection.end++, rune);
     view->selection.beg = view->selection.end;
     view->selection.col = buf_getcol(&(view->buffer), view->selection.end);
     view->sync_needed = true;
 }
 
-void view_delete(View* view) {
+void view_delete(View* view, int dir, bool byword) {
     Sel sel = view->selection;
     selswap(&sel);
-    size_t num = num_selected(view->selection);
-    if (num == 0) {
-        if (sel.end < buf_end(&(view->buffer)))
-            buf_del(&(view->buffer), sel.end);
-    } else {
+    size_t num = num_selected(sel);
+    if (num != 0) {
         for (size_t i = 0; i < num; i++)
             buf_del(&(view->buffer), sel.beg);
+        sel.end = sel.beg;
+    } else {
+        if ((dir == LEFT) && (sel.end > 0))
+            buf_del(&(view->buffer), --sel.end);
+        else if ((dir == RIGHT) && (sel.end < buf_end(&(view->buffer))))
+            buf_del(&(view->buffer), sel.end);
     }
-    view->selection.beg = view->selection.end = sel.beg;
+    view->selection.beg = view->selection.end = sel.end;
     view->selection.col = buf_getcol(&(view->buffer), view->selection.end);
     view->sync_needed = true;
 }
diff --git a/xedit.c b/xedit.c
index 6f402e5082aa6e761096bdb3e84d08938e5ae6fe..31afa7d773425c00af4c9e38214e16cc6fc4b084 100644 (file)
--- a/xedit.c
+++ b/xedit.c
@@ -26,6 +26,7 @@ static void redraw(int width, int height);
 
 // UI Callbacks
 static void delete(void);
+static void backspace(void);
 static void cursor_home(void);
 static void cursor_end(void);
 static void cursor_up(void);
@@ -138,7 +139,7 @@ static KeyBinding Bindings[] = {
     { ModNone, KEY_PGUP,      page_up       },
     { ModNone, KEY_PGDN,      page_dn       },
     { ModAny,  KEY_DELETE,    delete        },
-    //{ ModAny,  KEY_BACKSPACE, backspace     },
+    { ModAny,  KEY_BACKSPACE, backspace     },
 
     /* Cursor Movements */
     { ModAny, KEY_HOME,  cursor_home  },
@@ -360,7 +361,13 @@ static void redraw(int width, int height) {
 /* UI Callbacks
  *****************************************************************************/
 static void delete(void) {
-    view_delete(currview());
+    bool byword = x11_keymodsset(ModCtrl);
+    view_delete(currview(), RIGHT, byword);
+}
+
+static void backspace(void) {
+    bool byword = x11_keymodsset(ModCtrl);
+    view_delete(currview(), LEFT, byword);
 }
 
 static void cursor_home(void) {
@@ -375,30 +382,30 @@ static void cursor_end(void) {
 
 static void cursor_up(void) {
     bool extsel = x11_keymodsset(ModShift);
-    view_byline(currview(), -1, extsel);
+    view_byline(currview(), UP, extsel);
 }
 
 static void cursor_dn(void) {
     bool extsel = x11_keymodsset(ModShift);
-    view_byline(currview(), +1, extsel);
+    view_byline(currview(), DOWN, extsel);
 }
 
 static void cursor_left(void) {
     bool extsel = x11_keymodsset(ModShift);
-    view_byrune(currview(), -1, extsel);
+    view_byrune(currview(), LEFT, extsel);
 }
 
 static void cursor_right(void) {
     bool extsel = x11_keymodsset(ModShift);
-    view_byrune(currview(), +1, true);
+    view_byrune(currview(), RIGHT, extsel);
 }
 
 static void page_up(void) {
-    view_scrollpage(currview(), -1);
+    view_scrollpage(currview(), UP);
 }
 
 static void page_dn(void) {
-    view_scrollpage(currview(), +1);
+    view_scrollpage(currview(), DOWN);
 }
 
 static void select_prev(void) {