]> git.mdlowis.com Git - projs/tide.git/commitdiff
Added cut, copy, and paste shortcuts
authorMichael D. Lowis <mike@mdlowis.com>
Fri, 18 Nov 2016 00:58:42 +0000 (19:58 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Fri, 18 Nov 2016 00:58:42 +0000 (19:58 -0500)
inc/edit.h
libedit/buf.c
libedit/view.c
xedit.c

index ce4d7ba341595058cb7af31b7dad44c0cc8a9258..55f0b3f272c6f78b1351d7e881adbdcf94cdec1d 100644 (file)
@@ -159,12 +159,8 @@ void view_bol(View* view);
 void view_eol(View* view);
 void view_undo(View* view);
 void view_redo(View* view);
-
-//size_t view_getoff(View* view, size_t pos, size_t row, size_t col);
-//void view_getsize(View* view, size_t* nrows, size_t* ncols);
-//void view_clearrow(View* view, size_t row);
-//size_t view_setcell(View* view, size_t row, size_t col, uint32_t attr, Rune r);
-//UGlyph* view_getglyph(View* view, size_t row, size_t col, size_t* scrwidth);
+void view_putstr(View* view, char* str);
+char* view_getstr(View* view, Sel* sel);
 
 /* Command Executions
  *****************************************************************************/
@@ -264,4 +260,4 @@ enum {
         0xff2aa198,   \
         0xff859900    \
     }
-#define DEFAULT_TAGS "Quit Save Cut Copy Paste | Find \n"
+#define DEFAULT_TAGS "Quit Save Cut Copy Paste | Find "
index 7fd22fd2a50b00de2e450803df7a06829b22493b..96d024b88a3e7b2b276bfac21061fbb8bba3c26c 100644 (file)
@@ -334,39 +334,39 @@ unsigned buf_setcol(Buf* buf, unsigned pos, unsigned col) {
     return curr;
 }
 
-char* buf_getstr(Buf* buf, unsigned beg, unsigned end) {
-    char utf[UTF_MAX] = {0};
-    size_t len = 0;
-    char*  str = NULL;
-    for (; beg <= end; beg++) {
-        Rune rune = buf_get(buf, beg);
-        if (rune == RUNE_CRLF) {
-            str = realloc(str, len + 2);
-            str[len + 1] = '\r';
-            str[len + 2] = '\n';
-            len += 2;
-        } else {
-            size_t n = utf8encode(utf, rune);
-            str = realloc(str, len + n);
-            memcpy(str+len, utf, n);
-            len += n;
-        }
-    }
-    str = realloc(str, len+1);
-    if (str) str[len] = '\0';
-    return str;
-}
-
-unsigned buf_putstr(Buf* buf, unsigned beg, unsigned end, char* str) {
-    /* delete the selected text first */
-    for (unsigned i = beg; ((end-beg) > 1) && (i <= end); i++)
-        buf_del(buf, beg);
-    /* insert the text */
-    while (*str) {
-        Rune rune = 0;
-        size_t length = 0;
-        while (!utf8decode(&rune, &length, *str++));
-        buf_ins(buf, beg++, rune);
-    }
-    return beg;
-}
+//char* buf_getstr(Buf* buf, unsigned beg, unsigned end) {
+//    char utf[UTF_MAX] = {0};
+//    size_t len = 0;
+//    char*  str = NULL;
+//    for (; beg <= end; beg++) {
+//        Rune rune = buf_get(buf, beg);
+//        if (rune == RUNE_CRLF) {
+//            str = realloc(str, len + 2);
+//            str[len + 1] = '\r';
+//            str[len + 2] = '\n';
+//            len += 2;
+//        } else {
+//            size_t n = utf8encode(utf, rune);
+//            str = realloc(str, len + n);
+//            memcpy(str+len, utf, n);
+//            len += n;
+//        }
+//    }
+//    str = realloc(str, len+1);
+//    if (str) str[len] = '\0';
+//    return str;
+//}
+//
+//unsigned buf_putstr(Buf* buf, unsigned beg, unsigned end, char* str) {
+//    /* delete the selected text first */
+//    for (unsigned i = beg; ((end-beg) > 1) && (i <= end); i++)
+//        buf_del(buf, beg);
+//    /* insert the text */
+//    while (*str) {
+//        Rune rune = 0;
+//        size_t length = 0;
+//        while (!utf8decode(&rune, &length, *str++));
+//        buf_ins(buf, beg++, rune);
+//    }
+//    return beg;
+//}
index bd026f3c35c6807abd71a37584ed7da9419efac5..d2cf1a8f8cd1c6f928a6a6bfff5a44667c6d67e1 100644 (file)
@@ -149,7 +149,7 @@ static size_t getoffset(View* view, size_t row, size_t col) {
                 pos++;
     }
     if (pos >= buf_end(&(view->buffer)))
-        return buf_end(&(view->buffer))-1;
+        return buf_end(&(view->buffer));
     return pos;
 }
 
