From: Michael D. Lowis Date: Tue, 18 Jul 2017 18:16:54 +0000 (-0400) Subject: Adapted middle click functionality to perform a send when pty is active X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=a78706372fe021822dd2c5244fce72c2e165b5a6;p=projs%2Ftide.git Adapted middle click functionality to perform a send when pty is active --- diff --git a/inc/edit.h b/inc/edit.h index d37e0df..b293367 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -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); diff --git a/lib/pty.c b/lib/pty.c index 1a2ae27..73e3864 100644 --- 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 10f1ec0..325fac4 100644 --- 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) {