summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJonathan Slenders <jonathan@slenders.be>2016-12-17 12:28:17 +0100
committerJonathan Slenders <jonathan@slenders.be>2016-12-17 12:28:17 +0100
commit1c2410855a9473ef140c958d3752da52fb4eddcb (patch)
tree290a2866504326fe4509ae972435b72c1f3dd491
parent29233e723d7acf926414178908cc86007993db66 (diff)
Implemented prefix-meta command.
-rw-r--r--prompt_toolkit/key_binding/bindings/named_commands.py14
-rw-r--r--tests/test_cli.py19
2 files changed, 32 insertions, 1 deletions
diff --git a/prompt_toolkit/key_binding/bindings/named_commands.py b/prompt_toolkit/key_binding/bindings/named_commands.py
index c8588492..f72766c7 100644
--- a/prompt_toolkit/key_binding/bindings/named_commands.py
+++ b/prompt_toolkit/key_binding/bindings/named_commands.py
@@ -12,6 +12,8 @@ import six
from .completion import generate_completions
from prompt_toolkit.document import Document
from prompt_toolkit.enums import EditingMode
+from prompt_toolkit.key_binding.input_processor import KeyPress
+from prompt_toolkit.keys import Keys
__all__ = (
'get_by_name',
@@ -466,3 +468,15 @@ def vi_editing_mode(event):
def emacs_editing_mode(event):
" Switch to Emacs editing mode. "
event.cli.editing_mode = EditingMode.EMACS
+
+
+@register('prefix-meta')
+def prefix_meta(event):
+ """
+ Metafy the next character typed. This is for keyboards without a meta key.
+
+ Sometimes people also want to bind other keys to Meta, e.g. 'jj'::
+
+ registry.add_key_binding('j', 'j', filter=ViInsertMode())(prefix_meta)
+ """
+ event.cli.input_processor.feed(KeyPress(Keys.Escape))
diff --git a/tests/test_cli.py b/tests/test_cli.py
index 33d55d20..ecab145d 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -28,7 +28,8 @@ def _history():
def _feed_cli_with_input(text, editing_mode=EditingMode.EMACS, clipboard=None,
- history=None, multiline=False, check_line_ending=True):
+ history=None, multiline=False, check_line_ending=True,
+ pre_run_callback=None):
"""
Create a CommandLineInterface, feed it with the given user input and return
the CLI object.
@@ -54,6 +55,10 @@ def _feed_cli_with_input(text, editing_mode=EditingMode.EMACS, clipboard=None,
eventloop=loop,
input=inp,
output=DummyOutput())
+
+ if pre_run_callback:
+ pre_run_callback(cli)
+
result = cli.run()
return result, cli
finally:
@@ -423,6 +428,18 @@ def test_emacs_insert_comment():
assert result.text == '#hello\n#world'
+def test_prefix_meta():
+ # Test the prefix-meta command.
+ def setup_keybindings(cli):
+ from prompt_toolkit.key_binding.bindings.named_commands import prefix_meta
+ from prompt_toolkit.filters import ViInsertMode
+ cli.application.key_bindings_registry.add_binding('j', 'j', filter=ViInsertMode())(prefix_meta)
+
+ result, cli = _feed_cli_with_input(
+ 'hellojjIX\n', pre_run_callback=setup_keybindings, editing_mode=EditingMode.VI)
+ assert result.text == 'Xhello'
+
+
def test_bracketed_paste():
result, cli = _feed_cli_with_input('\x1b[200~hello world\x1b[201~\n')
assert result.text == 'hello world'