This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch tymux-12-session-parity
in repository terminology.

View the commit online.

commit 0bc675a6da781df315bf73c0768dfe7b31c3ebc3
Author: Cedric BAIL <[email protected]>
AuthorDate: Sun Nov 22 23:17:46 2026 -0600

    tymux: never let a pasted Ctrl+b d detach
    
    The inline proxy looks for the detach chord in its raw input, so
    pasted text containing it detached the session. The proxy now keeps
    bracketed paste on in the outer terminal for as long as it can send
    input, whatever the application asked for, and does not look for the
    chord inside a paste. The markers only reach applications that asked
    for them.
    
    A typed Escape is never delayed for this. A paste that goes silent
    for 5 seconds ends, or a lost end marker would disable the chord for
    good. A paste cut by a hijack is closed or reopened on resume so that
    the application always sees a balanced pair, and stays open during
    the hijacker's turn, as a paused client cannot send input.
---
 DESIGN.md            |   2 +-
 man/tymux.1          |  29 ++++--
 src/bin/termsrv.c    | 287 +++++++++++++++++++++++++++++++++++++++++++++++++++
 src/bin/termsrv.h    |  43 ++++++++
 src/bin/tymux.c      | 136 +++++++++++++++++++++---
 src/bin/tytest.c     |   1 +
 src/bin/unit_tests.h |   1 +
 tests/meson.build    |   2 +-
 tests/tymux.py       | 245 ++++++++++++++++++++++++++++++++++++++++++-
 9 files changed, 718 insertions(+), 28 deletions(-)

diff --git a/DESIGN.md b/DESIGN.md
index dc22292b..6393449e 100644
--- a/DESIGN.md
+++ b/DESIGN.md
@@ -41,7 +41,7 @@ A simple documentation to navigate through the C files:
 * `src/bin/termptygfx.c`: charset translations
 * `src/bin/termptyops.c`: handling history
 * `src/bin/termptystatus.c`: program status escape codes (OSC 7501)
-* `src/bin/termsrv.c`: the tymux wire protocol shared by `tymuxd` and `tymux`: messages, screen shared memory, daemon spawning
+* `src/bin/termsrv.c`: the tymux wire protocol shared by `tymuxd` and `tymux`: messages, screen shared memory, daemon spawning, the filters of the typed bytes (detach chord, late hand-off reply, paste markers)
 * `src/bin/termtymux.c`: shows a `tymuxd` session in a terminal, through a shadow `Termpty` fed from the session's screen
 * `src/bin/tyalpha.c`: the `tyalpha` tool
 * `src/bin/tybenchd.c`: the `tybenchd` tool, benchmarks the throughput of `tymuxd`
diff --git a/man/tymux.1 b/man/tymux.1
index 37445422..2eafdf6f 100644
--- a/man/tymux.1
+++ b/man/tymux.1
@@ -37,8 +37,16 @@ natively (see \fBNATIVE HAND\-OFF\fP) dims it instead.
 The inline proxy asks the terminal it runs in for what the program in the
 session asks for: application cursor keys and keypad, mouse reporting,
 bracketed paste and focus reporting.
-It switches them off again when it detaches, exits or is paused. A read-only
-attach switches none of them on.
+It switches them off again when it detaches, exits or is paused, except
+bracketed paste while paused. A read-only attach switches none of them on, and
+the proxy looks for the detach chord in what it reads, pasted or not.
+Otherwise the terminal is always asked to bracket what is pasted, also while
+paused, and the proxy only forwards the markers when the program asked for
+them, so that a pasted
+.B Ctrl+b d
+never detaches, unless nothing is read for 5 seconds inside a paste: the
+rest is then taken for typed text, so that a paste whose end marker was lost
+does not disable the chord for good.
 .PP
 The size of the terminal in control is the size of the shell's screen.
 .PP
@@ -97,6 +105,8 @@ Stop \fINAME\fP: its shell is hung up and every attached terminal detached.
 .B Ctrl+b d
 Detach from the shell, which keeps running.
 Ctrl+b followed by any other key sends both keys to the shell.
+Text pasted in a terminal that brackets pastes is not scanned for the
+chord, unless the paste is silent for 5 seconds (see \fBBUGS\fP).
 .SH EXIT STATUS
 When the shell exits,
 .B tymux
@@ -222,11 +232,16 @@ configuration when the session started. The
 inline proxy still has no scrollback of its own.
 .PP
 When proxied (not natively handed off), the attached terminal is also
-only redrawn from the shell's screen, and mouse reporting, bracketed
-paste, application cursor keys and focus reporting are not forwarded to
-it; a native hand-off does not have this limitation, since it runs the
-session through Terminology's normal terminal emulation and input
-encoding.
+only redrawn from the shell's screen; a native hand-off runs the session
+through Terminology's normal terminal emulation and input encoding.
+A paste is only recognized by the proxy when the terminal it runs in sends
+the bracketed paste markers; in one that does not, a pasted
+.B Ctrl+b d
+detaches.
+The daemon ignores the input of a paused terminal, so a paste that a pause
+cuts is not seen whole by the program: on resume the proxy gives it the start
+marker it lacks, if the paste goes on, or the end marker it lacks, if the paste
+ended while paused, as long as the program still asks for bracketed paste.
 .PP
 Color scheme changes (OSC 4, 10, 11 and 12) are ignored in a session in
 both modes, and desktop notifications (OSC 99 and 777) are not forwarded,
diff --git a/src/bin/termsrv.c b/src/bin/termsrv.c
index c6059a00..1a21c11a 100644
--- a/src/bin/termsrv.c
+++ b/src/bin/termsrv.c
@@ -864,6 +864,129 @@ termsrv_chord_flush(TermSrvChord *chord, Eina_Binbuf *out)
    return EINA_TRUE;
 }
 
