Skip to content

Insert pasted text literally instead of as keystrokes - #6

Merged
leancode merged 1 commit into
masterfrom
bracketed-paste
Sep 13, 2026
Merged

leancode merged 1 commit into
masterfrom
bracketed-paste

Conversation

@leancode

Copy link
Copy Markdown
Owner

Pasting indented text produced a staircase — each line indented one level further than the last.

Cause

A paste arrives at the editor as ordinary keystrokes. editor.py auto-indents on every newline, prepending the current line's leading whitespace to the next line — which, in a paste, already carries its own indentation. Each line compounds the one before it.

Reproduced by pasting an 11-line .bashrc fragment, leading whitespace per line:

source before after
0 0 0
0 0 0
4 4 4
6 10 6
0 10 0
0 10 0

The literal [200~ and [201~ markers were also being typed into the buffer, since nothing recognised them.

Fix

Enable bracketed paste mode (ESC[?2004h), which makes the terminal wrap pastes in ESC[200~ … ESC[201~. Those have no terminfo entry, so curses hands them over as plain characters — verified — and they are matched in get_input. Non-matching characters are pushed back with unget_wch, so a lone ESC and every other escape sequence are untouched.

The payload is then inserted with auto-indent suppressed, tabs kept literal, and a single undo state for the whole paste.

Verification

Pasting the fragment now produces a file byte-identical to the source (diff reports no difference).

ESC handling, the main regression risk since Suplemon binds ESC to single-cursor:

input result
ABC ABC
AB ESC CD ABCD — ESC consumed, next char not eaten
ESC ABC ABC
ESC ESC handled
ESC then a paste XPASTEDY

Edge cases: tabs stay literal tabs, mid-line paste, multi-line into an empty buffer, empty paste, and a paste ending in a newline all behave. The mode is disabled on exit, verified as the last sequence written, so the shell does not inherit it.

test.sh passes, flake8 clean.

Not fixed here

Undoing a paste steps back one edit too far. That is pre-existing and unrelated: on master, type KEEP, Enter, one undo also yields K. store_action_state only records continued same-action editing when current_state < len(history)-1, so a typing run after the first character is never captured and the next action's undo point is stale. Worth its own change.

A paste reaches the editor as ordinary characters, so auto indent ran on
every newline in it. Each pasted line already carries its own leading
whitespace, and auto indent prepended the previous line's on top, so the
indentation grew line by line into a staircase. Tabs in the pasted text
were expanded to spaces for the same reason.

Enable the terminal's bracketed paste mode, which wraps a paste in
ESC[200~ and ESC[201~. Those markers have no terminfo entry, so they
arrive as plain characters and are matched in get_input; anything that
does not match is pushed back, leaving a lone ESC and every other escape
sequence untouched.

The payload is inserted with auto indent suppressed and one undo state
for the whole paste rather than one per keystroke. The mode is turned
off again on exit so the shell does not inherit it.

Terminals that do not support bracketed paste ignore the enabling
sequence and behave exactly as before.
@leancode
leancode merged commit 95ede62 into master Sep 13, 2026
4 checks passed
@leancode
leancode deleted the bracketed-paste branch September 13, 2026 16:18
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