Skip to content

Commit e2170df

Browse files
Bug fix in ControlX-ControlX binding + unit test.
The cursor did not go to the start of the line when we were at the latest line of a multi-line input, or in a single line input.
1 parent de598e9 commit e2170df

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

prompt_toolkit/key_binding/bindings/emacs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def _(event):
198198
"""
199199
buffer = event.current_buffer
200200

201-
if buffer.document.current_char == '\n':
201+
if buffer.document.is_cursor_at_the_end_of_line:
202202
buffer.cursor_position += buffer.document.get_start_of_line_position(after_whitespace=False)
203203
else:
204204
buffer.cursor_position += buffer.document.get_end_of_line_position()

tests/test_cli.py

+15
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,21 @@ def test_emacs_other_bindings():
245245
assert result.text == 'hello X'
246246

247247

248+
def test_controlx_controlx():
249+
# At the end: go to the start of the line.
250+
result, cli = _feed_cli_with_input('hello world\x18\x18X\n')
251+
assert result.text == 'Xhello world'
252+
assert result.cursor_position == 1
253+
254+
# At the start: go to the end of the line.
255+
result, cli = _feed_cli_with_input('hello world\x01\x18\x18X\n')
256+
assert result.text == 'hello worldX'
257+
258+
# Left, Left Control-X Control-X: go to the end of the line.
259+
result, cli = _feed_cli_with_input('hello world\x1b[D\x1b[D\x18\x18X\n')
260+
assert result.text == 'hello worldX'
261+
262+
248263
def test_emacs_history_bindings():
249264
# Adding a new item to the history.
250265
history = _history()

0 commit comments

Comments
 (0)