+#define PASTE_MARKER_LEN 6
+static const unsigned char _paste_start[PASTE_MARKER_LEN] = "\033[200~";
+static const unsigned char _paste_end[PASTE_MARKER_LEN] = "\033[201~";
+
+/* A byte that is no part of a marker: text in a paste, a key outside. */
+static Eina_Bool
+_paste_plain(TermSrvPasteFilter *filter, unsigned char c, Eina_Binbuf *out)
+{
+   if (filter->in_paste)
+     {
+        eina_binbuf_append_length(out, &c, 1);
+        return EINA_FALSE;
+     }
+   return termsrv_chord_filter(&filter->chord, (const char *)&c, 1, out);
+}
+
+/* Forwards the held marker bytes that were not yet, in order. They stay
+ * held, as already forwarded, so that the marker can still complete. */
+static Eina_Bool
+_paste_marker_send_unsent(TermSrvPasteFilter *filter, Eina_Binbuf *out)
+{
+   unsigned int i;
+
+   for (i = filter->marker_sent; i < filter->marker_len; i++)
+     {
+        if (_paste_plain(filter, filter->marker[i], out))
+          return EINA_TRUE;
+     }
+   filter->marker_sent = filter->marker_len;
+   return EINA_FALSE;
+}
+
+static Eina_Bool
+_paste_marker_release(TermSrvPasteFilter *filter, Eina_Binbuf *out)
+{
+   Eina_Bool chord_done = _paste_marker_send_unsent(filter, out);
+
+   filter->marker_len = 0;
+   filter->marker_sent = 0;
+   return chord_done;
+}
+
+Eina_Bool
+termsrv_paste_filter(TermSrvPasteFilter *filter, const char *in, int in_len,
+                     Eina_Bool app_bracketed, Eina_Bool more_input,
+                     Eina_Binbuf *out)
+{
+   int i;
+
+   EINA_SAFETY_ON_NULL_RETURN_VAL(out, EINA_FALSE);
+   for (i = 0; i < in_len; i++)
+     {
+        unsigned char c = (unsigned char)in[i];
+
+        if (filter->marker_len || c == '\033')
+          {
+             unsigned int len = filter->marker_len + 1;
+             Eina_Bool start, end;
+
+             filter->marker[len - 1] = c;
+             start = !memcmp(filter->marker, _paste_start, len);
+             end = !memcmp(filter->marker, _paste_end, len);
+             if (len < PASTE_MARKER_LEN && (start || end))
+               {
+                  filter->marker_len = len;
+                  continue;
+               }
+             if (len == PASTE_MARKER_LEN && (start || end))
+               {
+                  Eina_Bool forward;
+
+                  if (start && !filter->in_paste)
+                    {
+                       filter->paste_forwarded = app_bracketed;
+                       forward = app_bracketed;
+                    }
+                  else
+                    forward = filter->in_paste && filter->paste_forwarded;
+                  if (end)
+                    filter->paste_forwarded = EINA_FALSE;
+                  filter->in_paste = start;
+                  termsrv_chord_flush(&filter->chord, out);
+                  if (forward)
+                    eina_binbuf_append_length(out,
+                                              filter->marker + filter->marker_sent,
+                                              len - filter->marker_sent);
+                  filter->marker_len = 0;
+                  filter->marker_sent = 0;
+                  continue;
+               }
+             if (_paste_marker_release(filter, out))
+               return EINA_TRUE;
+             if (c == '\033')
+               {
+                  filter->marker[0] = c;
+                  filter->marker_len = 1;
+                  continue;
+               }
+          }
+        if (_paste_plain(filter, c, out))
+          return EINA_TRUE;
+     }
+   if (filter->marker_len && !filter->in_paste && !more_input)
+     return _paste_marker_send_unsent(filter, out);
+   return EINA_FALSE;
+}
+
+Eina_Bool
+termsrv_paste_filter_timeout(TermSrvPasteFilter *filter, Eina_Binbuf *out)
+{
+   Eina_Bool closing;
+
+   EINA_SAFETY_ON_NULL_RETURN_VAL(out, EINA_FALSE);
+   closing = filter->in_paste && filter->paste_forwarded;
+   filter->in_paste = EINA_FALSE;
+   filter->paste_forwarded = EINA_FALSE;
+   if (_paste_marker_release(filter, out))
+     return EINA_TRUE;
+   if (closing)
+     eina_binbuf_append_length(out, _paste_end, PASTE_MARKER_LEN);
+   return EINA_FALSE;
+}
+
 TermSrvHandoffReply
 termsrv_handoff_reply_find(const char *buf, size_t len, const char *nonce,
                            size_t *start, size_t *end)
@@ -1336,6 +1459,170 @@ _binbuf_is(const Eina_Binbuf *buf, const char *s)
      !memcmp(eina_binbuf_string_get(buf), s, strlen(s));
 }
 
