diff options
author | Jonathan Slenders <jonathan@slenders.be> | 2016-07-30 16:30:42 +0200 |
---|---|---|
committer | Jonathan Slenders <jonathan@slenders.be> | 2016-07-30 16:30:45 +0200 |
commit | a2fcaec29858dd1c20250c05db849c9eeb485883 (patch) | |
tree | d1c69553b2e07fbd767bac72a9017793e29dd2b7 | |
parent | b8c25314db52796b749d859468c11b61e9628386 (diff) |
Better handling of arguments in Emacs mode.
Pressing sequences like 'esc---123' do now work:
- Repetition of minus signs is ignored.
- No esc prefix required for each number.
-rw-r--r-- | prompt_toolkit/key_binding/bindings/emacs.py | 18 | ||||
-rw-r--r-- | prompt_toolkit/key_binding/input_processor.py | 26 | ||||
-rw-r--r-- | prompt_toolkit/layout/toolbars.py | 6 |
3 files changed, 35 insertions, 15 deletions
diff --git a/prompt_toolkit/key_binding/bindings/emacs.py b/prompt_toolkit/key_binding/bindings/emacs.py index f173b4e2..e7a2e1bf 100644 --- a/prompt_toolkit/key_binding/bindings/emacs.py +++ b/prompt_toolkit/key_binding/bindings/emacs.py @@ -3,7 +3,7 @@ from __future__ import unicode_literals from prompt_toolkit.buffer import SelectionType, indent, unindent from prompt_toolkit.keys import Keys from prompt_toolkit.enums import IncrementalSearchDirection, SEARCH_BUFFER, SYSTEM_BUFFER -from prompt_toolkit.filters import Always, Condition, EmacsMode, to_cli_filter, HasSelection, EmacsInsertMode, HasFocus +from prompt_toolkit.filters import Always, Condition, EmacsMode, to_cli_filter, HasSelection, EmacsInsertMode, HasFocus, HasArg from prompt_toolkit.completion import CompleteEvent from .utils import create_handle_decorator @@ -109,22 +109,32 @@ def load_emacs_bindings(registry, filter=Always()): def handle_digit(c): """ - Handle Alt + digit in the `meta_digit` method. + Handle input of arguments. + The first number needs to be preceeded by escape. """ - @handle(Keys.Escape, c) + @handle(c, filter=HasArg()) + @handle(Keys.Escape, c, filter=~HasArg()) def _(event): event.append_to_arg_count(c) for c in '0123456789': handle_digit(c) - @handle(Keys.Escape, '-') + @handle(Keys.Escape, '-', filter=~HasArg()) def _(event): """ """ if event._arg is None: event.append_to_arg_count('-') + @handle('-', filter=Condition(lambda cli: cli.input_processor.arg == '-')) + def _(event): + """ + When '-' is typed again, after exactly '-' has been given as an + argument, ignore this. + """ + event.cli.input_processor.arg = '-' + is_returnable = Condition( lambda cli: cli.current_buffer.accept_action.is_returnable) diff --git a/prompt_toolkit/key_binding/input_processor.py b/prompt_toolkit/key_binding/input_processor.py index d8fe4300..c23c76db 100644 --- a/prompt_toolkit/key_binding/input_processor.py +++ b/prompt_toolkit/key_binding/input_processor.py @@ -310,7 +310,16 @@ class KeyPressEvent(object): """ Repetition argument. """ - return self._arg or 1 + if self._arg == '-': + return -1 + + result = int(self._arg or 1) + + # Don't exceed a million. + if int(result) >= 1000000: + result = 1 + + return result def append_to_arg_count(self, data): """ @@ -321,15 +330,12 @@ class KeyPressEvent(object): assert data in '-0123456789' current = self._arg - if current is None: - if data == '-': - data = '-1' - result = int(data) + if data == '-': + assert current is None or current == '-' + result = data + elif current is None: + result = data else: - result = int("%s%s" % (current, data)) - - # Don't exceed a million. - if int(result) >= 1000000: - result = None + result = "%s%s" % (current, data) self.input_processor.arg = result diff --git a/prompt_toolkit/layout/toolbars.py b/prompt_toolkit/layout/toolbars.py index c5885b11..2e77c2fa 100644 --- a/prompt_toolkit/layout/toolbars.py +++ b/prompt_toolkit/layout/toolbars.py @@ -56,9 +56,13 @@ class SystemToolbar(ConditionalContainer): class ArgToolbarControl(TokenListControl): def __init__(self): def get_tokens(cli): + arg = cli.input_processor.arg + if arg == '-': + arg = '-1' + return [ (Token.Toolbar.Arg, 'Repeat: '), - (Token.Toolbar.Arg.Text, str(cli.input_processor.arg)), + (Token.Toolbar.Arg.Text, arg), ] super(ArgToolbarControl, self).__init__(get_tokens) |