@@ -226,9 +226,11 @@ void view_byline(View* view, int move) {
 
 void view_setcursor(View* view, size_t row, size_t col) {
     size_t off = getoffset(view, row, col);
-    view->selection.beg = view->selection.end = off;
-    view->selection.col = buf_getcol(&(view->buffer), view->selection.end);
-    sync_view(view, view->selection.end);
+    if (off != SIZE_MAX) {
+        view->selection.beg = view->selection.end = off;
+        view->selection.col = buf_getcol(&(view->buffer), view->selection.end);
+        view->sync_needed = true;
+    }
 }
 
 void view_selext(View* view, size_t row, size_t col) {
@@ -295,3 +297,40 @@ void view_redo(View* view) {
     view->selection.col = buf_getcol(&(view->buffer), view->selection.end);
     view->sync_needed = true;
 }
+
+void view_putstr(View* view, char* str) {
+    while (*str) {
+        Rune rune = 0;
+        size_t length = 0;
+        while (!utf8decode(&rune, &length, *str++));
+        view_insert(view, rune);
+    }
+}
+
+char* view_getstr(View* view, Sel* range) {
+    Buf* buf = &(view->buffer);
+    Sel sel = (range ? *range : view->selection);
+    selswap(&sel);
+    char utf[UTF_MAX] = {0};
+    size_t len = 0;
+    char*  str = NULL;
+
+    for (; sel.beg <= sel.end; sel.beg++) {
+        Rune rune = buf_get(buf, sel.beg);
+        if (rune == RUNE_CRLF) {
+            str = realloc(str, len + 2);
+            str[len + 1] = '\r';
+            str[len + 2] = '\n';
+            len += 2;
+        } else {
+            size_t n = utf8encode(utf, rune);
+            str = realloc(str, len + n);
+            memcpy(str+len, utf, n);
+            len += n;
+        }
+    }
+
+    str = realloc(str, len+1);
+    if (str) str[len] = '\0';
+    return str;
+}
diff --git a/xedit.c b/xedit.c
index 4b42a310734cdacd590397d8c8350f4aa7b9bed2..de36454f90c42a44f3ec7fb0c1966a5a0fee85d4 100644 (file)
--- a/xedit.c
+++ b/xedit.c
@@ -27,6 +27,16 @@ static XConfig Config = {
     .palette      = COLOR_PALETTE
 };
 
+/* External Commands
+ *****************************************************************************/
+#ifdef __MACH__
+static char* CopyCmd[]  = { "pbcopy", NULL };
+static char* PasteCmd[] = { "pbpaste", NULL };
+#else
+static char* CopyCmd[]  = { "xsel", "-bi", NULL };
+static char* PasteCmd[] = { "xsel", "-bo", NULL };
+#endif
+
 /* Region Utils
  *****************************************************************************/
 static View* getview(enum RegionId id) {
@@ -120,24 +130,24 @@ static void redo(void) {
     view_redo(currview());
 }
 
-//static void cut(void) {
-//    char* str = buf_getstr(&Buffer, SelBeg, SelEnd);
-//    cmdwrite(CopyCmd, str);
-//    free(str);
-//    delete();
-//}
-//
-//static void copy(void) {
-//    char* str = buf_getstr(&Buffer, SelBeg, SelEnd);
-//    cmdwrite(CopyCmd, str);
-//    free(str);
-//}
-//
-//static void paste(void) {
-//    char* str = cmdread(PasteCmd);
-//    buf_putstr(&Buffer, SelBeg, SelEnd, str);
-//    free(str);
-//}
+static void cut(void) {
+    char* str = view_getstr(currview(), NULL);
+    cmdwrite(CopyCmd, str);
+    free(str);
+    delete();
+}
+
+static void copy(void) {
+    char* str = view_getstr(currview(), NULL);
+    cmdwrite(CopyCmd, str);
+    free(str);
+}
+
+static void paste(void) {
+    char* str = cmdread(PasteCmd);
+    view_putstr(currview(), str);
+    free(str);
+}
 
 /* Mouse Handling
  *****************************************************************************/
@@ -225,22 +235,22 @@ static void mouse_handler(MouseAct act, MouseBtn btn, int x, int y) {
 /* Keyboard Bindings
  *****************************************************************************/
 static KeyBinding Insert[] = {
-    { KEY_DELETE,    delete        },
-    { KEY_UP,        cursor_up     },
-    { KEY_DOWN,      cursor_dn     },
-    { KEY_LEFT,      cursor_left   },
-    { KEY_RIGHT,     cursor_right  },
-    { KEY_HOME,      cursor_bol    },
-    { KEY_END,       cursor_eol    },
-    { KEY_CTRL_T,    change_focus  },
-    { KEY_CTRL_Q,    quit          },
-    { KEY_CTRL_S,    save          },
-    { KEY_CTRL_Z,    undo          },
-    { KEY_CTRL_Y,    redo          },
-    //{ KEY_CTRL_X,    cut           },
-    //{ KEY_CTRL_C,    copy          },
-    //{ KEY_CTRL_V,    paste         },
-    { 0,             NULL          }
+    { KEY_DELETE,    delete       },
+    { KEY_UP,        cursor_up    },
+    { KEY_DOWN,      cursor_dn    },
+    { KEY_LEFT,      cursor_left  },
+    { KEY_RIGHT,     cursor_right },
+    { KEY_HOME,      cursor_bol   },
+    { KEY_END,       cursor_eol   },
+    { KEY_CTRL_T,    change_focus },
+    { KEY_CTRL_Q,    quit         },
+    { KEY_CTRL_S,    save         },
+    { KEY_CTRL_Z,    undo         },
+    { KEY_CTRL_Y,    redo         },
+    { KEY_CTRL_X,    cut          },
+    { KEY_CTRL_C,    copy         },
+    { KEY_CTRL_V,    paste        },
+    { 0,             NULL         }
 };
 
 static void process_table(KeyBinding* bindings, Rune key) {
@@ -384,8 +394,8 @@ static void redraw(int width, int height) {
 int main(int argc, char** argv) {
     /* load the buffer views */
     view_init(getview(TAGS), NULL);
+    view_putstr(getview(TAGS), DEFAULT_TAGS);
     view_init(getview(EDIT), (argc > 1 ? argv[1] : NULL));
-    buf_putstr(getbuf(TAGS), 0, 0, DEFAULT_TAGS);
     /* initialize the display engine */
     x11_init(&Config);
     x11_window("edit", Width, Height);
@@ -396,28 +406,9 @@ int main(int argc, char** argv) {
 }
 
 #if 0
-/* External Commands
- *****************************************************************************/
-#ifdef __MACH__
-static char* CopyCmd[]  = { "pbcopy", NULL };
-static char* PasteCmd[] = { "pbpaste", NULL };
-#else
-static char* CopyCmd[]  = { "xsel", "-bi", NULL };
-static char* PasteCmd[] = { "xsel", "-bo", NULL };
-#endif
-
-/* Keyboard Actions
- *****************************************************************************/
-
-static void tagwin(void) {
-    TagWinExpanded = !TagWinExpanded;
-}
 
 /* Mouse Actions
  *****************************************************************************/
-void unused(int x, int y) {
-}
-
 void move_cursor(int x, int y) {
     if (y == 0) return;
     //SelBeg = SelEnd = screen_getoff(&Buffer, SelEnd, y-1, x);