+/* Test: a pasted Ctrl+b d never detaches, whatever the markers around it,
+ * wherever the reads are cut; the markers follow the application. */
+int
+tytest_termsrv_paste_filter(void)
+{
+   TermSrvPasteFilter f;
+   Eina_Binbuf *out = eina_binbuf_new();
+   static const char paste[] = "\033[200~a\x02" "db\033[201~";
+   size_t i;
+
+   assert(out);
+
+#define FEED(s, app) \
+   (eina_binbuf_reset(out), \
+    termsrv_paste_filter(&f, s, sizeof(s) - 1, app, EINA_TRUE, out))
+#define FEED_LAST(s, app) \
+   (eina_binbuf_reset(out), \
+    termsrv_paste_filter(&f, s, sizeof(s) - 1, app, EINA_FALSE, out))
+#define OUT_IS(s) _binbuf_is(out, s)
+
+   memset(&f, 0, sizeof(f));
+   assert(!FEED("\033[200~a\x02" "db\033[201~", EINA_TRUE));
+   assert(OUT_IS("\033[200~a\x02" "db\033[201~"));
+   assert(!f.in_paste && !f.marker_len);
+   assert(!FEED("\033[200~a\x02" "db\033[201~", EINA_FALSE));
+   assert(OUT_IS("a\x02" "db"));
+
+   /* markers and chord cut across reads */
+   assert(!FEED("\033[20", EINA_FALSE) && OUT_IS(""));
+   assert(!FEED("0~\x02", EINA_FALSE) && OUT_IS("\x02"));
+   assert(f.in_paste);
+   assert(!FEED("d\033[201~", EINA_FALSE) && OUT_IS("d"));
+   assert(!f.in_paste);
+   assert(FEED("\x02" "d", EINA_FALSE));
+   assert(OUT_IS(""));
+
+   /* each cut of the paste in two reads gives the same bytes and no detach */
+   for (i = 1; i < sizeof(paste) - 1; i++)
+     {
+        Eina_Binbuf *all = eina_binbuf_new();
+
+        memset(&f, 0, sizeof(f));
+        assert(!termsrv_paste_filter(&f, paste, i, EINA_TRUE, EINA_TRUE, all));
+        assert(!termsrv_paste_filter(&f, paste + i, sizeof(paste) - 1 - i,
+                                     EINA_TRUE, EINA_TRUE, all));
+        assert(eina_binbuf_length_get(all) == sizeof(paste) - 1 &&
+               !memcmp(eina_binbuf_string_get(all), paste,
+                       sizeof(paste) - 1));
+        assert(!f.in_paste && !f.marker_len);
+        eina_binbuf_free(all);
+     }
+
+   /* other sequences, and a lone Escape, are forwarded in order */
+   memset(&f, 0, sizeof(f));
+   assert(!FEED("\033[A", EINA_FALSE) && OUT_IS("\033[A"));
+   assert(!FEED("\033[2", EINA_FALSE) && OUT_IS(""));
+   assert(!FEED("5~", EINA_FALSE) && OUT_IS("\033[25~"));
+   assert(!FEED("\033", EINA_FALSE) && OUT_IS("") && f.marker_len == 1);
+   assert(!FEED("x", EINA_FALSE) && OUT_IS("\033x"));
+   assert(!FEED("\033\033[200~", EINA_FALSE) && OUT_IS("\033"));
+   assert(f.in_paste);
+
+   /* a paste whose end never came: the timeout makes the held bytes text
+    * and the chord works again */
+   memset(&f, 0, sizeof(f));
+   assert(!FEED("\033[200~a\033[20", EINA_FALSE) && OUT_IS("a"));
+   assert(f.in_paste && f.marker_len == 4);
+   eina_binbuf_reset(out);
+   assert(!termsrv_paste_filter_timeout(&f, out) && OUT_IS("\033[20"));
+   assert(!f.in_paste && !f.marker_len);
+   assert(FEED("\x02" "d", EINA_FALSE));
+
+   /* with no more input waiting, a cut marker outside a paste is typed text
+    * and is forwarded at once, so a lone Escape is never joined to the next
+    * key */
+   memset(&f, 0, sizeof(f));
+   assert(!FEED_LAST("\033", EINA_FALSE) && OUT_IS("\033") && f.marker_sent == 1);
+   assert(!FEED_LAST("x", EINA_FALSE) && OUT_IS("x"));
+   assert(!FEED_LAST("\033[20", EINA_FALSE) && OUT_IS("\033[20"));
+   assert(!f.in_paste && f.marker_len == 4 && f.marker_sent == 4);
+   eina_binbuf_reset(out);
+   assert(!termsrv_paste_filter_timeout(&f, out) && OUT_IS(""));
+   assert(!f.marker_len && !f.marker_sent);
+
+   /* the rest of such a marker, within the time allowed, starts the paste
+    * (the application saw the stray prefix, and gets the rest only when it
+    * asked for the markers), and a pasted chord is text */
+   memset(&f, 0, sizeof(f));
+   assert(!FEED_LAST("\033[20", EINA_TRUE) && OUT_IS("\033[20"));
+   assert(!FEED_LAST("0~\x02" "d", EINA_TRUE) && OUT_IS("0~\x02" "d"));
+   assert(f.in_paste && !f.marker_len);
+   assert(!FEED_LAST("\033[201~", EINA_FALSE) && OUT_IS("\033[201~"));
+   memset(&f, 0, sizeof(f));
+   assert(!FEED_LAST("\033[20", EINA_FALSE) && OUT_IS("\033[20"));
+   assert(!FEED_LAST("0~\x02" "d", EINA_FALSE) && OUT_IS("\x02" "d"));
+   assert(f.in_paste);
+
+   /* once expired, the same bytes are typed and the chord detaches */
+   memset(&f, 0, sizeof(f));
+   assert(!FEED_LAST("\033[20", EINA_TRUE) && OUT_IS("\033[20"));
+   assert(!termsrv_paste_filter_timeout(&f, out));
+   assert(FEED_LAST("0~\x02" "d", EINA_TRUE) && OUT_IS("0~"));
+
+   /* a remembered prefix that does not complete is not forwarded twice */
+   memset(&f, 0, sizeof(f));
+   assert(!FEED_LAST("\033[20", EINA_TRUE) && OUT_IS("\033[20"));
+   assert(!FEED_LAST("x", EINA_TRUE) && OUT_IS("x"));
+   assert(!f.marker_len);
+
+   /* a start marker split across a full read is still recognized */
+   assert(!FEED("\033[20", EINA_FALSE) && OUT_IS("") && f.marker_len == 4);
+   assert(!FEED_LAST("0~a", EINA_FALSE) && OUT_IS("a") && f.in_paste);
+
+   /* inside a paste a partial end marker is held even with nothing waiting */
+   assert(!FEED_LAST("b\033[20", EINA_FALSE) && OUT_IS("b"));
+   assert(f.marker_len == 4 && f.in_paste);
+   assert(!FEED_LAST("1~", EINA_FALSE) && OUT_IS("") && !f.in_paste);
+
+   /* a Ctrl+b typed before a paste is not part of a chord with it */
+   memset(&f, 0, sizeof(f));
+   assert(!FEED("\x02\033[200~d", EINA_FALSE) && OUT_IS("\x02" "d"));
+   assert(f.in_paste);
+
+   /* a paste that never ends: a new start marker keeps it a paste */
+   assert(!FEED("\033[200~\x02" "d", EINA_TRUE));
+   assert(OUT_IS("\x02" "d") && f.in_paste);
+   memset(&f, 0, sizeof(f));
+   assert(FEED("\x02" "d", EINA_FALSE));
+
+   /* the application's state is read at the start marker, so that the
+    * markers of a paste are forwarded both or neither */
+   memset(&f, 0, sizeof(f));
+   assert(!FEED("\033[200", EINA_FALSE));
+   assert(!FEED("~", EINA_TRUE) && OUT_IS("\033[200~"));
+   assert(!FEED("a\033[201~", EINA_FALSE) && OUT_IS("a\033[201~"));
+   assert(!FEED("\033[200~a\033[201~", EINA_FALSE) && OUT_IS("a"));
+   assert(!FEED("\033[200~a", EINA_FALSE) && OUT_IS("a"));
+   assert(!FEED("b\033[201~", EINA_TRUE) && OUT_IS("b"));
+
+   /* an end marker outside a paste is dropped: the application never gets
+    * a second one */
+   assert(!FEED("x\033[201~y", EINA_TRUE) && OUT_IS("xy"));
+
+   /* a forwarded paste that expires is closed for the application, and its
+    * late real end is dropped; one that was not forwarded is not */
+   memset(&f, 0, sizeof(f));
+   assert(!FEED("\033[200~a\033[20", EINA_TRUE) && OUT_IS("\033[200~a"));
+   eina_binbuf_reset(out);
+   assert(!termsrv_paste_filter_timeout(&f, out));
+   assert(OUT_IS("\033[20\033[201~") && !f.in_paste);
+   assert(!FEED("1~z", EINA_TRUE) && OUT_IS("1~z"));
+   assert(!FEED("\033[201~", EINA_TRUE) && OUT_IS(""));
+   memset(&f, 0, sizeof(f));
+   assert(!FEED("\033[200~a", EINA_FALSE) && OUT_IS("a"));
+   eina_binbuf_reset(out);
+   assert(!termsrv_paste_filter_timeout(&f, out) && OUT_IS(""));
+
+#undef FEED_LAST
+#undef FEED
+#undef OUT_IS
+   eina_binbuf_free(out);
+   return 0;
+}
+
 /* Test: a reply is removed whole or split anywhere, what only looks like
  * one is forwarded once it stops matching. */
 int
