diff --git a/src/prompt_toolkit/input/vt100_parser.py b/src/prompt_toolkit/input/vt100_parser.py index 34ea11057..6a8cd8bd6 100644 --- a/src/prompt_toolkit/input/vt100_parser.py +++ b/src/prompt_toolkit/input/vt100_parser.py @@ -188,6 +188,12 @@ def _call_handler( self._in_bracketed_paste = True self._paste_buffer = "" else: + # For character-valued ANSI_SEQUENCES entries (plain characters + # that are not Keys members), use the character as insert_text + # instead of the raw escape sequence so that self-insert + # bindings work correctly. + if not isinstance(key, Keys): + insert_text = key self.feed_key_callback(KeyPress(key, insert_text)) def feed(self, data: str) -> None: diff --git a/tests/test_inputstream.py b/tests/test_inputstream.py index ab1b03689..63b5deb0f 100644 --- a/tests/test_inputstream.py +++ b/tests/test_inputstream.py @@ -139,3 +139,26 @@ def test_cpr_response_2(processor, stream): assert len(processor.keys) == 2 assert processor.keys[0].key == Keys.CPRResponse assert processor.keys[1].key == Keys.ControlJ + + +def test_character_valued_ansi_sequence(processor, stream): + """Character-valued ANSI_SEQUENCES entries should use the character as data. + + Regression test for: https://github.com/prompt-toolkit/python-prompt-toolkit/issues/2086 + When a character-valued entry is added to ANSI_SEQUENCES (e.g., for xterm + modifyOtherKeys support), the KeyPress data should be the character, not the + raw escape sequence. + """ + from prompt_toolkit.input.ansi_escape_sequences import ANSI_SEQUENCES + + SEQ = "\x1b[27;2;78~" # Shift+N via xterm modifyOtherKeys=2 + ANSI_SEQUENCES[SEQ] = "N" + + try: + stream.feed(SEQ) + assert len(processor.keys) == 1 + assert processor.keys[0].key == "N" + # data should be the character, not the raw escape sequence + assert processor.keys[0].data == "N" + finally: + del ANSI_SEQUENCES[SEQ]