diff options
author | Jonathan Slenders <jonathan@slenders.be> | 2016-12-17 18:15:56 +0100 |
---|---|---|
committer | Jonathan Slenders <jonathan@slenders.be> | 2016-12-17 18:15:56 +0100 |
commit | 35b11f96ca20f6e2c9f71f0509bf0df59f9d9c74 (patch) | |
tree | a6be5311556cc442c20dac8787d17d262bd6c1bf | |
parent | 83499e11189b2b2e35b5f546c6bd7e4bd3f7e831 (diff) |
Added quoted-insert named command.
-rw-r--r-- | prompt_toolkit/interface.py | 3 | ||||
-rw-r--r-- | prompt_toolkit/key_binding/bindings/basic.py | 8 | ||||
-rw-r--r-- | prompt_toolkit/key_binding/bindings/emacs.py | 16 | ||||
-rw-r--r-- | prompt_toolkit/key_binding/bindings/named_commands.py | 11 | ||||
-rw-r--r-- | prompt_toolkit/key_binding/bindings/vi.py | 10 |
5 files changed, 29 insertions, 19 deletions
diff --git a/prompt_toolkit/interface.py b/prompt_toolkit/interface.py index c9807f62..898ac0ed 100644 --- a/prompt_toolkit/interface.py +++ b/prompt_toolkit/interface.py @@ -87,6 +87,9 @@ class CommandLineInterface(object): #: EditingMode.VI or EditingMode.EMACS self.editing_mode = application.editing_mode + #: Quoted insert. This flag is set if we go into quoted insert mode. + self.quoted_insert = False + #: Vi state. (For Vi key bindings.) self.vi_state = ViState() diff --git a/prompt_toolkit/key_binding/bindings/basic.py b/prompt_toolkit/key_binding/bindings/basic.py index feab22d4..5564223b 100644 --- a/prompt_toolkit/key_binding/bindings/basic.py +++ b/prompt_toolkit/key_binding/bindings/basic.py @@ -221,6 +221,14 @@ def load_basic_bindings(): event.current_buffer.insert_text(data) + @handle(Keys.Any, filter=Condition(lambda cli: cli.quoted_insert), eager=True) + def _(event): + """ + Handle quoted insert. + """ + event.current_buffer.insert_text(event.data, overwrite=False) + event.cli.quoted_insert = False + return registry diff --git a/prompt_toolkit/key_binding/bindings/emacs.py b/prompt_toolkit/key_binding/bindings/emacs.py index 0cb7aa22..48b06eb6 100644 --- a/prompt_toolkit/key_binding/bindings/emacs.py +++ b/prompt_toolkit/key_binding/bindings/emacs.py @@ -78,6 +78,11 @@ def load_emacs_bindings(): handle(Keys.Escape, '#', filter=insert_mode)(get_by_name('insert-comment')) handle(Keys.ControlO)(get_by_name('operate-and-get-next')) + # ControlQ does a quoted insert. Not that for vt100 terminals, you have to + # disable flow control by running ``stty -ixon``, otherwise Ctrl-Q and + # Ctrl-S are captured by the terminal. + handle(Keys.ControlQ, filter= ~has_selection)(get_by_name('quoted-insert')) + @handle(Keys.ControlN) def _(event): " Next line. " @@ -88,17 +93,6 @@ def load_emacs_bindings(): " Previous line. " event.current_buffer.auto_up(count=event.arg) - @handle(Keys.ControlQ, Keys.Any, filter= ~has_selection) - def _(event): - """ - Quoted insert. - - For vt100 terminals, you have to disable flow control by running - ``stty -ixon``, otherwise Ctrl-Q and Ctrl-S are captured by the - terminal. - """ - event.current_buffer.insert_text(event.data, overwrite=False) - def handle_digit(c): """ Handle input of arguments. diff --git a/prompt_toolkit/key_binding/bindings/named_commands.py b/prompt_toolkit/key_binding/bindings/named_commands.py index 977da982..f960a495 100644 --- a/prompt_toolkit/key_binding/bindings/named_commands.py +++ b/prompt_toolkit/key_binding/bindings/named_commands.py @@ -138,7 +138,6 @@ def previous_history(event): event.current_buffer.history_backward(count=event.arg) - @register('next-history') def next_history(event): " Move `forward' through the history list, fetching the next command. " @@ -267,6 +266,16 @@ def capitalize_word(event): words = buff.document.text_after_cursor[:pos] buff.insert_text(words.title(), overwrite=True) + +@register('quoted-insert') +def quoted_insert(event): + """ + Add the next character typed to the line verbatim. This is how to insert + key sequences like C-q, for example. + """ + event.cli.quoted_insert = True + + # # Killing and yanking. # diff --git a/prompt_toolkit/key_binding/bindings/vi.py b/prompt_toolkit/key_binding/bindings/vi.py index a16baa26..cf5cdb5a 100644 --- a/prompt_toolkit/key_binding/bindings/vi.py +++ b/prompt_toolkit/key_binding/bindings/vi.py @@ -348,6 +348,9 @@ def load_vi_bindings(get_search_state=None): (('~', ), Condition(lambda cli: cli.vi_state.tilde_operator), lambda string: string.swapcase()), ] + # Insert a character literally (quoted insert). + handle(Keys.ControlV, filter=insert_mode)(get_by_name('quoted-insert')) + @handle(Keys.Escape) def _(event): """ @@ -420,13 +423,6 @@ def load_vi_bindings(get_search_state=None): event.current_buffer.cursor_position += \ event.current_buffer.document.get_cursor_left_position(count=event.arg) - @handle(Keys.ControlV, Keys.Any, filter=insert_mode) - def _(event): - """ - Insert a character literally (quoted insert). - """ - event.current_buffer.insert_text(event.data, overwrite=False) - @handle(Keys.ControlN, filter=insert_mode) def _(event): b = event.current_buffer |