diff --git a/src/bin/termsrv.h b/src/bin/termsrv.h
index 599e021a..1fdccd58 100644
--- a/src/bin/termsrv.h
+++ b/src/bin/termsrv.h
@@ -431,6 +431,49 @@ Eina_Bool termsrv_chord_filter(TermSrvChord *chord, const char *in,
  * byte was held, EINA_FALSE otherwise. */
 Eina_Bool termsrv_chord_flush(TermSrvChord *chord, Eina_Binbuf *out);
 
+/* The detach chord and the bracketed paste markers (ESC [ 200 ~ and
+ * ESC [ 201 ~) of one input stream. Zero it before use. marker holds the
+ * start of a marker seen at the end of a call, marker_len bytes of it, of
+ * which marker_sent were already forwarded as typed text. paste_forwarded
+ * is whether the application got the start marker of the paste in progress,
+ * and so is owed its end. */
+typedef struct
+{
+   TermSrvChord  chord;
+   Eina_Bool     in_paste;
+   Eina_Bool     paste_forwarded;
+   unsigned char marker[6];
+   unsigned int  marker_len;
+   unsigned int  marker_sent;
+} TermSrvPasteFilter;
+
+/* Like termsrv_chord_filter(), but a Ctrl+b d inside a bracketed paste is
+ * text: the chord is only looked for outside of the markers. The markers
+ * of a paste are forwarded when app_bracketed was true at its start marker,
+ * and dropped otherwise, and an end marker outside a paste is dropped, so
+ * that the application never sees a start without its end, or two ends.
+ *
+ * A marker cut at the end of in is held in *filter, unless it is outside a
+ * paste and more_input is false: the terminal writes a start marker whole,
+ * so with no more bytes waiting it is most likely a lone Escape, forwarded
+ * at once instead of being joined with the next key. It is nevertheless
+ * remembered (marker_sent), and if the next call completes the marker, the
+ * paste starts and only the rest of the marker is forwarded: the application
+ * then saw a stray prefix. Call termsrv_paste_filter_timeout() when no call
+ * follows within a short time (0.1 s). A start marker seen while already in
+ * a paste changes nothing. */
+Eina_Bool termsrv_paste_filter(TermSrvPasteFilter *filter, const char *in,
+                               int in_len, Eina_Bool app_bracketed,
+                               Eina_Bool more_input, Eina_Binbuf *out);
+
+/* Leaves the paste, if in one, and appends to out what is held and was not
+ * forwarded yet, as typed bytes, then the end marker if the application
+ * got the start of the paste. For a paste whose end never came, and for
+ * the expiry of a remembered start marker. Returns EINA_TRUE when the
+ * bytes completed the chord. */
+Eina_Bool termsrv_paste_filter_timeout(TermSrvPasteFilter *filter,
+                                       Eina_Binbuf *out);
+
 /* Fill buf with $XDG_RUNTIME_DIR/terminology/tymux, or
  * /tmp/terminology/tymux when XDG_RUNTIME_DIR is unset.
  * Returns 0 on success, -1 if buf is too small. */
diff --git a/src/bin/tymux.c b/src/bin/tymux.c
index 79526ed6..63b16453 100644
--- a/src/bin/tymux.c
+++ b/src/bin/tymux.c
@@ -31,13 +31,17 @@ typedef struct
    size_t                  cells_cap;
    bool                    prev_valid;
    uint32_t                prev_cols, prev_rows;
-   TermSrvChord            chord;
+   TermSrvPasteFilter      paste;
    Eina_Binbuf            *chord_out; /* reused across _input_handle() calls */
    /* the reply of a hand-off that came too late, which is not typed text */
    TermSrvReplyStrip       strip;
    Eina_Binbuf            *strip_out;
-   Ecore_Timer            *strip_timer, *strip_expire;
+   Ecore_Timer            *strip_timer, *strip_expire, *paste_timer;
    bool                    paused;
+   /* the application got the start of a paste that the pause cut: nothing
+    * typed while paused reaches it, so on resume the paste is closed, or
+    * kept open if it goes on, with the markers it lacks */
+   bool                    paste_open;
    bool                    running;
    bool                    readonly;
    /* the modes written to the outer terminal; wanted is the latest the
@@ -449,13 +453,23 @@ _modes_write(Proxy *proxy, const TermSrvMsgNotify *want)
      _out_printf(out, want->appkeypad ? "\033=" : "\033>");
    if (!had->focus_reporting != !want->focus_reporting)
      _mode_put(out, 1004, want->focus_reporting);
-   if (!had->bracketed_paste != !want->bracketed_paste)
-     _mode_put(out, 2004, want->bracketed_paste);
    _out_flush(out);
    eina_strbuf_free(out);
    proxy->modes = *want;
 }
 
+/* The outer terminal brackets what is pasted whatever the application asked
+ * for, also while paused: _input_feed() needs the markers to keep a paste
+ * from detaching. What the application wanted only decides whether the
+ * markers are forwarded. A read-only proxy does not ask for them, and its
+ * pastes are scanned for the chord like typed text. */
+static void
+_paste_mode_set(const Proxy *proxy)
+{
+   if (!proxy->readonly)
+     _write_str("\033[?2004h");
+}
+
 /* Forward the input modes of the application to the outer terminal. Nothing
  * is written while paused or read-only: what is wanted is kept, to be applied
  * on resume. */
@@ -503,14 +517,11 @@ _detach(Proxy *proxy)
    proxy->running = false;
 }
 
