diff options
author | shadchin <[email protected]> | 2022-02-10 16:44:30 +0300 |
---|---|---|
committer | Daniil Cherednik <[email protected]> | 2022-02-10 16:44:30 +0300 |
commit | 2598ef1d0aee359b4b6d5fdd1758916d5907d04f (patch) | |
tree | 012bb94d777798f1f56ac1cec429509766d05181 /contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding | |
parent | 6751af0b0c1b952fede40b19b71da8025b5d8bcf (diff) |
Restoring authorship annotation for <[email protected]>. Commit 1 of 2.
Diffstat (limited to 'contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding')
8 files changed, 855 insertions, 855 deletions
diff --git a/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/bindings/basic.py b/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/bindings/basic.py index 401135dec06..6b36bc30f5c 100644 --- a/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/bindings/basic.py +++ b/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/bindings/basic.py @@ -2,7 +2,7 @@ from __future__ import unicode_literals from prompt_toolkit.enums import DEFAULT_BUFFER -from prompt_toolkit.filters import HasSelection, Condition, EmacsInsertMode, ViInsertMode +from prompt_toolkit.filters import HasSelection, Condition, EmacsInsertMode, ViInsertMode from prompt_toolkit.keys import Keys from prompt_toolkit.layout.screen import Point from prompt_toolkit.mouse_events import MouseEventType, MouseEvent @@ -10,7 +10,7 @@ from prompt_toolkit.renderer import HeightIsUnknownError from prompt_toolkit.utils import suspend_to_background_supported, is_windows from .named_commands import get_by_name -from ..registry import Registry +from ..registry import Registry __all__ = ( @@ -26,10 +26,10 @@ def if_no_repeat(event): return not event.is_repeat -def load_basic_bindings(): - registry = Registry() +def load_basic_bindings(): + registry = Registry() insert_mode = ViInsertMode() | EmacsInsertMode() - handle = registry.add_binding + handle = registry.add_binding has_selection = HasSelection() @handle(Keys.ControlA) @@ -141,8 +141,8 @@ def load_basic_bindings(): get_by_name('self-insert')) handle(Keys.ControlT, filter=insert_mode)(get_by_name('transpose-chars')) handle(Keys.ControlW, filter=insert_mode)(get_by_name('unix-word-rubout')) - handle(Keys.ControlI, filter=insert_mode)(get_by_name('menu-complete')) - handle(Keys.BackTab, filter=insert_mode)(get_by_name('menu-complete-backward')) + handle(Keys.ControlI, filter=insert_mode)(get_by_name('menu-complete')) + handle(Keys.BackTab, filter=insert_mode)(get_by_name('menu-complete-backward')) handle(Keys.PageUp, filter= ~has_selection)(get_by_name('previous-history')) handle(Keys.PageDown, filter= ~has_selection)(get_by_name('next-history')) @@ -181,7 +181,7 @@ def load_basic_bindings(): data = event.current_buffer.cut_selection() event.cli.clipboard.set_data(data) - # Global bindings. + # Global bindings. @handle(Keys.ControlZ) def _(event): @@ -196,7 +196,7 @@ def load_basic_bindings(): """ event.current_buffer.insert_text(event.data) - @handle(Keys.CPRResponse, save_before=lambda e: False) + @handle(Keys.CPRResponse, save_before=lambda e: False) def _(event): """ Handle incoming Cursor-Position-Request response. @@ -208,7 +208,7 @@ def load_basic_bindings(): # Report absolute cursor position to the renderer. event.cli.renderer.report_absolute_cursor_row(row) - @handle(Keys.BracketedPaste) + @handle(Keys.BracketedPaste) def _(event): " Pasting from clipboard. " data = event.data @@ -221,24 +221,24 @@ 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 - - -def load_mouse_bindings(): + @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 + + +def load_mouse_bindings(): """ Key bindings, required for mouse support. (Mouse events enter through the key binding system.) """ - registry = Registry() - + registry = Registry() + @registry.add_binding(Keys.Vt100MouseEvent) def _(event): """ @@ -335,15 +335,15 @@ def load_mouse_bindings(): handler(event.cli, MouseEvent(position=Point(x=x, y=y), event_type=event_type)) - return registry - + return registry -def load_abort_and_exit_bindings(): + +def load_abort_and_exit_bindings(): """ Basic bindings for abort (Ctrl-C) and exit (Ctrl-D). """ - registry = Registry() - handle = registry.add_binding + registry = Registry() + handle = registry.add_binding @handle(Keys.ControlC) def _(event): @@ -359,34 +359,34 @@ def load_abort_and_exit_bindings(): handle(Keys.ControlD, filter=ctrl_d_condition)(get_by_name('end-of-file')) - return registry - + return registry -def load_basic_system_bindings(): + +def load_basic_system_bindings(): """ Basic system bindings (For both Emacs and Vi mode.) """ - registry = Registry() + registry = Registry() suspend_supported = Condition( lambda cli: suspend_to_background_supported()) - @registry.add_binding(Keys.ControlZ, filter=suspend_supported) + @registry.add_binding(Keys.ControlZ, filter=suspend_supported) def _(event): """ Suspend process to background. """ event.cli.suspend_to_background() - return registry + return registry - -def load_auto_suggestion_bindings(): + +def load_auto_suggestion_bindings(): """ Key bindings for accepting auto suggestion text. """ - registry = Registry() - handle = registry.add_binding + registry = Registry() + handle = registry.add_binding suggestion_available = Condition( lambda cli: @@ -403,5 +403,5 @@ def load_auto_suggestion_bindings(): if suggestion: b.insert_text(suggestion.text) - - return registry + + return registry diff --git a/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/bindings/emacs.py b/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/bindings/emacs.py index bccdb04ff38..0f4ea37b01c 100644 --- a/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/bindings/emacs.py +++ b/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/bindings/emacs.py @@ -3,12 +3,12 @@ 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 Condition, EmacsMode, HasSelection, EmacsInsertMode, HasFocus, HasArg +from prompt_toolkit.filters import Condition, EmacsMode, HasSelection, EmacsInsertMode, HasFocus, HasArg from prompt_toolkit.completion import CompleteEvent from .scroll import scroll_page_up, scroll_page_down from .named_commands import get_by_name -from ..registry import Registry, ConditionalRegistry +from ..registry import Registry, ConditionalRegistry __all__ = ( 'load_emacs_bindings', @@ -18,14 +18,14 @@ __all__ = ( ) -def load_emacs_bindings(): +def load_emacs_bindings(): """ Some e-macs extensions. """ # Overview of Readline emacs commands: # http://www.catonmat.net/download/readline-emacs-editing-mode-cheat-sheet.pdf - registry = ConditionalRegistry(Registry(), EmacsMode()) - handle = registry.add_binding + registry = ConditionalRegistry(Registry(), EmacsMode()) + handle = registry.add_binding insert_mode = EmacsInsertMode() has_selection = HasSelection() @@ -57,9 +57,9 @@ def load_emacs_bindings(): handle(Keys.Escape, 'f')(get_by_name('forward-word')) handle(Keys.Escape, 'l', filter=insert_mode)(get_by_name('downcase-word')) handle(Keys.Escape, 'u', filter=insert_mode)(get_by_name('uppercase-word')) - handle(Keys.Escape, 'y', filter=insert_mode)(get_by_name('yank-pop')) - handle(Keys.Escape, Keys.ControlH, filter=insert_mode)(get_by_name('backward-kill-word')) - handle(Keys.Escape, Keys.Backspace, filter=insert_mode)(get_by_name('backward-kill-word')) + handle(Keys.Escape, 'y', filter=insert_mode)(get_by_name('yank-pop')) + handle(Keys.Escape, Keys.ControlH, filter=insert_mode)(get_by_name('backward-kill-word')) + handle(Keys.Escape, Keys.Backspace, filter=insert_mode)(get_by_name('backward-kill-word')) handle(Keys.Escape, '\\', filter=insert_mode)(get_by_name('delete-horizontal-space')) handle(Keys.ControlUnderscore, save_before=(lambda e: False), filter=insert_mode)( @@ -75,18 +75,18 @@ def load_emacs_bindings(): handle(Keys.Escape, '.', filter=insert_mode)(get_by_name('yank-last-arg')) handle(Keys.Escape, '_', filter=insert_mode)(get_by_name('yank-last-arg')) handle(Keys.Escape, Keys.ControlY, filter=insert_mode)(get_by_name('yank-nth-arg')) - 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.ControlX, '(')(get_by_name('start-kbd-macro')) - handle(Keys.ControlX, ')')(get_by_name('end-kbd-macro')) - handle(Keys.ControlX, 'e')(get_by_name('call-last-kbd-macro')) - + 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.ControlX, '(')(get_by_name('start-kbd-macro')) + handle(Keys.ControlX, ')')(get_by_name('end-kbd-macro')) + handle(Keys.ControlX, 'e')(get_by_name('call-last-kbd-macro')) + @handle(Keys.ControlN) def _(event): " Next line. " @@ -144,13 +144,13 @@ def load_emacs_bindings(): @handle(Keys.ControlSquareClose, Keys.Any) def _(event): " When Ctl-] + a character is pressed. go to that character. " - # Also named 'character-search' + # Also named 'character-search' character_search(event.current_buffer, event.data, event.arg) @handle(Keys.Escape, Keys.ControlSquareClose, Keys.Any) def _(event): " Like Ctl-], but backwards. " - # Also named 'character-search-backward' + # Also named 'character-search-backward' character_search(event.current_buffer, event.data, -event.arg) @handle(Keys.Escape, 'a') @@ -296,26 +296,26 @@ def load_emacs_bindings(): unindent(buffer, from_, to + 1, count=event.arg) - return registry + return registry - -def load_emacs_open_in_editor_bindings(): + +def load_emacs_open_in_editor_bindings(): """ Pressing C-X C-E will open the buffer in an external editor. """ - registry = Registry() - - registry.add_binding(Keys.ControlX, Keys.ControlE, - filter=EmacsMode() & ~HasSelection())( - get_by_name('edit-and-execute-command')) + registry = Registry() - return registry + registry.add_binding(Keys.ControlX, Keys.ControlE, + filter=EmacsMode() & ~HasSelection())( + get_by_name('edit-and-execute-command')) + return registry -def load_emacs_system_bindings(): - registry = ConditionalRegistry(Registry(), EmacsMode()) - handle = registry.add_binding - + +def load_emacs_system_bindings(): + registry = ConditionalRegistry(Registry(), EmacsMode()) + handle = registry.add_binding + has_focus = HasFocus(SYSTEM_BUFFER) @handle(Keys.Escape, '!', filter= ~has_focus) @@ -347,13 +347,13 @@ def load_emacs_system_bindings(): # Focus previous buffer again. event.cli.pop_focus() - return registry - + return registry -def load_emacs_search_bindings(get_search_state=None): - registry = ConditionalRegistry(Registry(), EmacsMode()) - handle = registry.add_binding +def load_emacs_search_bindings(get_search_state=None): + registry = ConditionalRegistry(Registry(), EmacsMode()) + handle = registry.add_binding + has_focus = HasFocus(SEARCH_BUFFER) assert get_search_state is None or callable(get_search_state) @@ -375,7 +375,7 @@ def load_emacs_search_bindings(get_search_state=None): event.cli.pop_focus() @handle(Keys.ControlJ, filter=has_focus) - @handle(Keys.Escape, filter=has_focus, eager=True) + @handle(Keys.Escape, filter=has_focus, eager=True) def _(event): """ When enter pressed in isearch, quit isearch mode. (Multiline @@ -433,20 +433,20 @@ def load_emacs_search_bindings(get_search_state=None): def _(event): incremental_search(event.cli, IncrementalSearchDirection.FORWARD, count=event.arg) - return registry + return registry - -def load_extra_emacs_page_navigation_bindings(): + +def load_extra_emacs_page_navigation_bindings(): """ Key bindings, for scrolling up and down through pages. This are separate bindings, because GNU readline doesn't have them. """ - registry = ConditionalRegistry(Registry(), EmacsMode()) - handle = registry.add_binding + registry = ConditionalRegistry(Registry(), EmacsMode()) + handle = registry.add_binding handle(Keys.ControlV)(scroll_page_down) handle(Keys.PageDown)(scroll_page_down) handle(Keys.Escape, 'v')(scroll_page_up) handle(Keys.PageUp)(scroll_page_up) - - return registry + + return registry diff --git a/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/bindings/named_commands.py b/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/bindings/named_commands.py index f80c439fc6d..d045b1268f9 100644 --- a/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/bindings/named_commands.py +++ b/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/bindings/named_commands.py @@ -5,15 +5,15 @@ See: http://www.delorie.com/gnu/docs/readline/rlman_13.html """ from __future__ import unicode_literals from prompt_toolkit.enums import IncrementalSearchDirection, SEARCH_BUFFER -from prompt_toolkit.selection import PasteMode +from prompt_toolkit.selection import PasteMode from six.moves import range import six -from .completion import generate_completions, display_completions_like_readline -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 +from .completion import generate_completions, display_completions_like_readline +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', @@ -134,13 +134,13 @@ def accept_line(event): @register('previous-history') def previous_history(event): - " Move `back` through the history list, fetching the previous command. " + " Move `back` through the history list, fetching the previous command. " 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. " + " Move `forward` through the history list, fetching the next command. " event.current_buffer.history_forward(count=event.arg) @@ -163,13 +163,13 @@ def end_of_history(event): @register('reverse-search-history') def reverse_search_history(event): """ - Search backward starting at the current line and moving `up` through + Search backward starting at the current line and moving `up` through the history as necessary. This is an incremental search. """ event.cli.current_search_state.direction = IncrementalSearchDirection.BACKWARD event.cli.push_focus(SEARCH_BUFFER) - + # # Commands for changing text # @@ -267,16 +267,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 - - + +@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. # @@ -316,13 +316,13 @@ def kill_word(event): @register('unix-word-rubout') -def unix_word_rubout(event, WORD=True): +def unix_word_rubout(event, WORD=True): """ - Kill the word behind point, using whitespace as a word boundary. - Usually bound to ControlW. + Kill the word behind point, using whitespace as a word boundary. + Usually bound to ControlW. """ buff = event.current_buffer - pos = buff.document.find_start_of_previous_word(count=event.arg, WORD=WORD) + pos = buff.document.find_start_of_previous_word(count=event.arg, WORD=WORD) if pos is None: # Nothing found? delete until the start of the document. (The @@ -344,15 +344,15 @@ def unix_word_rubout(event, WORD=True): event.cli.output.bell() -@register('backward-kill-word') -def backward_kill_word(event): - """ - Kills the word before point, using "not a letter nor a digit" as a word boundary. - Usually bound to M-Del or M-Backspace. - """ - unix_word_rubout(event, WORD=False) - - +@register('backward-kill-word') +def backward_kill_word(event): + """ + Kills the word before point, using "not a letter nor a digit" as a word boundary. + Usually bound to M-Del or M-Backspace. + """ + unix_word_rubout(event, WORD=False) + + @register('delete-horizontal-space') def delete_horizontal_space(event): " Delete all spaces and tabs around point. " @@ -387,7 +387,7 @@ def yank(event): Paste before cursor. """ event.current_buffer.paste_clipboard_data( - event.cli.clipboard.get_data(), count=event.arg, paste_mode=PasteMode.EMACS) + event.cli.clipboard.get_data(), count=event.arg, paste_mode=PasteMode.EMACS) @register('yank-nth-arg') def yank_nth_arg(event): @@ -408,86 +408,86 @@ def yank_last_arg(event): n = (event.arg if event.arg_present else None) event.current_buffer.yank_last_arg(n) -@register('yank-pop') -def yank_pop(event): - """ - Rotate the kill ring, and yank the new top. Only works following yank or - yank-pop. - """ - buff = event.current_buffer - doc_before_paste = buff.document_before_paste - clipboard = event.cli.clipboard - - if doc_before_paste is not None: - buff.document = doc_before_paste - clipboard.rotate() - buff.paste_clipboard_data( - clipboard.get_data(), paste_mode=PasteMode.EMACS) - +@register('yank-pop') +def yank_pop(event): + """ + Rotate the kill ring, and yank the new top. Only works following yank or + yank-pop. + """ + buff = event.current_buffer + doc_before_paste = buff.document_before_paste + clipboard = event.cli.clipboard + + if doc_before_paste is not None: + buff.document = doc_before_paste + clipboard.rotate() + buff.paste_clipboard_data( + clipboard.get_data(), paste_mode=PasteMode.EMACS) + # # Completion. # @register('complete') def complete(event): - " Attempt to perform completion. " - display_completions_like_readline(event) - - -@register('menu-complete') -def menu_complete(event): - """ - Generate completions, or go to the next completion. (This is the default - way of completing input in prompt_toolkit.) - """ + " Attempt to perform completion. " + display_completions_like_readline(event) + + +@register('menu-complete') +def menu_complete(event): + """ + Generate completions, or go to the next completion. (This is the default + way of completing input in prompt_toolkit.) + """ generate_completions(event) -@register('menu-complete-backward') -def menu_complete_backward(event): - " Move backward through the list of possible completions. " - event.current_buffer.complete_previous() - -# -# Keyboard macros. -# - -@register('start-kbd-macro') -def start_kbd_macro(event): - """ - Begin saving the characters typed into the current keyboard macro. - """ - event.cli.input_processor.start_macro() - - -@register('end-kbd-macro') -def start_kbd_macro(event): - """ - Stop saving the characters typed into the current keyboard macro and save - the definition. - """ - event.cli.input_processor.end_macro() - - -@register('call-last-kbd-macro') -def start_kbd_macro(event): - """ - Re-execute the last keyboard macro defined, by making the characters in the - macro appear as if typed at the keyboard. - """ - event.cli.input_processor.call_macro() - - -@register('print-last-kbd-macro') -def print_last_kbd_macro(event): - " Print the last keboard macro. " - # TODO: Make the format suitable for the inputrc file. - def print_macro(): - for k in event.cli.input_processor.macro: - print(k) - event.cli.run_in_terminal(print_macro) - +@register('menu-complete-backward') +def menu_complete_backward(event): + " Move backward through the list of possible completions. " + event.current_buffer.complete_previous() + # +# Keyboard macros. +# + +@register('start-kbd-macro') +def start_kbd_macro(event): + """ + Begin saving the characters typed into the current keyboard macro. + """ + event.cli.input_processor.start_macro() + + +@register('end-kbd-macro') +def start_kbd_macro(event): + """ + Stop saving the characters typed into the current keyboard macro and save + the definition. + """ + event.cli.input_processor.end_macro() + + +@register('call-last-kbd-macro') +def start_kbd_macro(event): + """ + Re-execute the last keyboard macro defined, by making the characters in the + macro appear as if typed at the keyboard. + """ + event.cli.input_processor.call_macro() + + +@register('print-last-kbd-macro') +def print_last_kbd_macro(event): + " Print the last keboard macro. " + # TODO: Make the format suitable for the inputrc file. + def print_macro(): + for k in event.cli.input_processor.macro: + print(k) + event.cli.run_in_terminal(print_macro) + +# # Miscellaneous Commands. # @@ -495,84 +495,84 @@ def print_last_kbd_macro(event): def undo(event): " Incremental undo. " event.current_buffer.undo() - - -@register('insert-comment') -def insert_comment(event): - """ - Without numeric argument, comment all lines. - With numeric argument, uncomment all lines. - In any case accept the input. - """ - buff = event.current_buffer - - # Transform all lines. - if event.arg != 1: - def change(line): - return line[1:] if line.startswith('#') else line - else: - def change(line): - return '#' + line - - buff.document = Document( - text='\n'.join(map(change, buff.text.splitlines())), - cursor_position=0) - - # Accept input. - buff.accept_action.validate_and_handle(event.cli, buff) - - -@register('vi-editing-mode') -def vi_editing_mode(event): - " Switch to Vi editing mode. " - event.cli.editing_mode = EditingMode.VI - - -@register('emacs-editing-mode') -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)) - - -@register('operate-and-get-next') -def operate_and_get_next(event): - """ - Accept the current line for execution and fetch the next line relative to - the current line from the history for editing. - """ - buff = event.current_buffer - new_index = buff.working_index + 1 - - # Accept the current input. (This will also redraw the interface in the - # 'done' state.) - buff.accept_action.validate_and_handle(event.cli, buff) - - # Set the new index at the start of the next run. - def set_working_index(): - if new_index < len(buff._working_lines): - buff.working_index = new_index - - event.cli.pre_run_callables.append(set_working_index) - - -@register('edit-and-execute-command') -def edit_and_execute(event): - """ - Invoke an editor on the current command line, and accept the result. - """ - buff = event.current_buffer - - buff.open_in_editor(event.cli) - buff.accept_action.validate_and_handle(event.cli, buff) + + +@register('insert-comment') +def insert_comment(event): + """ + Without numeric argument, comment all lines. + With numeric argument, uncomment all lines. + In any case accept the input. + """ + buff = event.current_buffer + + # Transform all lines. + if event.arg != 1: + def change(line): + return line[1:] if line.startswith('#') else line + else: + def change(line): + return '#' + line + + buff.document = Document( + text='\n'.join(map(change, buff.text.splitlines())), + cursor_position=0) + + # Accept input. + buff.accept_action.validate_and_handle(event.cli, buff) + + +@register('vi-editing-mode') +def vi_editing_mode(event): + " Switch to Vi editing mode. " + event.cli.editing_mode = EditingMode.VI + + +@register('emacs-editing-mode') +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)) + + +@register('operate-and-get-next') +def operate_and_get_next(event): + """ + Accept the current line for execution and fetch the next line relative to + the current line from the history for editing. + """ + buff = event.current_buffer + new_index = buff.working_index + 1 + + # Accept the current input. (This will also redraw the interface in the + # 'done' state.) + buff.accept_action.validate_and_handle(event.cli, buff) + + # Set the new index at the start of the next run. + def set_working_index(): + if new_index < len(buff._working_lines): + buff.working_index = new_index + + event.cli.pre_run_callables.append(set_working_index) + + +@register('edit-and-execute-command') +def edit_and_execute(event): + """ + Invoke an editor on the current command line, and accept the result. + """ + buff = event.current_buffer + + buff.open_in_editor(event.cli) + buff.accept_action.validate_and_handle(event.cli, buff) diff --git a/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/bindings/vi.py b/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/bindings/vi.py index 72568ee273d..25133196af4 100644 --- a/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/bindings/vi.py +++ b/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/bindings/vi.py @@ -3,17 +3,17 @@ from __future__ import unicode_literals from prompt_toolkit.buffer import ClipboardData, indent, unindent, reshape_text from prompt_toolkit.document import Document from prompt_toolkit.enums import IncrementalSearchDirection, SEARCH_BUFFER, SYSTEM_BUFFER -from prompt_toolkit.filters import Filter, Condition, HasArg, Always, IsReadOnly +from prompt_toolkit.filters import Filter, Condition, HasArg, Always, IsReadOnly from prompt_toolkit.filters.cli import ViNavigationMode, ViInsertMode, ViInsertMultipleMode, ViReplaceMode, ViSelectionMode, ViWaitingForTextObjectMode, ViDigraphMode, ViMode from prompt_toolkit.key_binding.digraphs import DIGRAPHS from prompt_toolkit.key_binding.vi_state import CharacterFind, InputMode from prompt_toolkit.keys import Keys from prompt_toolkit.layout.utils import find_window_for_buffer_name -from prompt_toolkit.selection import SelectionType, SelectionState, PasteMode +from prompt_toolkit.selection import SelectionType, SelectionState, PasteMode from .scroll import scroll_forward, scroll_backward, scroll_half_page_up, scroll_half_page_down, scroll_one_line_up, scroll_one_line_down, scroll_page_up, scroll_page_down -from .named_commands import get_by_name -from ..registry import Registry, ConditionalRegistry, BaseRegistry +from .named_commands import get_by_name +from ..registry import Registry, ConditionalRegistry, BaseRegistry import prompt_toolkit.filters as filters from six.moves import range @@ -137,168 +137,168 @@ class TextObject(object): return new_document, clipboard_data -def create_text_object_decorator(registry): - """ - Create a decorator that can be used to register Vi text object implementations. - """ - assert isinstance(registry, BaseRegistry) - - operator_given = ViWaitingForTextObjectMode() - navigation_mode = ViNavigationMode() - selection_mode = ViSelectionMode() - - def text_object_decorator(*keys, **kw): - """ - Register a text object function. - - Usage:: - - @text_object('w', filter=..., no_move_handler=False) - def handler(event): - # Return a text object for this key. - return TextObject(...) - - :param no_move_handler: Disable the move handler in navigation mode. - (It's still active in selection mode.) - """ - filter = kw.pop('filter', Always()) - no_move_handler = kw.pop('no_move_handler', False) - no_selection_handler = kw.pop('no_selection_handler', False) - eager = kw.pop('eager', False) - assert not kw - - def decorator(text_object_func): - assert callable(text_object_func) - - @registry.add_binding(*keys, filter=operator_given & filter, eager=eager) - def _(event): - # Arguments are multiplied. - vi_state = event.cli.vi_state - event._arg = (vi_state.operator_arg or 1) * (event.arg or 1) - - # Call the text object handler. - text_obj = text_object_func(event) - if text_obj is not None: - assert isinstance(text_obj, TextObject) - - # Call the operator function with the text object. - vi_state.operator_func(event, text_obj) - - # Clear operator. - event.cli.vi_state.operator_func = None - event.cli.vi_state.operator_arg = None - - # Register a move operation. (Doesn't need an operator.) - if not no_move_handler: - @registry.add_binding(*keys, filter=~operator_given & filter & navigation_mode, eager=eager) - def _(event): - " Move handler for navigation mode. " - text_object = text_object_func(event) - event.current_buffer.cursor_position += text_object.start - - # Register a move selection operation. - if not no_selection_handler: - @registry.add_binding(*keys, filter=~operator_given & filter & selection_mode, eager=eager) - def _(event): - " Move handler for selection mode. " - text_object = text_object_func(event) - buff = event.current_buffer - - # When the text object has both a start and end position, like 'i(' or 'iw', - # Turn this into a selection, otherwise the cursor. - if text_object.end: - # Take selection positions from text object. - start, end = text_object.operator_range(buff.document) - start += buff.cursor_position - end += buff.cursor_position - - buff.selection_state.original_cursor_position = start - buff.cursor_position = end - - # Take selection type from text object. - if text_object.type == TextObjectType.LINEWISE: - buff.selection_state.type = SelectionType.LINES - else: - buff.selection_state.type = SelectionType.CHARACTERS - else: - event.current_buffer.cursor_position += text_object.start - - # Make it possible to chain @text_object decorators. - return text_object_func - - return decorator - return text_object_decorator - - -def create_operator_decorator(registry): - """ - Create a decorator that can be used for registering Vi operators. - """ - assert isinstance(registry, BaseRegistry) - - operator_given = ViWaitingForTextObjectMode() - navigation_mode = ViNavigationMode() - selection_mode = ViSelectionMode() - - def operator_decorator(*keys, **kw): - """ - Register a Vi operator. - - Usage:: - - @operator('d', filter=...) - def handler(cli, text_object): - # Do something with the text object here. - """ - filter = kw.pop('filter', Always()) - eager = kw.pop('eager', False) - assert not kw - - def decorator(operator_func): - @registry.add_binding(*keys, filter=~operator_given & filter & navigation_mode, eager=eager) - def _(event): - """ - Handle operator in navigation mode. - """ - # When this key binding is matched, only set the operator - # function in the ViState. We should execute it after a text - # object has been received. - event.cli.vi_state.operator_func = operator_func - event.cli.vi_state.operator_arg = event.arg - - @registry.add_binding(*keys, filter=~operator_given & filter & selection_mode, eager=eager) - def _(event): - """ - Handle operator in selection mode. - """ - buff = event.current_buffer - selection_state = buff.selection_state - - # Create text object from selection. - if selection_state.type == SelectionType.LINES: - text_obj_type = TextObjectType.LINEWISE - elif selection_state.type == SelectionType.BLOCK: - text_obj_type = TextObjectType.BLOCK - else: - text_obj_type = TextObjectType.INCLUSIVE - - text_object = TextObject( - selection_state.original_cursor_position - buff.cursor_position, - type=text_obj_type) - - # Execute operator. - operator_func(event, text_object) - - # Quit selection mode. - buff.selection_state = None - - return operator_func - return decorator - return operator_decorator - - -def load_vi_bindings(get_search_state=None): +def create_text_object_decorator(registry): """ + Create a decorator that can be used to register Vi text object implementations. + """ + assert isinstance(registry, BaseRegistry) + + operator_given = ViWaitingForTextObjectMode() + navigation_mode = ViNavigationMode() + selection_mode = ViSelectionMode() + + def text_object_decorator(*keys, **kw): + """ + Register a text object function. + + Usage:: + + @text_object('w', filter=..., no_move_handler=False) + def handler(event): + # Return a text object for this key. + return TextObject(...) + + :param no_move_handler: Disable the move handler in navigation mode. + (It's still active in selection mode.) + """ + filter = kw.pop('filter', Always()) + no_move_handler = kw.pop('no_move_handler', False) + no_selection_handler = kw.pop('no_selection_handler', False) + eager = kw.pop('eager', False) + assert not kw + + def decorator(text_object_func): + assert callable(text_object_func) + + @registry.add_binding(*keys, filter=operator_given & filter, eager=eager) + def _(event): + # Arguments are multiplied. + vi_state = event.cli.vi_state + event._arg = (vi_state.operator_arg or 1) * (event.arg or 1) + + # Call the text object handler. + text_obj = text_object_func(event) + if text_obj is not None: + assert isinstance(text_obj, TextObject) + + # Call the operator function with the text object. + vi_state.operator_func(event, text_obj) + + # Clear operator. + event.cli.vi_state.operator_func = None + event.cli.vi_state.operator_arg = None + + # Register a move operation. (Doesn't need an operator.) + if not no_move_handler: + @registry.add_binding(*keys, filter=~operator_given & filter & navigation_mode, eager=eager) + def _(event): + " Move handler for navigation mode. " + text_object = text_object_func(event) + event.current_buffer.cursor_position += text_object.start + + # Register a move selection operation. + if not no_selection_handler: + @registry.add_binding(*keys, filter=~operator_given & filter & selection_mode, eager=eager) + def _(event): + " Move handler for selection mode. " + text_object = text_object_func(event) + buff = event.current_buffer + + # When the text object has both a start and end position, like 'i(' or 'iw', + # Turn this into a selection, otherwise the cursor. + if text_object.end: + # Take selection positions from text object. + start, end = text_object.operator_range(buff.document) + start += buff.cursor_position + end += buff.cursor_position + + buff.selection_state.original_cursor_position = start + buff.cursor_position = end + + # Take selection type from text object. + if text_object.type == TextObjectType.LINEWISE: + buff.selection_state.type = SelectionType.LINES + else: + buff.selection_state.type = SelectionType.CHARACTERS + else: + event.current_buffer.cursor_position += text_object.start + + # Make it possible to chain @text_object decorators. + return text_object_func + + return decorator + return text_object_decorator + + +def create_operator_decorator(registry): + """ + Create a decorator that can be used for registering Vi operators. + """ + assert isinstance(registry, BaseRegistry) + + operator_given = ViWaitingForTextObjectMode() + navigation_mode = ViNavigationMode() + selection_mode = ViSelectionMode() + + def operator_decorator(*keys, **kw): + """ + Register a Vi operator. + + Usage:: + + @operator('d', filter=...) + def handler(cli, text_object): + # Do something with the text object here. + """ + filter = kw.pop('filter', Always()) + eager = kw.pop('eager', False) + assert not kw + + def decorator(operator_func): + @registry.add_binding(*keys, filter=~operator_given & filter & navigation_mode, eager=eager) + def _(event): + """ + Handle operator in navigation mode. + """ + # When this key binding is matched, only set the operator + # function in the ViState. We should execute it after a text + # object has been received. + event.cli.vi_state.operator_func = operator_func + event.cli.vi_state.operator_arg = event.arg + + @registry.add_binding(*keys, filter=~operator_given & filter & selection_mode, eager=eager) + def _(event): + """ + Handle operator in selection mode. + """ + buff = event.current_buffer + selection_state = buff.selection_state + + # Create text object from selection. + if selection_state.type == SelectionType.LINES: + text_obj_type = TextObjectType.LINEWISE + elif selection_state.type == SelectionType.BLOCK: + text_obj_type = TextObjectType.BLOCK + else: + text_obj_type = TextObjectType.INCLUSIVE + + text_object = TextObject( + selection_state.original_cursor_position - buff.cursor_position, + type=text_obj_type) + + # Execute operator. + operator_func(event, text_object) + + # Quit selection mode. + buff.selection_state = None + + return operator_func + return decorator + return operator_decorator + + +def load_vi_bindings(get_search_state=None): + """ Vi extensions. # Overview of Readline Vi commands: @@ -316,8 +316,8 @@ def load_vi_bindings(get_search_state=None): # handled correctly. There is no need to add "~IsReadOnly" to all key # bindings that do text manipulation. - registry = ConditionalRegistry(Registry(), ViMode()) - handle = registry.add_binding + registry = ConditionalRegistry(Registry(), ViMode()) + handle = registry.add_binding # Default get_search_state. if get_search_state is None: @@ -348,9 +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')) - + # Insert a character literally (quoted insert). + handle(Keys.ControlV, filter=insert_mode)(get_by_name('quoted-insert')) + @handle(Keys.Escape) def _(event): """ @@ -635,8 +635,8 @@ def load_vi_bindings(get_search_state=None): """ event.current_buffer.paste_clipboard_data( event.cli.clipboard.get_data(), - count=event.arg, - paste_mode=PasteMode.VI_AFTER) + count=event.arg, + paste_mode=PasteMode.VI_AFTER) @handle('P', filter=navigation_mode) def _(event): @@ -645,8 +645,8 @@ def load_vi_bindings(get_search_state=None): """ event.current_buffer.paste_clipboard_data( event.cli.clipboard.get_data(), - count=event.arg, - paste_mode=PasteMode.VI_BEFORE) + count=event.arg, + paste_mode=PasteMode.VI_BEFORE) @handle('"', Keys.Any, 'p', filter=navigation_mode) def _(event): @@ -655,8 +655,8 @@ def load_vi_bindings(get_search_state=None): if c in vi_register_names: data = event.cli.vi_state.named_registers.get(c) if data: - event.current_buffer.paste_clipboard_data( - data, count=event.arg, paste_mode=PasteMode.VI_AFTER) + event.current_buffer.paste_clipboard_data( + data, count=event.arg, paste_mode=PasteMode.VI_AFTER) @handle('"', Keys.Any, 'P', filter=navigation_mode) def _(event): @@ -666,7 +666,7 @@ def load_vi_bindings(get_search_state=None): data = event.cli.vi_state.named_registers.get(c) if data: event.current_buffer.paste_clipboard_data( - data, count=event.arg, paste_mode=PasteMode.VI_BEFORE) + data, count=event.arg, paste_mode=PasteMode.VI_BEFORE) @handle('r', Keys.Any, filter=navigation_mode) def _(event): @@ -723,7 +723,7 @@ def load_vi_bindings(get_search_state=None): else: event.current_buffer.exit_selection() - @handle('v', filter=navigation_mode) + @handle('v', filter=navigation_mode) def _(event): " Enter character selection mode. " event.current_buffer.start_selection(selection_type=SelectionType.CHARACTERS) @@ -910,8 +910,8 @@ def load_vi_bindings(get_search_state=None): # XXX: should become text_object. pass - operator = create_operator_decorator(registry) - text_object = create_text_object_decorator(registry) + operator = create_operator_decorator(registry) + text_object = create_text_object_decorator(registry) @text_object(Keys.Any, filter=operator_given) def _(event): @@ -1705,25 +1705,25 @@ def load_vi_bindings(get_search_state=None): event.cli.vi_state.waiting_for_digraph = False event.cli.vi_state.digraph_symbol1 = None - return registry - + return registry -def load_vi_open_in_editor_bindings(): + +def load_vi_open_in_editor_bindings(): """ Pressing 'v' in navigation mode will open the buffer in an external editor. """ - registry = Registry() + registry = Registry() navigation_mode = ViNavigationMode() - registry.add_binding('v', filter=navigation_mode)( - get_by_name('edit-and-execute-command')) - return registry + registry.add_binding('v', filter=navigation_mode)( + get_by_name('edit-and-execute-command')) + return registry -def load_vi_system_bindings(): - registry = ConditionalRegistry(Registry(), ViMode()) - handle = registry.add_binding - +def load_vi_system_bindings(): + registry = ConditionalRegistry(Registry(), ViMode()) + handle = registry.add_binding + has_focus = filters.HasFocus(SYSTEM_BUFFER) navigation_mode = ViNavigationMode() @@ -1759,28 +1759,28 @@ def load_vi_system_bindings(): # Focus previous buffer again. event.cli.pop_focus() - return registry - + return registry -def load_vi_search_bindings(get_search_state=None, - search_buffer_name=SEARCH_BUFFER): + +def load_vi_search_bindings(get_search_state=None, + search_buffer_name=SEARCH_BUFFER): assert get_search_state is None or callable(get_search_state) if not get_search_state: def get_search_state(cli): return cli.search_state - registry = ConditionalRegistry(Registry(), ViMode()) - handle = registry.add_binding - + registry = ConditionalRegistry(Registry(), ViMode()) + handle = registry.add_binding + has_focus = filters.HasFocus(search_buffer_name) navigation_mode = ViNavigationMode() selection_mode = ViSelectionMode() - reverse_vi_search_direction = Condition( - lambda cli: cli.application.reverse_vi_search_direction(cli)) - - @handle('/', filter=(navigation_mode|selection_mode)&~reverse_vi_search_direction) - @handle('?', filter=(navigation_mode|selection_mode)&reverse_vi_search_direction) + reverse_vi_search_direction = Condition( + lambda cli: cli.application.reverse_vi_search_direction(cli)) + + @handle('/', filter=(navigation_mode|selection_mode)&~reverse_vi_search_direction) + @handle('?', filter=(navigation_mode|selection_mode)&reverse_vi_search_direction) @handle(Keys.ControlS, filter=~has_focus) def _(event): """ @@ -1793,8 +1793,8 @@ def load_vi_search_bindings(get_search_state=None, # Focus search buffer. event.cli.push_focus(search_buffer_name) - @handle('?', filter=(navigation_mode|selection_mode)&~reverse_vi_search_direction) - @handle('/', filter=(navigation_mode|selection_mode)&reverse_vi_search_direction) + @handle('?', filter=(navigation_mode|selection_mode)&~reverse_vi_search_direction) + @handle('/', filter=(navigation_mode|selection_mode)&reverse_vi_search_direction) @handle(Keys.ControlR, filter=~has_focus) def _(event): """ @@ -1808,7 +1808,7 @@ def load_vi_search_bindings(get_search_state=None, event.cli.vi_state.input_mode = InputMode.INSERT @handle(Keys.ControlJ, filter=has_focus) - @handle(Keys.Escape, filter=has_focus) + @handle(Keys.Escape, filter=has_focus) def _(event): """ Apply the search. (At the / or ? prompt.) @@ -1870,16 +1870,16 @@ def load_vi_search_bindings(get_search_state=None, event.cli.pop_focus() event.cli.buffers[search_buffer_name].reset() - return registry - + return registry -def load_extra_vi_page_navigation_bindings(): + +def load_extra_vi_page_navigation_bindings(): """ Key bindings, for scrolling up and down through pages. This are separate bindings, because GNU readline doesn't have them. """ - registry = ConditionalRegistry(Registry(), ViMode()) - handle = registry.add_binding + registry = ConditionalRegistry(Registry(), ViMode()) + handle = registry.add_binding handle(Keys.ControlF)(scroll_forward) handle(Keys.ControlB)(scroll_backward) @@ -1890,9 +1890,9 @@ def load_extra_vi_page_navigation_bindings(): handle(Keys.PageDown)(scroll_page_down) handle(Keys.PageUp)(scroll_page_up) - return registry - + return registry + class ViStateFilter(Filter): " Deprecated! " def __init__(self, get_vi_state, mode): diff --git a/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/defaults.py b/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/defaults.py index fb2c1070f79..a7396b82ccb 100644 --- a/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/defaults.py +++ b/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/defaults.py @@ -1,119 +1,119 @@ -""" -Default key bindings.:: - - registry = load_key_bindings() - app = Application(key_bindings_registry=registry) -""" -from __future__ import unicode_literals -from prompt_toolkit.key_binding.registry import ConditionalRegistry, MergedRegistry -from prompt_toolkit.key_binding.bindings.basic import load_basic_bindings, load_abort_and_exit_bindings, load_basic_system_bindings, load_auto_suggestion_bindings, load_mouse_bindings -from prompt_toolkit.key_binding.bindings.emacs import load_emacs_bindings, load_emacs_system_bindings, load_emacs_search_bindings, load_emacs_open_in_editor_bindings, load_extra_emacs_page_navigation_bindings -from prompt_toolkit.key_binding.bindings.vi import load_vi_bindings, load_vi_system_bindings, load_vi_search_bindings, load_vi_open_in_editor_bindings, load_extra_vi_page_navigation_bindings -from prompt_toolkit.filters import to_cli_filter - -__all__ = ( - 'load_key_bindings', - 'load_key_bindings_for_prompt', -) - - -def load_key_bindings( - get_search_state=None, - enable_abort_and_exit_bindings=False, - enable_system_bindings=False, - enable_search=False, - enable_open_in_editor=False, - enable_extra_page_navigation=False, - enable_auto_suggest_bindings=False): - """ - Create a Registry object that contains the default key bindings. - - :param enable_abort_and_exit_bindings: Filter to enable Ctrl-C and Ctrl-D. - :param enable_system_bindings: Filter to enable the system bindings (meta-! - prompt and Control-Z suspension.) - :param enable_search: Filter to enable the search bindings. - :param enable_open_in_editor: Filter to enable open-in-editor. - :param enable_open_in_editor: Filter to enable open-in-editor. - :param enable_extra_page_navigation: Filter for enabling extra page - navigation. (Bindings for up/down scrolling through long pages, like in - Emacs or Vi.) - :param enable_auto_suggest_bindings: Filter to enable fish-style suggestions. - """ - - assert get_search_state is None or callable(get_search_state) - - # Accept both Filters and booleans as input. - enable_abort_and_exit_bindings = to_cli_filter(enable_abort_and_exit_bindings) - enable_system_bindings = to_cli_filter(enable_system_bindings) - enable_search = to_cli_filter(enable_search) - enable_open_in_editor = to_cli_filter(enable_open_in_editor) - enable_extra_page_navigation = to_cli_filter(enable_extra_page_navigation) - enable_auto_suggest_bindings = to_cli_filter(enable_auto_suggest_bindings) - - registry = MergedRegistry([ - # Load basic bindings. - load_basic_bindings(), - load_mouse_bindings(), - - ConditionalRegistry(load_abort_and_exit_bindings(), - enable_abort_and_exit_bindings), - - ConditionalRegistry(load_basic_system_bindings(), - enable_system_bindings), - - # Load emacs bindings. - load_emacs_bindings(), - - ConditionalRegistry(load_emacs_open_in_editor_bindings(), - enable_open_in_editor), - - ConditionalRegistry(load_emacs_search_bindings(get_search_state=get_search_state), - enable_search), - - ConditionalRegistry(load_emacs_system_bindings(), - enable_system_bindings), - - ConditionalRegistry(load_extra_emacs_page_navigation_bindings(), - enable_extra_page_navigation), - - # Load Vi bindings. - load_vi_bindings(get_search_state=get_search_state), - - ConditionalRegistry(load_vi_open_in_editor_bindings(), - enable_open_in_editor), - - ConditionalRegistry(load_vi_search_bindings(get_search_state=get_search_state), - enable_search), - - ConditionalRegistry(load_vi_system_bindings(), - enable_system_bindings), - - ConditionalRegistry(load_extra_vi_page_navigation_bindings(), - enable_extra_page_navigation), - - # Suggestion bindings. - # (This has to come at the end, because the Vi bindings also have an - # implementation for the "right arrow", but we really want the - # suggestion binding when a suggestion is available.) - ConditionalRegistry(load_auto_suggestion_bindings(), - enable_auto_suggest_bindings), - ]) - - return registry - - -def load_key_bindings_for_prompt(**kw): - """ - Create a ``Registry`` object with the defaults key bindings for an input - prompt. - - This activates the key bindings for abort/exit (Ctrl-C/Ctrl-D), - incremental search and auto suggestions. - - (Not for full screen applications.) - """ - kw.setdefault('enable_abort_and_exit_bindings', True) - kw.setdefault('enable_search', True) - kw.setdefault('enable_auto_suggest_bindings', True) - - return load_key_bindings(**kw) +""" +Default key bindings.:: + + registry = load_key_bindings() + app = Application(key_bindings_registry=registry) +""" +from __future__ import unicode_literals +from prompt_toolkit.key_binding.registry import ConditionalRegistry, MergedRegistry +from prompt_toolkit.key_binding.bindings.basic import load_basic_bindings, load_abort_and_exit_bindings, load_basic_system_bindings, load_auto_suggestion_bindings, load_mouse_bindings +from prompt_toolkit.key_binding.bindings.emacs import load_emacs_bindings, load_emacs_system_bindings, load_emacs_search_bindings, load_emacs_open_in_editor_bindings, load_extra_emacs_page_navigation_bindings +from prompt_toolkit.key_binding.bindings.vi import load_vi_bindings, load_vi_system_bindings, load_vi_search_bindings, load_vi_open_in_editor_bindings, load_extra_vi_page_navigation_bindings +from prompt_toolkit.filters import to_cli_filter + +__all__ = ( + 'load_key_bindings', + 'load_key_bindings_for_prompt', +) + + +def load_key_bindings( + get_search_state=None, + enable_abort_and_exit_bindings=False, + enable_system_bindings=False, + enable_search=False, + enable_open_in_editor=False, + enable_extra_page_navigation=False, + enable_auto_suggest_bindings=False): + """ + Create a Registry object that contains the default key bindings. + + :param enable_abort_and_exit_bindings: Filter to enable Ctrl-C and Ctrl-D. + :param enable_system_bindings: Filter to enable the system bindings (meta-! + prompt and Control-Z suspension.) + :param enable_search: Filter to enable the search bindings. + :param enable_open_in_editor: Filter to enable open-in-editor. + :param enable_open_in_editor: Filter to enable open-in-editor. + :param enable_extra_page_navigation: Filter for enabling extra page + navigation. (Bindings for up/down scrolling through long pages, like in + Emacs or Vi.) + :param enable_auto_suggest_bindings: Filter to enable fish-style suggestions. + """ + + assert get_search_state is None or callable(get_search_state) + + # Accept both Filters and booleans as input. + enable_abort_and_exit_bindings = to_cli_filter(enable_abort_and_exit_bindings) + enable_system_bindings = to_cli_filter(enable_system_bindings) + enable_search = to_cli_filter(enable_search) + enable_open_in_editor = to_cli_filter(enable_open_in_editor) + enable_extra_page_navigation = to_cli_filter(enable_extra_page_navigation) + enable_auto_suggest_bindings = to_cli_filter(enable_auto_suggest_bindings) + + registry = MergedRegistry([ + # Load basic bindings. + load_basic_bindings(), + load_mouse_bindings(), + + ConditionalRegistry(load_abort_and_exit_bindings(), + enable_abort_and_exit_bindings), + + ConditionalRegistry(load_basic_system_bindings(), + enable_system_bindings), + + # Load emacs bindings. + load_emacs_bindings(), + + ConditionalRegistry(load_emacs_open_in_editor_bindings(), + enable_open_in_editor), + + ConditionalRegistry(load_emacs_search_bindings(get_search_state=get_search_state), + enable_search), + + ConditionalRegistry(load_emacs_system_bindings(), + enable_system_bindings), + + ConditionalRegistry(load_extra_emacs_page_navigation_bindings(), + enable_extra_page_navigation), + + # Load Vi bindings. + load_vi_bindings(get_search_state=get_search_state), + + ConditionalRegistry(load_vi_open_in_editor_bindings(), + enable_open_in_editor), + + ConditionalRegistry(load_vi_search_bindings(get_search_state=get_search_state), + enable_search), + + ConditionalRegistry(load_vi_system_bindings(), + enable_system_bindings), + + ConditionalRegistry(load_extra_vi_page_navigation_bindings(), + enable_extra_page_navigation), + + # Suggestion bindings. + # (This has to come at the end, because the Vi bindings also have an + # implementation for the "right arrow", but we really want the + # suggestion binding when a suggestion is available.) + ConditionalRegistry(load_auto_suggestion_bindings(), + enable_auto_suggest_bindings), + ]) + + return registry + + +def load_key_bindings_for_prompt(**kw): + """ + Create a ``Registry`` object with the defaults key bindings for an input + prompt. + + This activates the key bindings for abort/exit (Ctrl-C/Ctrl-D), + incremental search and auto suggestions. + + (Not for full screen applications.) + """ + kw.setdefault('enable_abort_and_exit_bindings', True) + kw.setdefault('enable_search', True) + kw.setdefault('enable_auto_suggest_bindings', True) + + return load_key_bindings(**kw) diff --git a/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/input_processor.py b/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/input_processor.py index 51a3110827a..7c1da5de905 100644 --- a/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/input_processor.py +++ b/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/input_processor.py @@ -12,7 +12,7 @@ from prompt_toolkit.filters.cli import ViNavigationMode from prompt_toolkit.keys import Keys, Key from prompt_toolkit.utils import Event -from .registry import BaseRegistry +from .registry import BaseRegistry from collections import deque from six.moves import range @@ -67,11 +67,11 @@ class InputProcessor(object): # Now the ControlX-ControlC callback will be called if this sequence is # registered in the registry. - :param registry: `BaseRegistry` instance. + :param registry: `BaseRegistry` instance. :param cli_ref: weakref to `CommandLineInterface`. """ def __init__(self, registry, cli_ref): - assert isinstance(registry, BaseRegistry) + assert isinstance(registry, BaseRegistry) self._registry = registry self._cli_ref = cli_ref @@ -86,14 +86,14 @@ class InputProcessor(object): # (This is at at most the amount of keys that make up for one key binding.) self.key_buffer = [] - # Simple macro recording. (Like readline does.) - self.record_macro = False - self.macro = [] - + # Simple macro recording. (Like readline does.) + self.record_macro = False + self.macro = [] + self.reset() def reset(self): - self._previous_key_sequence = [] + self._previous_key_sequence = [] self._previous_handler = None self._process_coroutine = self._process() @@ -103,19 +103,19 @@ class InputProcessor(object): #: https://www.gnu.org/software/bash/manual/html_node/Readline-Arguments.html self.arg = None - def start_macro(self): - " Start recording macro. " - self.record_macro = True - self.macro = [] - - def end_macro(self): - " End recording macro. " - self.record_macro = False - - def call_macro(self): - for k in self.macro: - self.feed(k) - + def start_macro(self): + " Start recording macro. " + self.record_macro = True + self.macro = [] + + def end_macro(self): + " End recording macro. " + self.record_macro = False + + def call_macro(self): + for k in self.macro: + self.feed(k) + def _get_matches(self, key_presses): """ For a list of :class:`KeyPress` instances. Give the matching handlers @@ -163,17 +163,17 @@ class InputProcessor(object): is_prefix_of_longer_match = self._is_prefix_of_longer_match(buffer) matches = self._get_matches(buffer) - # When eager matches were found, give priority to them and also - # ignore all the longer matches. - eager_matches = [m for m in matches if m.eager(self._cli_ref())] - - if eager_matches: - matches = eager_matches + # When eager matches were found, give priority to them and also + # ignore all the longer matches. + eager_matches = [m for m in matches if m.eager(self._cli_ref())] + + if eager_matches: + matches = eager_matches is_prefix_of_longer_match = False # Exact matches found, call handler. if not is_prefix_of_longer_match and matches: - self._call_handler(matches[-1], key_sequence=buffer[:]) + self._call_handler(matches[-1], key_sequence=buffer[:]) del buffer[:] # Keep reference. # No match found. @@ -227,7 +227,7 @@ class InputProcessor(object): cli.invalidate() def _call_handler(self, handler, key_sequence=None): - was_recording = self.record_macro + was_recording = self.record_macro arg = self.arg self.arg = None @@ -255,11 +255,11 @@ class InputProcessor(object): self._previous_key_sequence = key_sequence self._previous_handler = handler - # Record the key sequence in our macro. (Only if we're in macro mode - # before and after executing the key.) - if self.record_macro and was_recording: - self.macro.extend(key_sequence) - + # Record the key sequence in our macro. (Only if we're in macro mode + # before and after executing the key.) + if self.record_macro and was_recording: + self.macro.extend(key_sequence) + def _fix_vi_cursor_position(self, event): """ After every command, make sure that if we are in Vi navigation mode, we diff --git a/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/manager.py b/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/manager.py index 83612c2a5cc..7bad16a6c5d 100644 --- a/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/manager.py +++ b/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/manager.py @@ -1,7 +1,7 @@ """ -DEPRECATED: -Use `prompt_toolkit.key_binding.defaults.load_key_bindings` instead. - +DEPRECATED: +Use `prompt_toolkit.key_binding.defaults.load_key_bindings` instead. + :class:`KeyBindingManager` is a utility (or shortcut) for loading all the key bindings in a key binding registry, with a logic set of filters to quickly to quickly change from Vi to Emacs key bindings at runtime. @@ -11,12 +11,12 @@ You don't have to use this, but it's practical. Usage:: manager = KeyBindingManager() - app = Application(key_bindings_registry=manager.registry) + app = Application(key_bindings_registry=manager.registry) """ from __future__ import unicode_literals -from .defaults import load_key_bindings +from .defaults import load_key_bindings from prompt_toolkit.filters import to_cli_filter -from prompt_toolkit.key_binding.registry import Registry, ConditionalRegistry, MergedRegistry +from prompt_toolkit.key_binding.registry import Registry, ConditionalRegistry, MergedRegistry __all__ = ( 'KeyBindingManager', @@ -40,37 +40,37 @@ class KeyBindingManager(object): :param enable_vi_mode: Deprecated! """ - def __init__(self, - registry=None, # XXX: not used anymore. + def __init__(self, + registry=None, # XXX: not used anymore. enable_vi_mode=None, # (`enable_vi_mode` is deprecated.) - enable_all=True, # + enable_all=True, # get_search_state=None, enable_abort_and_exit_bindings=False, - enable_system_bindings=False, - enable_search=False, - enable_open_in_editor=False, - enable_extra_page_navigation=False, - enable_auto_suggest_bindings=False): + enable_system_bindings=False, + enable_search=False, + enable_open_in_editor=False, + enable_extra_page_navigation=False, + enable_auto_suggest_bindings=False): assert registry is None or isinstance(registry, Registry) assert get_search_state is None or callable(get_search_state) enable_all = to_cli_filter(enable_all) - defaults = load_key_bindings( - get_search_state=get_search_state, - enable_abort_and_exit_bindings=enable_abort_and_exit_bindings, - enable_system_bindings=enable_system_bindings, - enable_search=enable_search, - enable_open_in_editor=enable_open_in_editor, - enable_extra_page_navigation=enable_extra_page_navigation, - enable_auto_suggest_bindings=enable_auto_suggest_bindings) - - # Note, we wrap this whole thing again in a MergedRegistry, because we - # don't want the `enable_all` settings to apply on items that were - # added to the registry as a whole. - self.registry = MergedRegistry([ - ConditionalRegistry(defaults, enable_all) - ]) + defaults = load_key_bindings( + get_search_state=get_search_state, + enable_abort_and_exit_bindings=enable_abort_and_exit_bindings, + enable_system_bindings=enable_system_bindings, + enable_search=enable_search, + enable_open_in_editor=enable_open_in_editor, + enable_extra_page_navigation=enable_extra_page_navigation, + enable_auto_suggest_bindings=enable_auto_suggest_bindings) + + # Note, we wrap this whole thing again in a MergedRegistry, because we + # don't want the `enable_all` settings to apply on items that were + # added to the registry as a whole. + self.registry = MergedRegistry([ + ConditionalRegistry(defaults, enable_all) + ]) @classmethod def for_prompt(cls, **kw): diff --git a/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/registry.py b/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/registry.py index 24d0e729a1b..5d6bc3493cc 100644 --- a/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/registry.py +++ b/contrib/python/prompt-toolkit/py2/prompt_toolkit/key_binding/registry.py @@ -1,42 +1,42 @@ -""" -Key bindings registry. - -A `Registry` object is a container that holds a list of key bindings. It has a -very efficient internal data structure for checking which key bindings apply -for a pressed key. - -Typical usage:: - - r = Registry() - - @r.add_binding(Keys.ControlX, Keys.ControlC, filter=INSERT) - def handler(event): - # Handle ControlX-ControlC key sequence. - pass - - -It is also possible to combine multiple registries. We do this in the default -key bindings. There are some registries that contain Emacs bindings, while -others contain the Vi bindings. They are merged together using a -`MergedRegistry`. - -We also have a `ConditionalRegistry` object that can enable/disable a group of -key bindings at once. -""" +""" +Key bindings registry. + +A `Registry` object is a container that holds a list of key bindings. It has a +very efficient internal data structure for checking which key bindings apply +for a pressed key. + +Typical usage:: + + r = Registry() + + @r.add_binding(Keys.ControlX, Keys.ControlC, filter=INSERT) + def handler(event): + # Handle ControlX-ControlC key sequence. + pass + + +It is also possible to combine multiple registries. We do this in the default +key bindings. There are some registries that contain Emacs bindings, while +others contain the Vi bindings. They are merged together using a +`MergedRegistry`. + +We also have a `ConditionalRegistry` object that can enable/disable a group of +key bindings at once. +""" from __future__ import unicode_literals -from abc import ABCMeta, abstractmethod - +from abc import ABCMeta, abstractmethod + from prompt_toolkit.cache import SimpleCache from prompt_toolkit.filters import CLIFilter, to_cli_filter, Never from prompt_toolkit.keys import Key, Keys -from six import text_type, with_metaclass +from six import text_type, with_metaclass __all__ = ( - 'BaseRegistry', + 'BaseRegistry', 'Registry', - 'ConditionalRegistry', - 'MergedRegistry', + 'ConditionalRegistry', + 'MergedRegistry', ) @@ -65,36 +65,36 @@ class _Binding(object): self.__class__.__name__, self.keys, self.handler) -class BaseRegistry(with_metaclass(ABCMeta, object)): +class BaseRegistry(with_metaclass(ABCMeta, object)): """ - Interface for a Registry. - """ - _version = 0 # For cache invalidation. - - @abstractmethod - def get_bindings_for_keys(self, keys): - pass - - @abstractmethod - def get_bindings_starting_with_keys(self, keys): - pass - - # `add_binding` and `remove_binding` don't have to be part of this - # interface. - - -class Registry(BaseRegistry): - """ - Key binding registry. + Interface for a Registry. + """ + _version = 0 # For cache invalidation. + + @abstractmethod + def get_bindings_for_keys(self, keys): + pass + + @abstractmethod + def get_bindings_starting_with_keys(self, keys): + pass + + # `add_binding` and `remove_binding` don't have to be part of this + # interface. + + +class Registry(BaseRegistry): """ + Key binding registry. + """ def __init__(self): self.key_bindings = [] self._get_bindings_for_keys_cache = SimpleCache(maxsize=10000) self._get_bindings_starting_with_keys_cache = SimpleCache(maxsize=1000) - self._version = 0 # For cache invalidation. + self._version = 0 # For cache invalidation. def _clear_cache(self): - self._version += 1 + self._version += 1 self._get_bindings_for_keys_cache.clear() self._get_bindings_starting_with_keys_cache.clear() @@ -215,136 +215,136 @@ class Registry(BaseRegistry): return result return self._get_bindings_starting_with_keys_cache.get(keys, get) - - -class _AddRemoveMixin(BaseRegistry): - """ - Common part for ConditionalRegistry and MergedRegistry. - """ - def __init__(self): - # `Registry` to be synchronized with all the others. - self._registry2 = Registry() - self._last_version = None - - # The 'extra' registry. Mostly for backwards compatibility. - self._extra_registry = Registry() - - def _update_cache(self): - raise NotImplementedError - - # For backwards, compatibility, we allow adding bindings to both - # ConditionalRegistry and MergedRegistry. This is however not the - # recommended way. Better is to create a new registry and merge them - # together using MergedRegistry. - - def add_binding(self, *k, **kw): - return self._extra_registry.add_binding(*k, **kw) - - def remove_binding(self, *k, **kw): - return self._extra_registry.remove_binding(*k, **kw) - - # Proxy methods to self._registry2. - - @property - def key_bindings(self): - self._update_cache() - return self._registry2.key_bindings - - @property - def _version(self): - self._update_cache() - return self._last_version - - def get_bindings_for_keys(self, *a, **kw): - self._update_cache() - return self._registry2.get_bindings_for_keys(*a, **kw) - - def get_bindings_starting_with_keys(self, *a, **kw): - self._update_cache() - return self._registry2.get_bindings_starting_with_keys(*a, **kw) - - -class ConditionalRegistry(_AddRemoveMixin): - """ - Wraps around a `Registry`. Disable/enable all the key bindings according to - the given (additional) filter.:: - - @Condition - def setting_is_true(cli): - return True # or False - - registy = ConditionalRegistry(registry, setting_is_true) - - When new key bindings are added to this object. They are also - enable/disabled according to the given `filter`. - - :param registries: List of `Registry` objects. - :param filter: `CLIFilter` object. - """ - def __init__(self, registry=None, filter=True): - registry = registry or Registry() - assert isinstance(registry, BaseRegistry) - - _AddRemoveMixin.__init__(self) - - self.registry = registry - self.filter = to_cli_filter(filter) - - def _update_cache(self): - " If the original registry was changed. Update our copy version. " - expected_version = (self.registry._version, self._extra_registry._version) - - if self._last_version != expected_version: - registry2 = Registry() - - # Copy all bindings from `self.registry`, adding our condition. - for reg in (self.registry, self._extra_registry): - for b in reg.key_bindings: - registry2.key_bindings.append( - _Binding( - keys=b.keys, - handler=b.handler, - filter=self.filter & b.filter, - eager=b.eager, - save_before=b.save_before)) - - self._registry2 = registry2 - self._last_version = expected_version - - -class MergedRegistry(_AddRemoveMixin): - """ - Merge multiple registries of key bindings into one. - - This class acts as a proxy to multiple `Registry` objects, but behaves as - if this is just one bigger `Registry`. - - :param registries: List of `Registry` objects. - """ - def __init__(self, registries): - assert all(isinstance(r, BaseRegistry) for r in registries) - - _AddRemoveMixin.__init__(self) - - self.registries = registries - - def _update_cache(self): - """ - If one of the original registries was changed. Update our merged - version. - """ - expected_version = ( - tuple(r._version for r in self.registries) + - (self._extra_registry._version, )) - - if self._last_version != expected_version: - registry2 = Registry() - - for reg in self.registries: - registry2.key_bindings.extend(reg.key_bindings) - - # Copy all bindings from `self._extra_registry`. - registry2.key_bindings.extend(self._extra_registry.key_bindings) - - self._registry2 = registry2 - self._last_version = expected_version + + +class _AddRemoveMixin(BaseRegistry): + """ + Common part for ConditionalRegistry and MergedRegistry. + """ + def __init__(self): + # `Registry` to be synchronized with all the others. + self._registry2 = Registry() + self._last_version = None + + # The 'extra' registry. Mostly for backwards compatibility. + self._extra_registry = Registry() + + def _update_cache(self): + raise NotImplementedError + + # For backwards, compatibility, we allow adding bindings to both + # ConditionalRegistry and MergedRegistry. This is however not the + # recommended way. Better is to create a new registry and merge them + # together using MergedRegistry. + + def add_binding(self, *k, **kw): + return self._extra_registry.add_binding(*k, **kw) + + def remove_binding(self, *k, **kw): + return self._extra_registry.remove_binding(*k, **kw) + + # Proxy methods to self._registry2. + + @property + def key_bindings(self): + self._update_cache() + return self._registry2.key_bindings + + @property + def _version(self): + self._update_cache() + return self._last_version + + def get_bindings_for_keys(self, *a, **kw): + self._update_cache() + return self._registry2.get_bindings_for_keys(*a, **kw) + + def get_bindings_starting_with_keys(self, *a, **kw): + self._update_cache() + return self._registry2.get_bindings_starting_with_keys(*a, **kw) + + +class ConditionalRegistry(_AddRemoveMixin): + """ + Wraps around a `Registry`. Disable/enable all the key bindings according to + the given (additional) filter.:: + + @Condition + def setting_is_true(cli): + return True # or False + + registy = ConditionalRegistry(registry, setting_is_true) + + When new key bindings are added to this object. They are also + enable/disabled according to the given `filter`. + + :param registries: List of `Registry` objects. + :param filter: `CLIFilter` object. + """ + def __init__(self, registry=None, filter=True): + registry = registry or Registry() + assert isinstance(registry, BaseRegistry) + + _AddRemoveMixin.__init__(self) + + self.registry = registry + self.filter = to_cli_filter(filter) + + def _update_cache(self): + " If the original registry was changed. Update our copy version. " + expected_version = (self.registry._version, self._extra_registry._version) + + if self._last_version != expected_version: + registry2 = Registry() + + # Copy all bindings from `self.registry`, adding our condition. + for reg in (self.registry, self._extra_registry): + for b in reg.key_bindings: + registry2.key_bindings.append( + _Binding( + keys=b.keys, + handler=b.handler, + filter=self.filter & b.filter, + eager=b.eager, + save_before=b.save_before)) + + self._registry2 = registry2 + self._last_version = expected_version + + +class MergedRegistry(_AddRemoveMixin): + """ + Merge multiple registries of key bindings into one. + + This class acts as a proxy to multiple `Registry` objects, but behaves as + if this is just one bigger `Registry`. + + :param registries: List of `Registry` objects. + """ + def __init__(self, registries): + assert all(isinstance(r, BaseRegistry) for r in registries) + + _AddRemoveMixin.__init__(self) + + self.registries = registries + + def _update_cache(self): + """ + If one of the original registries was changed. Update our merged + version. + """ + expected_version = ( + tuple(r._version for r in self.registries) + + (self._extra_registry._version, )) + + if self._last_version != expected_version: + registry2 = Registry() + + for reg in self.registries: + registry2.key_bindings.extend(reg.key_bindings) + + # Copy all bindings from `self._extra_registry`. + registry2.key_bindings.extend(self._extra_registry.key_bindings) + + self._registry2 = registry2 + self._last_version = expected_version |