Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion Lib/_pyrepl/historical_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@


isearch_keymap: tuple[tuple[KeySpec, CommandName], ...] = tuple(
[("\\%03o" % c, "isearch-end") for c in range(256) if chr(c) != "\\"]
[("\\%03o" % c, "isearch-end") for c in range(256) if chr(c) not in ("\\", "\x1b")]
+ [(c, "isearch-add-character") for c in map(chr, range(32, 127)) if c != "\\"]
+ [
("\\%03o" % c, "isearch-add-character")
Expand All @@ -45,6 +45,7 @@
(r"\C-c", "isearch-cancel"),
(r"\C-g", "isearch-cancel"),
(r"\<backspace>", "isearch-backspace"),
(r"\x1b[200~", "isearch-bracketed-paste"),
]
)

Expand Down Expand Up @@ -209,6 +210,21 @@ def do(self) -> None:
r.pop_input_trans()
r.dirty = True

class isearch_bracketed_paste(commands.Command):
def do(self) -> None:
r = self.reader
b = r.buffer
done = "\x1b[201~"
data = ""
while done not in data:
ev = r.console.getpending()
data += ev.data
paste_content = data.replace(done, "")
r.isearch_term += paste_content
r.dirty = True
if "".join(b[r.pos:r.pos+len(r.isearch_term)]) != r.isearch_term:
r.isearch_next()


@dataclass
class HistoricalReader(Reader):
Expand Down Expand Up @@ -245,6 +261,7 @@ def __post_init__(self) -> None:
isearch_backspace,
isearch_forwards,
isearch_backwards,
isearch_bracketed_paste,
operate_and_get_next,
history_search_backward,
history_search_forward,
Expand Down
38 changes: 38 additions & 0 deletions Lib/test/test_pyrepl/test_pyrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1509,6 +1509,44 @@ def test_bracketed_paste_single_line(self):
output = multiline_input(reader)
self.assertEqual(output, input_code)

def test_bracketed_paste_in_isearch(self):
paste_start = "\x1b[200~"
paste_end = "\x1b[201~"

events = itertools.chain(
# Add some history
code_to_events("print('hello')\n"),
# Search for 'hello'
[
Event(evt="key", data="\x12", raw=bytearray(b"\x12")),
],
code_to_events(paste_start),
code_to_events("hello"),
code_to_events(paste_end),
[
Event(evt="key", data="\n", raw=bytearray(b"\n")),
Event(evt="key", data="\n", raw=bytearray(b"\n")),
],
[
Event(evt="key", data="\x12", raw=bytearray(b"\x12")),
],
# Search for 'world', which should not be found
code_to_events(paste_start),
code_to_events("world"),
code_to_events(paste_end),
[
Event(evt="key", data="\n", raw=bytearray(b"\n")),
Event(evt="key", data="\n", raw=bytearray(b"\n")),
],
)

reader = self.prepare_reader(events)
multiline_input(reader)
output = multiline_input(reader)
self.assertEqual(output, "print('hello')")
output = multiline_input(reader)
self.assertEqual(output, "")


@skipUnless(pty, "requires pty")
class TestDumbTerminal(ReplTestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Handle bracketed paste in :mod:`!_pyrepl` isearch mode.
Loading