-/* Forward what was typed, minus the detach chord (termsrv_chord_filter(),
- * shared with the native client's key input in termtymux.c). While paused,
- * only the chord is honored. */
+/* Send what chord_out holds, or detach if the chord completed. */
 static void
-_input_feed(Proxy *proxy, const char *in, size_t n)
+_input_send(Proxy *proxy, Eina_Bool chord_done)
 {
-   eina_binbuf_reset(proxy->chord_out);
-   if (termsrv_chord_filter(&proxy->chord, in, (int)n, proxy->chord_out))
+   if (chord_done)
      {
         _detach(proxy);
         return;
@@ -522,6 +533,66 @@ _input_feed(Proxy *proxy, const char *in, size_t n)
      proxy->running = false;
 }
 
+/* A paste whose end never came, or a start marker that was not completed
+ * in time, ends: what is held is typed text. */
+static Eina_Bool
+_cb_paste_timeout(void *data)
+{
+   Proxy *proxy = data;
+
+   proxy->paste_timer = NULL;
+   eina_binbuf_reset(proxy->chord_out);
+   _input_send(proxy,
+               termsrv_paste_filter_timeout(&proxy->paste, proxy->chord_out));
+   if (!proxy->running)
+     ecore_main_loop_quit();
+   return ECORE_CALLBACK_CANCEL;
+}
+
+/* How long a paste may stay silent before it is taken for one whose end
+ * marker was lost, and typed text detaches again. */
+#define TYMUX_PASTE_IDLE_SECONDS 5.0
+/* How long after a cut start marker was forwarded as typed text its rest may
+ * still start a paste. */
+#define TYMUX_PASTE_MARKER_SECONDS 0.1
+
+/* Forward what was typed, minus the detach chord (shared with the native
+ * client's key input in termtymux.c) and minus what was pasted, which never
+ * completes it. While paused, the filter still runs and only a chord typed
+ * outside a paste detaches. A read-only proxy asked for no markers: it only
+ * looks for the chord. more_input tells that more bytes are already waiting:
+ * a start marker cut short is held only then, so that a lone Escape is
+ * forwarded at once, never joined with the next key (see
+ * termsrv_paste_filter()). */
+static void
+_input_feed(Proxy *proxy, const char *in, size_t n, bool more_input)
+{
+   Eina_Bool chord_done;
+
+   eina_binbuf_reset(proxy->chord_out);
+   if (proxy->readonly)
+     {
+        _input_send(proxy, termsrv_chord_filter(&proxy->paste.chord, in,
+                                                (int)n, proxy->chord_out));
+        return;
+     }
+   chord_done = termsrv_paste_filter(&proxy->paste, in, (int)n,
+                                     proxy->wanted.bracketed_paste &&
+                                     !proxy->paused, more_input,
+                                     proxy->chord_out);
+   if (proxy->paste_timer)
+     {
+        ecore_timer_del(proxy->paste_timer);
+        proxy->paste_timer = NULL;
+     }
+   if (!chord_done && (proxy->paste.in_paste || proxy->paste.marker_len))
+     proxy->paste_timer = ecore_timer_add(proxy->paste.in_paste ?
+                                          TYMUX_PASTE_IDLE_SECONDS :
+                                          TYMUX_PASTE_MARKER_SECONDS,
+                                          _cb_paste_timeout, proxy);
+   _input_send(proxy, chord_done);
+}
+
 /* How long after the start of the proxy an answer to the hand-off that
  * timed out is still looked for. */
 #define TYMUX_LATE_REPLY_SECONDS 5.0
@@ -537,7 +608,7 @@ _cb_strip_flush(void *data)
    eina_binbuf_reset(proxy->strip_out);
    if (termsrv_reply_strip_flush(&proxy->strip, proxy->strip_out))
      _input_feed(proxy, (const char *)eina_binbuf_string_get(proxy->strip_out),
-                 eina_binbuf_length_get(proxy->strip_out));
+                 eina_binbuf_length_get(proxy->strip_out), false);
    if (!proxy->running)
      ecore_main_loop_quit();
    return ECORE_CALLBACK_CANCEL;
@@ -559,7 +630,7 @@ _cb_strip_expire(void *data)
    termsrv_reply_strip_disarm(&proxy->strip, proxy->strip_out);
    if (eina_binbuf_length_get(proxy->strip_out) > 0)
      _input_feed(proxy, (const char *)eina_binbuf_string_get(proxy->strip_out),
-                 eina_binbuf_length_get(proxy->strip_out));
+                 eina_binbuf_length_get(proxy->strip_out), false);
    if (!proxy->running)
      ecore_main_loop_quit();
    return ECORE_CALLBACK_CANCEL;
@@ -568,11 +639,11 @@ _cb_strip_expire(void *data)
 /* _input_feed() of in minus the late hand-off reply, if any. Once that
  * reply came, or its window is over, input is fed directly. */
 static void
