summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJonathan Slenders <jonathan@slenders.be>2016-07-21 22:34:07 +0200
committerJonathan Slenders <jonathan@slenders.be>2016-07-21 22:34:07 +0200
commit153e2a1af915314506fa31aae500b66eaec39580 (patch)
tree7468e15a07194e144862226eeafd538346eeb63c
parentc6f29d94aca24513c847196df7ed1b4e1b21ce9f (diff)
Bugfix in emacs key bindings. (Crash when Left/Right was pressed with a negative argument.)
-rw-r--r--prompt_toolkit/document.py6
-rw-r--r--tests/test_cli.py26
2 files changed, 31 insertions, 1 deletions
diff --git a/prompt_toolkit/document.py b/prompt_toolkit/document.py
index ce34820c..6e7c8564 100644
--- a/prompt_toolkit/document.py
+++ b/prompt_toolkit/document.py
@@ -594,12 +594,18 @@ class Document(object):
"""
Relative position for cursor left.
"""
+ if count < 0:
+ return self.get_cursor_right_position(-count)
+
return - min(self.cursor_position_col, count)
def get_cursor_right_position(self, count=1):
"""
Relative position for cursor_right.
"""
+ if count < 0:
+ return self.get_cursor_left_position(-count)
+
return min(count, len(self.current_line_after_cursor))
def get_cursor_up_position(self, count=1, preferred_column=None):
diff --git a/tests/test_cli.py b/tests/test_cli.py
index 1072cfa9..c4289385 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -12,8 +12,9 @@ from prompt_toolkit.eventloop.posix import PosixEventLoop
from prompt_toolkit.history import InMemoryHistory
from prompt_toolkit.input import PipeInput
from prompt_toolkit.interface import CommandLineInterface
-from prompt_toolkit.output import DummyOutput
from prompt_toolkit.key_binding.manager import KeyBindingManager
+from prompt_toolkit.output import DummyOutput
+from prompt_toolkit.terminal.vt100_input import ANSI_SEQUENCES
from functools import partial
import pytest
@@ -280,6 +281,29 @@ def test_emacs_reverse_search():
assert result.text == 'line2 second input'
+def test_emacs_arguments():
+ """
+ Test all Emacs commands with Meta-[0-9] arguments (both positive and
+ negative). No one should crash.
+ """
+ for key in ANSI_SEQUENCES:
+ # Ignore BracketedPaste. This would hang forever, because it waits for
+ # the end sequence.
+ if key != '\x1b[200~':
+ try:
+ # Note: we add an 'X' after the key, because Ctrl-Q (quoted-insert)
+ # expects something to follow. We add an additional \n, because
+ # Ctrl-R and Ctrl-S (reverse-search) expect that.
+ result, cli = _feed_cli_with_input(
+ 'hello\x1b4' + key + 'X\n\n')
+
+ result, cli = _feed_cli_with_input(
+ 'hello\x1b-' + key + 'X\n\n')
+ except KeyboardInterrupt:
+ # This exception should only be raised for Ctrl-C
+ assert key == '\x03'
+
+
def test_vi_cursor_movements():
"""
Test cursor movements with Vi key bindings.