|
30 | 30 | from dataclasses import dataclass, field |
31 | 31 |
|
32 | 32 | import os |
| 33 | +import re |
33 | 34 | from site import gethistoryfile |
34 | 35 | import sys |
35 | 36 | from rlcompleter import Completer as RLCompleter |
@@ -120,18 +121,22 @@ def __post_init__(self) -> None: |
120 | 121 | self.commands["maybe-accept"] = maybe_accept |
121 | 122 | self.commands["backspace_dedent"] = backspace_dedent |
122 | 123 | self.commands["backspace-dedent"] = backspace_dedent |
| 124 | + self.commands["paste_mode"] = paste_mode |
| 125 | + self.commands["paste-mode"] = paste_mode |
| 126 | + self.commands["perform_bracketed_paste"] = perform_bracketed_paste |
| 127 | + self.commands["perform-bracketed-paste"] = perform_bracketed_paste |
123 | 128 |
|
124 | 129 | @property |
125 | 130 | def can_strip_repl_prompts(self) -> bool: |
126 | 131 | return ( |
127 | 132 | self.paste_repl_sessions |
128 | | - and commands._is_repl_session(self.get_unicode()) |
| 133 | + and _is_repl_session(self.get_unicode()) |
129 | 134 | ) |
130 | 135 |
|
131 | 136 | def get_message(self) -> str: |
132 | 137 | message = super().get_message() |
133 | 138 | if not message and not self.paste_mode and self.can_strip_repl_prompts: |
134 | | - return self._format_error(commands._REPL_PASTE_HINT) |
| 139 | + return self._format_error(_REPL_PASTE_HINT) |
135 | 140 | return message |
136 | 141 |
|
137 | 142 | def get_stem(self) -> str: |
@@ -289,6 +294,57 @@ def _should_auto_indent(buffer: list[str], pos: int) -> bool: |
289 | 294 | return last_char == ":" |
290 | 295 |
|
291 | 296 |
|
| 297 | +_REPL_PASTE_HINT = "press F3 (paste mode) to strip >>> and ... prompts" |
| 298 | + |
| 299 | + |
| 300 | +def _is_repl_session(data: str) -> bool: |
| 301 | + return data[:3] in {">>>", "..."} and data[3:4] in {"", " ", "\r", "\n"} |
| 302 | + |
| 303 | + |
| 304 | +def _strip_repl_prompts(data: str) -> str: |
| 305 | + lines: list[str] = [] |
| 306 | + statement_start = None |
| 307 | + for line in re.findall(r"[^\r\n]*(?:\r\n?|\n|$)", data): |
| 308 | + if _is_repl_session(line): |
| 309 | + if line.startswith(">>>") or statement_start is None: |
| 310 | + statement_start = len(lines) |
| 311 | + lines.append(line[3:].removeprefix(" ")) |
| 312 | + elif statement_start is not None: |
| 313 | + if line.rstrip("\r\n") == "KeyboardInterrupt": |
| 314 | + # Ctrl-C leaves the abandoned input on screen with its prompts. |
| 315 | + del lines[statement_start:] |
| 316 | + statement_start = None |
| 317 | + elif (previous := lines[-1].rstrip("\r\n")).endswith("\\"): |
| 318 | + # PyREPL wraps input with a backslash and no prompt on the |
| 319 | + # next screen row. Rejoin it without the display backslash. |
| 320 | + lines[-1] = previous[:-1] + line |
| 321 | + else: |
| 322 | + # Output (including a traceback) ends the input statement. |
| 323 | + statement_start = None |
| 324 | + return "".join(lines) |
| 325 | + |
| 326 | + |
| 327 | +class paste_mode(commands.paste_mode): |
| 328 | + def do(self) -> None: |
| 329 | + super().do() |
| 330 | + r: ReadlineAlikeReader = self.reader # type: ignore[assignment] |
| 331 | + if r.paste_mode and r.can_strip_repl_prompts: |
| 332 | + r.buffer[:] = _strip_repl_prompts(r.get_unicode()) |
| 333 | + r.pos = len(r.buffer) |
| 334 | + r.invalidate_buffer(from_pos=0) |
| 335 | + |
| 336 | + |
| 337 | +class perform_bracketed_paste(commands.perform_bracketed_paste): |
| 338 | + def process_paste(self, data: str) -> None: |
| 339 | + r: ReadlineAlikeReader = self.reader # type: ignore[assignment] |
| 340 | + if r.paste_repl_sessions and _is_repl_session(data): |
| 341 | + if r.paste_mode: |
| 342 | + data = _strip_repl_prompts(data) |
| 343 | + else: |
| 344 | + r.error(_REPL_PASTE_HINT) |
| 345 | + super().process_paste(data) |
| 346 | + |
| 347 | + |
292 | 348 | class maybe_accept(commands.Command): |
293 | 349 | def do(self) -> None: |
294 | 350 | r: ReadlineAlikeReader |
|
0 commit comments