Skip to content

paste: send pastes exactly as xterm does - #2116

Merged
raphamorim merged 2 commits into
mainfrom
paste-like-xterm
Nov 20, 2026
Merged

raphamorim merged 2 commits into
mainfrom
paste-like-xterm

Conversation

@raphamorim

@raphamorim raphamorim commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

Follow-up to #2115: pastes are now sent exactly as xterm sends them, where #2115 still differed in two places.

Unbracketed line breaks. Only LF becomes CR, what Enter sends. A CRLF line ending is therefore two Enters, as xterm sends it; #2115 kept Rio's old behavior of collapsing it to one. Bracketed pastes still keep their line endings.

U+009B. The 8-bit CSI character is text like any other: it's no longer replaced by a space, and only ESC [201~ counts as a bracketed paste end when deciding whether to confirm. In UTF-8 the character is two bytes (C2 9B), and xterm's list is about single control bytes.

Encoding. With that, every byte encode changes is ASCII and replaced by ASCII, so the text can be changed in place:

  • encode_in_place compares each byte with the 16 replaced values and picks the result without a branch, so the loop compiles to SIMD; one loop per mode, so neither tests the mode per byte.
  • encode_owned(String) takes a paste the caller owns, such as the clipboard's text, encodes it in its own buffer and hands back the markers and the text as separate writes: the text is never copied. Rio's Screen::paste and paste_from_clipboard take the String now, and the dialog gives its held paste back without a copy.
  • librio's Surface::paste takes impl Into<String>: a String isn't copied, a &str is copied once (the writer thread needs the bytes after the call). rio_surface_paste hands over the Cow from from_utf8_lossy, so the C buffer is copied once and only once.
32 MB before after
code, bracketed 10.2 ms 4.1 ms (owned), 4.8 ms (&str)
code, unbracketed 15.9 ms 5.0 ms
accented text, bracketed 17.8 ms 4.1 ms (owned), 4.8 ms (&str)
accented text, unbracketed 21.2 ms 5.0 ms

Docs. librio's paste doc and rio_surface_paste in the header now say LF is sent as CR, and the comment in Rio's paste path too.

Tests. The unbracketed test expects \r\r for CRLF, U+009B is checked to pass through, is_replaced is checked against encode for every ASCII character, and an owned paste is checked to keep its buffer. rio-vt (733), rioterm (310) and librio (76) pass, and the workspace builds.

- Unbracketed, only LF becomes CR: a CRLF line ending is two Enters,
  as xterm sends it, where it was collapsed to one.
- U+009B is text like any other character: it is no longer replaced,
  and only ESC [201~ is a bracketed paste end for confirmation.
- Every byte encode changes is ASCII, so it copies the text once and
  changes it in place; accented text no longer breaks the copy into
  runs (32 MB bracketed: 17.7 ms to 9.7 ms).
- The is_replaced test checks every ASCII character against encode.
- encode_in_place compares each byte with the replaced values and picks
  the result without a branch, so the loop compiles to SIMD; one loop
  per mode. 32 MB: 10-21 ms to about 5 ms.
- encode_owned takes a String, encodes its buffer in place and hands
  back the markers and the text as separate writes, so the text isn't
  copied. Rio's paste and paste_from_clipboard take the String; the
  dialog gives its held paste back without a copy.
- librio's Surface::paste takes impl Into<String>: a String isn't
  copied, a &str once. rio_surface_paste hands over the Cow from
  from_utf8_lossy, so the C buffer is copied once.
- Test: an owned paste keeps its buffer and encodes like encode.
@raphamorim
raphamorim merged commit 6ca1153 into main Nov 20, 2026
5 checks passed
@raphamorim
raphamorim deleted the paste-like-xterm branch November 20, 2026 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant