From 65a9efd4fb6f5d33f8b557d78ac84f88f70480e5 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Mon, 18 Nov 2019 22:24:27 -0500 Subject: [PATCH] fetch now tracks launch commands and continues if a ruleset does not contain a launch command. This allows rule files to set vars to be used by rules that follow. Additionally, this commit has a first pass at bidirectional shell comm with tracking of output lcoation, input range, and selection. It's buggy still, but it's a start towards an editable terminal like 9term --- inc/edit.h | 1 + src/fetch.c | 5 ++++- src/lib/buf.c | 27 +++++++++++++++++++++--- src/lib/xpty.c | 57 +++++++++++++++++++++++++++++++++++++++++--------- src/tide.c | 13 ++++++++++-- 5 files changed, 87 insertions(+), 16 deletions(-) diff --git a/inc/edit.h b/inc/edit.h index 41d1447..1c61f54 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -40,6 +40,7 @@ typedef struct { GapBuf contents; /* underlying sequence data structure */ EditLog log; /* underlying log of edit operations */ Sel selection; /* the currently selected text */ + Sel point; /* tracks external command I/O */ } Buf; enum { diff --git a/src/fetch.c b/src/fetch.c index e10f915..12285b7 100644 --- a/src/fetch.c +++ b/src/fetch.c @@ -17,6 +17,7 @@ typedef struct { char* Matches[10]; Rule*** Rulesets = NULL; size_t NumRulesets = 0; +bool Launched = false; static Rule*** BuiltinRules = (Rule**[]){ (Rule*[]){ /* Match URLS and open them with the browser */ @@ -227,6 +228,7 @@ static bool launch(char* cmd) int pid = fork(); if (pid > 0) { + Launched = true; ret = true; } else if (pid == 0) @@ -453,6 +455,7 @@ static void parse_rules(char* path) static void check_ruleset(Rule** rule) { + Launched = false; for (; (*rule)->type != COMPLETE; rule++) { telem_send(" RULE(%s '%s' '%s')\n", type2str((*rule)->type), (*rule)->arg1, (*rule)->arg2); @@ -462,7 +465,7 @@ static void check_ruleset(Rule** rule) } } - if ((*rule)->type == COMPLETE) + if (Launched && (*rule)->type == COMPLETE) { telem_send(" ACCEPT\n"); exit(0); diff --git a/src/lib/buf.c b/src/lib/buf.c index ca4d0ec..232bfe1 100644 --- a/src/lib/buf.c +++ b/src/lib/buf.c @@ -86,18 +86,31 @@ static void selset(Buf* buf, Sel sel) getcol(buf); } +static void putb(Buf* buf, char b, Sel* p_sel) +{ + if (p_sel->end < buf->point.beg) + { + buf->point.beg++; + } + if (p_sel->end <= buf->point.end) + { + buf->point.end++; + } + gapbuf_putb(&buf->contents, b, p_sel); +} + static void putch(Buf* buf, char b, Sel* p_sel) { if (b != '\r') { if (b == '\n' && DosLineFeed) { - gapbuf_putb(&buf->contents, '\r', p_sel); - gapbuf_putb(&buf->contents, '\n', p_sel); + putb(buf, '\r', p_sel); + putb(buf, '\n', p_sel); } else { - gapbuf_putb(&buf->contents, b, p_sel); + putb(buf, b, p_sel); } buf->status = MODIFIED; } @@ -353,6 +366,14 @@ void buf_del(Buf* buf) char* str = buf_gets(buf); gapbuf_del(&buf->contents, sel.beg, nbytes); sel.end = sel.beg = (sel.beg < sel.end ? sel.beg : sel.end); + if (sel.end <= buf->point.beg) + { + buf->point.beg -= (buf->point.beg - sel.end); + } + if (sel.end <= buf->point.end) + { + buf->point.end -= (buf->point.end - sel.end); + } buf->selection = sel; editlog_add(buf, sel.beg, sel.end, str); } diff --git a/src/lib/xpty.c b/src/lib/xpty.c index f729ac0..23b07e9 100644 --- a/src/lib/xpty.c +++ b/src/lib/xpty.c @@ -18,22 +18,49 @@ static void xpty_read(Job* job) if (nread <= 0) { job->readfn = NULL; - buf_logstop(&EditView->buffer); Pty_Fd = -1; EditView = NULL; } else if (nread > 0) { +// &while [ 1 ]; do sleep 1; echo foo; done buffer[nread] = '\0'; - buf_logstart(&EditView->buffer); + +// printf("B1 - E: %lu P: %lu-%lu S: %lu-%lu\n", +// buf_end(&(EditView->buffer)), +// EditView->buffer.point.beg, EditView->buffer.point.end, +// EditView->buffer.selection.beg, EditView->buffer.selection.end); + + size_t start = EditView->buffer.point.beg; + Sel sel = EditView->buffer.selection; + EditView->buffer.selection.beg = EditView->buffer.point.beg; + EditView->buffer.selection.end = EditView->buffer.point.beg; + EditView->buffer.point = sel; + +// printf("B2 - E: %lu P: %lu-%lu S: %lu-%lu\n", +// buf_end(&(EditView->buffer)), +// EditView->buffer.point.beg, EditView->buffer.point.end, +// EditView->buffer.selection.beg, EditView->buffer.selection.end); + view_putstr(EditView, buffer); - } -} -static void xpty_write(Job* job) -{ - job->writefn = NULL; - shutdown(job->fd, SHUT_WR); + if (start <= sel.beg) + { + sel.beg += nread-1; + } + if (start <= sel.end) + { + sel.end += nread-1; + } + Sel point = EditView->buffer.selection; + EditView->buffer.selection = sel; + EditView->buffer.point = point; + +// printf("A - E: %lu P: %lu-%lu S: %lu-%lu\n", +// buf_end(&(EditView->buffer)), +// EditView->buffer.point.beg, EditView->buffer.point.end, +// EditView->buffer.selection.beg, EditView->buffer.selection.end); + } } bool xpty_active(void) @@ -60,7 +87,16 @@ int xpty_run(View* view, char** cmd) { Pty_Fd = fd; EditView = view; - job_spawn(Pty_Fd, xpty_read, xpty_write, NULL); + if (view_selsize(view)) + { +// puts("delete"); + view_delete(view, LEFT, false); + } +// printf("S - E: %lu P: %lu-%lu S: %lu-%lu\n", +// buf_end(&(EditView->buffer)), +// EditView->buffer.point.beg, EditView->buffer.point.end, +// EditView->buffer.selection.beg, EditView->buffer.selection.end); + job_spawn(Pty_Fd, xpty_read, NULL, NULL); } } return fd; @@ -68,5 +104,6 @@ int xpty_run(View* view, char** cmd) void xpty_send(uint32_t key) { - (void)key; + view_insert(EditView, key); +// printf("%lu-%lu\n", EditView->buffer.point.beg, EditView->buffer.point.end); } diff --git a/src/tide.c b/src/tide.c index 3148f70..5d3c9e7 100644 --- a/src/tide.c +++ b/src/tide.c @@ -141,7 +141,16 @@ static void xkeypress(XConf* x, XEvent* e) uint32_t key = x11_process_key(x, e, Bindings); telem_send("KEY(reg: %d, key: 0x%x)\n", Focused, key); if (key != RUNE_ERR) - view_insert(win_view(FOCUSED), key); + { + if (Focused == EDIT && xpty_active()) + { + xpty_send(key); + } + else + { + view_insert(win_view(FOCUSED), key); + } + } } static void xmousebtn(XConf* x, XEvent* e) @@ -445,7 +454,7 @@ static void cmd_exec(char* cmd) execcmd[2] = cmd; /* get the selection that the command will operate on */ - if (op && op != '<' && op != '!' && !view_selsize(win_view(EDIT))) + if (op && op != '<' && op != '!' && op != '&' && !view_selsize(win_view(EDIT))) { view_selectall(win_view(EDIT)); } -- 2.51.0