-_input_strip_feed(Proxy *proxy, const char *in, size_t n)
+_input_strip_feed(Proxy *proxy, const char *in, size_t n, bool more_input)
 {
    if (!termsrv_reply_strip_armed(&proxy->strip))
      {
-        _input_feed(proxy, in, n);
+        _input_feed(proxy, in, n, more_input);
         return;
      }
    if (proxy->strip_timer)
@@ -584,7 +655,7 @@ _input_strip_feed(Proxy *proxy, const char *in, size_t n)
    termsrv_reply_strip(&proxy->strip, in, n, proxy->strip_out);
    if (eina_binbuf_length_get(proxy->strip_out) > 0)
      _input_feed(proxy, (const char *)eina_binbuf_string_get(proxy->strip_out),
-                 eina_binbuf_length_get(proxy->strip_out));
+                 eina_binbuf_length_get(proxy->strip_out), more_input);
    if (!termsrv_reply_strip_armed(&proxy->strip) && proxy->strip_expire)
      {
         ecore_timer_del(proxy->strip_expire);
@@ -599,6 +670,8 @@ _input_handle(Proxy *proxy)
 {
    char in[4096];
    ssize_t n;
+   int queued = 0;
+   bool more_input = false;
 
    n = read(STDIN_FILENO, in, sizeof(in));
    if (n < 0 && errno == EINTR)
@@ -608,7 +681,10 @@ _input_handle(Proxy *proxy)
         _detach(proxy);
         return;
      }
-   _input_strip_feed(proxy, in, (size_t)n);
+   if (n == (ssize_t)sizeof(in) ||
+       (ioctl(STDIN_FILENO, FIONREAD, &queued) == 0 && queued > 0))
+     more_input = true;
+   _input_strip_feed(proxy, in, (size_t)n, more_input);
 }
 
 static void
@@ -650,7 +726,11 @@ _msg_handle(Proxy *proxy, int type, void *payload, uint32_t size)
          break;
       case TSRV_MSG_PAUSE:
          proxy->paused = true;
+         proxy->paste_open = proxy->paste.in_paste &&
+            proxy->paste.paste_forwarded;
+         proxy->paste.paste_forwarded = EINA_FALSE;
          _modes_reset(proxy);
+         _paste_mode_set(proxy);
          _paused_draw();
          break;
       case TSRV_MSG_RESUME:
@@ -663,6 +743,25 @@ _msg_handle(Proxy *proxy, int type, void *payload, uint32_t size)
            }
          proxy->strip.held_len = 0;
          proxy->paused = false;
+         proxy->paste.chord.prefix_pending = 0;
+         proxy->paste.marker_len = 0;
+         proxy->paste.marker_sent = 0;
+         if (proxy->wanted.bracketed_paste)
+           {
+              if (proxy->paste.in_paste)
+                {
+                   if (!proxy->paste_open &&
+                       termsrv_msg_send(proxy->sock, TSRV_MSG_INPUT,
+                                        "\033[200~", 6) < 0)
+                     proxy->running = false;
+                   proxy->paste.paste_forwarded = EINA_TRUE;
+                }
+              else if (proxy->paste_open &&
+                       termsrv_msg_send(proxy->sock, TSRV_MSG_INPUT,
+                                        "\033[201~", 6) < 0)
+                proxy->running = false;
+           }
+         proxy->paste_open = false;
          _resize_send(proxy);
          _screen_draw(proxy, true);
          _modes_apply(proxy, &proxy->wanted);
