]> git.mdlowis.com Git - projs/tide.git/commitdiff
Adapted middle click functionality to perform a send when pty is active
authorMichael D. Lowis <mike.lowis@gentex.com>
Tue, 18 Jul 2017 18:16:54 +0000 (14:16 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Tue, 18 Jul 2017 18:16:54 +0000 (14:16 -0400)
inc/edit.h
lib/pty.c
tide.c

index d37e0df4942e8c7f946182f47da0b15be77afd18..b293367a34fe48124a7c87cc2fc36dbe754692fb 100644 (file)
@@ -233,6 +233,7 @@ int exec_spawn(char** cmd, int* in, int* out);
 bool pty_active(void);
 void pty_spawn(char** argv);
 void pty_send(char* cmd);
+void pty_send_rune(Rune rune);
 void pty_send_intr(void);
 void pty_send_eof(void);
 void pty_send_susp(void);
index 1a2ae27ac33fb79d717aa2e63168cd3d15f3c907..73e38648b4c0c627e3db52d7b807c1045786fbcd 100644 (file)
--- a/lib/pty.c
+++ b/lib/pty.c
@@ -51,8 +51,30 @@ void pty_spawn(char** argv) {
 }
 
 void pty_send(char* str) {
-    if (write(PtyFD, str, strlen(str)-1) < 0)
-        PtyFD = -1;
+    if (!str) return;
+    view_eof(win_view(EDIT), false);
+    bool has_eol = (str[strlen(str)-1] == '\n');
+    while (*str) {
+        Rune rune = 0;
+        size_t length = 0;
+        while (!utf8decode(&rune, &length, *str++));
+        pty_send_rune(rune);
+    }
+    if (!has_eol)
+        pty_send_rune('\n');
+}
+
+void pty_send_rune(Rune rune) {
+    view_insert(win_view(EDIT), false, rune);
+    size_t point = win_buf(EDIT)->outpoint;
+    size_t pos   = win_view(EDIT)->selection.end;
+    if ((rune == '\n' || rune == RUNE_CRLF) && pos > point) {
+        Sel range = { .beg = point, .end = pos };
+        char* str = view_getstr(win_view(EDIT), &range);
+        if (write(PtyFD, str, strlen(str)-1) < 0)
+            PtyFD = -1;
+        free(str);
+    }
 }
 
 static void update(int fd, void* data) {
diff --git a/tide.c b/tide.c
index 10f1ec0c77a52bd9f0059641ca1a0968c9dbe9be..325fac4ddb2bf441723b18111a20cd756a5d1aeb 100644 (file)
--- a/tide.c
+++ b/tide.c
@@ -93,6 +93,8 @@ static void exec(char* cmd) {
     if (tag) {
         while (*cmd && !isspace(*cmd++));
         tag_exec(tag, (*cmd ? stringdup(cmd) : NULL));
+    } else if (pty_active()) {
+        pty_send(cmd);
     } else {
         cmd_exec(cmd);
     }
@@ -415,6 +417,7 @@ static Tag Builtins[] = {
     { .tag = "Reload",    .action.noarg = reload    },
     { .tag = "Save",      .action.noarg = save      },
     { .tag = "SaveAs",    .action.arg   = saveas    },
+    { .tag = "Send",      .action.arg   = pty_send  },
     { .tag = "Tabs",      .action.noarg = tabs      },
     { .tag = "Undo",      .action.noarg = tag_undo  },
     { .tag = "LineNums",  .action.noarg = tag_lnnum },
@@ -546,17 +549,10 @@ bool update_needed(void) {
 }
 
 static void oninput(Rune rune) {
-    view_insert(win_view(FOCUSED), !pty_active(), rune);
-    if (win_getregion() == EDIT) {
-        size_t point = win_buf(EDIT)->outpoint;
-        size_t pos   = win_view(EDIT)->selection.end;
-        if ((rune == '\n' || rune == RUNE_CRLF) && pos > point) {
-            Sel range = { .beg = point, .end = pos };
-            char* str = view_getstr(win_view(EDIT), &range);
-            pty_send(str);
-            free(str);
-        }
-    }
+    if (win_getregion() == EDIT && pty_active())
+        pty_send_rune(rune);
+    else
+        view_insert(win_view(FOCUSED), true, rune);
 }
 
 void edit_relative(char* path) {