@@ -854,10 +953,12 @@ _proxy_run(int sock, const char *nonce, const char *typed, size_t typed_len,
      proxy.strip_expire = ecore_timer_add(TYMUX_LATE_REPLY_SECONDS,
                                           _cb_strip_expire, &proxy);
    _raw_mode_enter();
+   _modes_reset(&proxy);
+   _paste_mode_set(&proxy);
    _resize_send(&proxy);
    _screen_draw(&proxy, true);
    if (typed_len > 0)
-     _input_strip_feed(&proxy, typed, typed_len);
+     _input_strip_feed(&proxy, typed, typed_len, false);
 
    if (proxy.running)
      ecore_main_loop_begin();
@@ -871,6 +972,7 @@ _proxy_run(int sock, const char *nonce, const char *typed, size_t typed_len,
    if (proxy.fdh_winch) ecore_main_fd_handler_del(proxy.fdh_winch);
    if (proxy.strip_timer) ecore_timer_del(proxy.strip_timer);
    if (proxy.strip_expire) ecore_timer_del(proxy.strip_expire);
+   if (proxy.paste_timer) ecore_timer_del(proxy.paste_timer);
    if (proxy.eh_exit) ecore_event_handler_del(proxy.eh_exit);
    if (proxy.eh_hup) ecore_event_handler_del(proxy.eh_hup);
    close(proxy.winch_pipe[0]);
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index 926fd3f9..48e12131 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -270,6 +270,7 @@ static struct {
        { "termsrv_command_route", tytest_termsrv_command_route},
        { "termsrv_chord_filter", tytest_termsrv_chord_filter},
        { "termsrv_chord_flush", tytest_termsrv_chord_flush},
+       { "termsrv_paste_filter", tytest_termsrv_paste_filter},
        { "termsrv_socket_path", tytest_termsrv_socket_path},
        { "termsrv_tymux_dir_ensure", tytest_termsrv_tymux_dir_ensure},
        { "termsrv_daemon_kill", tytest_termsrv_daemon_kill},
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index 45bf23b4..cced3912 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -210,6 +210,7 @@ int tytest_termsrv_handoff_reply(void);
 int tytest_termsrv_command_route(void);
 int tytest_termsrv_chord_filter(void);
 int tytest_termsrv_chord_flush(void);
+int tytest_termsrv_paste_filter(void);
 int tytest_termsrv_socket_path(void);
 int tytest_termsrv_tymux_dir_ensure(void);
 int tytest_termsrv_daemon_kill(void);
diff --git a/tests/meson.build b/tests/meson.build
index 47dea0d9..8c4c7228 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -96,7 +96,7 @@ if tests
          find_program('python3'),
          args: [files('tymux.py'), tymux.full_path()],
          depends: [tymux, tymuxd],
-         timeout: 120)
+         timeout: 240)
     # Drives a real tymuxd through the native client built into tytest.
     test('tymux-live',
          tytest,
diff --git a/tests/tymux.py b/tests/tymux.py
index d6ed6212..07a855b4 100644
--- a/tests/tymux.py
+++ b/tests/tymux.py
@@ -237,6 +237,242 @@ def check_no_focus_event():
     tymux("kill", name)
 
 
+def check_paste_no_detach():
+    name = "p%d" % os.getpid()
+    c = Client(["new", name])
+    check(c.expect("$ "), "paste: the shell prompt is shown")
+    check(c.expect("\033[?2004h"),
+          "paste: bracketed paste is set whatever the application wants")
+    check(c.out.count(b"\033[?2004h") == 1 and
+          b"\033[?1000l" in c.out and
+          c.out.index(b"\033[?2004l") < c.out.index(b"\033[?2004h"),
+          "paste: the proxy resets every mode, then sets bracketed paste")
+    c.send("head -c 4 | od -An -tx1; echo END\r")
+    c.idle(0.3)
+    c.send("\033[200~a\x02db\033[201~\r")
+    check(c.expect("61 02 64 62"),
+          "paste: the markers are stripped when the application has not "
+          "asked for them")
+    check(c.expect("END"), "paste: a pasted Ctrl+b d does not detach")
+    c.send("printf '\\033[?2004h'; head -c 16 | od -An -tx1; echo END2\r")
+    c.idle(0.3)
+    c.send("\033[200~a\x02db\033[201~\r")
+    check(c.expect("1b 5b 32 30 30 7e 61 02 64 62 1b 5b 32 30 31 7e"),
+          "paste: the markers are forwarded when the application asked")
+    check(c.expect("END2"), "paste: a pasted Ctrl+b d does not detach, ask")
+    # a paste whose end marker is cut: the terminal writes a start marker
+    # whole, but the end of a long paste may be split
+    c.send("\033[200~\x02d\033[20")
+    c.idle(0.02)
+    c.send("1~")
+    c.idle(0.3)
+    check(c.wait(0.3) is None, "paste: a cut end marker, after a pasted chord")
+    # a Ctrl+b typed while paused is dropped, not held for the next key
+    hijack = Client(["attach", name])
+    check(c.expect("PAUSED"), "paste: the proxy is paused")
+    c.send("\x02")
+    c.idle(0.2)
+    mark = len(c.out)
+    hijack.send("\x02d")
+    check(hijack.wait() == 0, "paste: the hijacker detaches")
+    c.idle(0.5)
+    c.send("d")
+    check(c.wait(0.5) is None, "paste: a Ctrl+b typed while paused is dropped")
+    c.send("head -c 1 | od -An -tx1\r")
+    c.idle(0.3)
+    c.send("\033")
+    check(c.expect("1b"), "paste: a lone Escape reaches the application")
+    c.send("\x02d")
+    check(c.wait() == 0, "paste: a typed Ctrl+b d still detaches")
+    tymux("kill", name)
+
+
+def check_paste_edge_cases():
+    name = "q%d" % os.getpid()
+    c = Client(["new", name])
+    check(c.expect("$ "), "paste edge: the shell prompt is shown")
+    hijack = Client(["attach", name])
+    check(c.expect("PAUSED"), "paste edge: the proxy is paused")
+    hijack.idle(0.3)
+    check(c.out.rindex(b"\033[?2004h") > c.out.rindex(b"\033[?2004l"),
+          "paste edge: bracketed paste stays on while paused")
+    c.send("\033[200~\x02d\033[201~")
+    check(c.wait(0.5) is None, "paste edge: a paste while paused does not detach")
+    # a paste that starts before the pause and ends after the resume
+    c.send("\033[200~")
+    c.idle(0.2)
+    hijack.send("\x02d")
+    check(hijack.wait() == 0, "paste edge: the hijacker detaches")
+    c.idle(0.5)
+    mark = len(c.out)
+    hijack = Client(["attach", name])
+    check(expect_after(c, mark, "PAUSED"), "paste edge: paused again")
+    c.send("\x02d")
+    check(c.wait(0.5) is None, "paste edge: a chord pasted across a pause")
+    hijack.send("\x02d")
+    check(hijack.wait() == 0, "paste edge: the hijacker detaches again")
+    c.idle(0.5)
+    c.send("\033[201~")
+    c.idle(0.2)
+    c.send("\x02d")
+    check(c.wait() == 0, "paste edge: the paste ended, the chord detaches")
+
+    # a start marker cut when nothing is queued, completed in time
+    c = Client(["attach", name])
+    check(c.expect("$ "), "paste edge: attached again")
+    c.send("\033[20")
+    c.idle(0.03)
+    c.send("0~\x02d\033[201~")
+    check(c.wait(0.5) is None, "paste edge: a split start marker is a paste")
+    # a delayed end marker is still the end
+    c.send("\033[200~x\033[20")
+    c.idle(0.4)
+    c.send("1~")
+    c.idle(0.2)
+    c.send("\x02d")
+    check(c.wait() == 0, "paste edge: a late end marker ends the paste")
+
+    # a paste that never ends stops disabling the chord
+    c = Client(["attach", name])
+    check(c.expect("$ "), "paste edge: attached once more")
+    c.send("\033[200~")
+    c.idle(0.3)
+    c.send("\x02d")
+    check(c.wait(0.5) is None, "paste edge: the chord is text in a paste")
+    c.idle(5.0)
+    c.send("\x02d")
+    check(c.wait() == 0, "paste edge: an idle paste stops hiding the chord")
+    tymux("kill", name)
+
+
+def paste_bytes_session(name, count):
+    """a client whose application reads count bytes, with markers asked for,
+    and prints them as one string of hex digits"""
+    c = Client(["attach", name])
+    check(c.expect("$ "), "paste markers: attached")
+    c.send("printf '\\033[?2004h'; head -c %d | od -An -v -w%d -tx1 | "
+           "tr -d ' '; echo END\r" % (count, count))
+    c.idle(0.4)
+    return c
+
+
+def pause_and_resume(c, name, during):
+    mark = len(c.out)
+    hijack = Client(["attach", name])
+    check(expect_after(c, mark, "PAUSED"), "paste markers: paused")
+    hijack.idle(0.3)
+    during()
+    c.idle(0.2)
+    hijack.send("\x02d")
+    check(hijack.wait() == 0, "paste markers: the hijacker detaches")
+    c.idle(0.5)
+
+
+def check_paste_markers_balanced():
+    name = "b%d" % os.getpid()
+    c = Client(["new", name])
+    check(c.expect("$ "), "paste markers: the shell prompt is shown")
+    START, END = "1b5b3230307e", "1b5b3230317e"
+
+    # what is read while paused does not reach the session after the resume
+    c.send("\x02d")
+    c.wait()
+    c = paste_bytes_session(name, 8)
+    pause_and_resume(c, name, lambda: (c.send("\033[200~paused"),
+                                       c.idle(0.2), c.send("\033[20"),
+                                       c.idle(0.3)))
+    mark = len(c.out)
+    c.send("X\r")
+    check(expect_after(c, mark, "END"), "paste markers: the read ends")
+    check((START + "580a").encode() in c.out[mark:],
+          "paste markers: bytes read while paused do not leak after it")
+    c.send("\033[201~")
+    c.idle(0.2)
+    c.send("\x02d")
+    c.wait()
+
+    # a start while paused, its end after the resume: the program gets the
+    # start the pause hid from it, and the paste stays a paste
+    c = paste_bytes_session(name, 17)
+    pause_and_resume(c, name, lambda: c.send("\033[200~"))
+    mark = len(c.out)
+    c.send("tail\033[201~\r")
+    check(expect_after(c, mark, "END"), "paste markers: the read ends, 2a")
+    check((START + "7461696c" + END + "0a").encode() in c.out[mark:],
+          "paste markers: a paste started while paused stays bracketed")
+    c.send("\x02d")
+    c.wait()
+
+    # the rest of a paste still arriving at the resume is bracketed
+    c = paste_bytes_session(name, 28)
+    c.send("\033[200~before")
+    c.idle(0.3)
+    pause_and_resume(c, name, lambda: None)
+    mark = len(c.out)
+    c.send("rest\rmore\033[201~\r")
+    check(expect_after(c, mark, "END"), "paste markers: the read ends, rest")
+    check((START + "6265666f7265" + "726573740a" + "6d6f7265" + END +
+           "0a").encode() in c.out,
+          "paste markers: the rest of a paste is still bracketed")
+    c.send("\x02d")
+    c.wait()
+
+    # the program that got the start exited while paused, and the next one
+    # asks for no paste: it gets no end marker
+    c = Client(["attach", name])
+    check(c.expect("$ "), "paste markers: attached, exit")
+    c.send("printf '\\033[?2004h'; head -c 13 >/dev/null; "
+           "printf '\\033[?2004l'; "
+           "echo R$(head -c 1 | od -An -tx1 | tr -d ' ')\r")
+    c.idle(0.4)
+    c.send("\033[200~before")
+    c.idle(0.3)
+    mark = len(c.out)
+    hijack = Client(["attach", name])
+    check(expect_after(c, mark, "PAUSED"), "paste markers: paused, exit")
+    hijack.idle(0.3)
+    hijack.send("\r")
+    hijack.idle(0.5)
+    hijack.send("\x02d")
+    check(hijack.wait() == 0, "paste markers: the hijacker detaches, exit")
+    c.idle(0.5)
+    mark = len(c.out)
+    c.send("Z\r")
+    check(expect_after(c, mark, "R5a"), "paste markers: the next program reads")
+    check(b"R1b" not in c.out[mark:],
+          "paste markers: no end marker for a program that got no start")
+    c.send("\033[201~")
+    c.idle(0.2)
+    c.send("\x02d")
+    c.wait()
+
+    # a start before the pause, its end while paused
+    c = paste_bytes_session(name, 24)
+    c.send("\033[200~before")
+    c.idle(0.3)
+    pause_and_resume(c, name, lambda: c.send("\033[201~"))
+    mark = len(c.out)
+    c.send("typed\r")
+    check(expect_after(c, mark, "END"), "paste markers: the read ends, 2b")
+    check((START + "6265666f7265" + END + "74797065640a").encode() in c.out,
+          "paste markers: a paste cut by a pause is closed on resume")
+    c.send("\x02d")
+    c.wait()
+
+    # a forwarded start, then the silence of 5 seconds
+    c = paste_bytes_session(name, 22)
+    c.send("\033[200~body")
+    c.idle(5.5)
+    mark = len(c.out)
+    c.send("after\r")
+    check(expect_after(c, mark, "END"), "paste markers: the read ends, 2c")
+    check((START + "626f6479" + END + "61667465720a").encode() in c.out,
+          "paste markers: an expired paste is closed for the application")
+    c.send("\x02d")
+    c.wait()
+    tymux("kill", name)
+
+
 def check_modes():
     name = "m%d" % os.getpid()
     c = Client(["new", name])
@@ -285,6 +521,8 @@ def check_modes():
               ("\033[?1l", "\033>", "\033[?1003l", "\033[?1015l",
                "\033[?1004l", "\033[?2004l")),
           "modes: a pause resets the modes that were set")
+    check(tail.rindex(b"\033[?2004h") > tail.index(b"\033[?2004l"),
+          "modes: a pause keeps bracketed paste on")
     hijack.idle(0.3)
     emit(hijack, "\\033[?1003l\\033[?1000h")
     check(hijack.expect("\033[?1000h"), "modes: the hijacker sets the mouse")
@@ -299,8 +537,8 @@ def check_modes():
     c.idle(0.3)
     tail = c.out[mark:]
     check(all(seq.encode() in tail for seq in
-              ("\033[?1h", "\033=", "\033[?1015h", "\033[?1004h",
-               "\033[?2004h")) and b"\033[?1003h" not in tail,
+              ("\033[?1h", "\033=", "\033[?1015h", "\033[?1004h")) and
+          b"\033[?1003h" not in tail and b"\033[?2004" not in tail,
           "modes: a resume sets the modes again")
 
     mark = len(c.out)
@@ -513,6 +751,9 @@ def main():
 
     check_modes()
     check_no_focus_event()
+    check_paste_no_detach()
+    check_paste_edge_cases()
+    check_paste_markers_balanced()
 
     # a client that stops reading is dropped instead of blocking tymuxd
     a = Client(["new", NAME])

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to