11from prompt_toolkit .key_binding .bindings .named_commands import (accept_line ,
22 self_insert , backward_delete_char , beginning_of_line )
33from prompt_toolkit .key_binding .bindings .basic import if_no_repeat
4- from prompt_toolkit .key_binding .defaults import load_key_bindings
5- from prompt_toolkit .key_binding .registry import Registry , MergedRegistry
6- from prompt_toolkit .keys import Keys , Key
4+ from prompt_toolkit .key_binding .bindings .basic import load_basic_bindings
5+ from prompt_toolkit .key_binding .bindings .emacs import load_emacs_bindings , load_emacs_search_bindings
6+ from prompt_toolkit .key_binding .bindings .mouse import load_mouse_bindings
7+ from prompt_toolkit .key_binding .bindings .cpr import load_cpr_bindings
8+
9+ from prompt_toolkit .key_binding import KeyBindings , merge_key_bindings
10+ from prompt_toolkit .keys import Keys , ALL_KEYS
711from prompt_toolkit .filters import Condition , HasSelection
812from prompt_toolkit .selection import SelectionState
913from prompt_toolkit .clipboard import ClipboardData
10- from prompt_toolkit .terminal .vt100_input import ANSI_SEQUENCES
14+ from prompt_toolkit .input .vt100_parser import ANSI_SEQUENCES
15+ from prompt_toolkit .application .current import get_app
16+ from prompt_toolkit .application import run_in_terminal
1117
12- from .multiline import (auto_newline , TabShouldInsertWhitespaceFilter ,
18+ from .multiline import (auto_newline , tab_should_insert_whitespace ,
1319 document_is_multiline_python )
1420from .tokenize import inside_string , matching_parens
1521from .theme import emoji
1925import sys
2026import textwrap
2127
22- def get_registry ():
23- registry = MergedRegistry ([
24- load_key_bindings (
25- enable_abort_and_exit_bindings = True ,
26- enable_search = True ,
27- # Not using now but may in the future
28- enable_auto_suggest_bindings = True ,
29- enable_extra_page_navigation = True ,
30- # Custom one defined below, without execute
31- enable_open_in_editor = False ,
32- enable_system_bindings = True ,
33- ),
34- custom_bindings_registry ,
35- ])
28+ def get_key_bindings ():
29+ # Based on prompt_toolkit.key_binding.defaults.load_key_bindings()
30+ return merge_key_bindings ([
31+ load_basic_bindings (),
32+
33+ load_emacs_bindings (),
34+ load_emacs_search_bindings (),
3635
37- return registry
36+ load_mouse_bindings (),
37+ load_cpr_bindings (),
3838
39- r = custom_bindings_registry = Registry ()
39+ custom_key_bindings ,
40+ ])
41+
42+ r = custom_key_bindings = KeyBindings ()
4043
4144@r .add_binding (Keys .Escape , 'p' )
4245def previous_history_search (event ):
@@ -276,10 +279,14 @@ def right_multiline(event):
276279
277280@r .add_binding (Keys .ControlD )
278281def exit (event ):
279- raise EOFError ("Control-D" )
282+ event .app .exit (exception = EOFError , style = 'class:exiting' )
283+
284+ @r .add_binding (Keys .ControlC )
285+ def keyboard_interrupt (event ):
286+ event .app .exit (exception = KeyboardInterrupt , style = 'class:aborting' )
280287
281288is_returnable = Condition (
282- lambda cli : cli .current_buffer . accept_action .is_returnable )
289+ lambda : get_app () .current_buffer .is_returnable )
283290
284291@r .add_binding (Keys .Enter , filter = is_returnable )
285292def multiline_enter (event ):
@@ -320,14 +327,15 @@ def multiline_enter(event):
320327@r .add_binding (Keys .Enter , filter = is_returnable )
321328def accept_after_history_backward (event ):
322329 pks = event .previous_key_sequence
323- if pks and getattr (pks [- 1 ], 'accept_next' , False ) and ((len (pks ) == 1 and isinstance ( pks [ 0 ]. key , Key ) and pks [ 0 ]. key . name == "<Up>" ) \
324- or (len (pks ) == 2 and isinstance ( pks [0 ].key , Key ) and pks [ 0 ]. key . name == "<Escape>"
325- and isinstance (pks [1 ].key , str ) and pks [1 ].key in 'pP' )):
330+ if pks and getattr (pks [- 1 ], 'accept_next' , False ) and ((len (pks ) == 1 and
331+ pks [ 0 ]. key == "<Up>" ) or (len (pks ) == 2 and pks [0 ].key == "<Escape>"
332+ and isinstance (pks [1 ].key , str ) and pks [1 ].key in 'pP' )):
326333 accept_line (event )
327334 else :
328335 multiline_enter (event )
329336
330337@r .add_binding (Keys .Escape , Keys .Enter )
338+ @r .add_binding (Keys .Escape , Keys .ControlJ )
331339def insert_newline (event ):
332340 auto_newline (event .current_buffer )
333341
@@ -337,12 +345,13 @@ def open_line(event):
337345 event .current_buffer .cursor_left ()
338346
339347# M-[ a g is set to S-Enter in iTerm2 settings
340- Keys .ShiftEnter = Key ("<Shift-Enter>" )
348+ Keys .ShiftEnter = "<Shift-Enter>"
349+ ALL_KEYS .append ('<Shift-Enter>' )
341350ANSI_SEQUENCES ['\x1b [ag' ] = Keys .ShiftEnter
342351
343352r .add_binding (Keys .ShiftEnter )(accept_line )
344353
345- @r .add_binding (Keys .Tab , filter = TabShouldInsertWhitespaceFilter () )
354+ @r .add_binding (Keys .Tab , filter = tab_should_insert_whitespace )
346355def indent (event ):
347356 """
348357 When tab should insert whitespace, do that instead of completion.
@@ -641,7 +650,7 @@ def osx_paste():
641650def copy_to_clipboard (event ):
642651 if event .current_buffer .document .selection :
643652 from_ , to = event .current_buffer .document .selection_range ()
644- event . cli . run_in_terminal (lambda :osx_copy (event .current_buffer .document .text [from_ :to + 1 ]))
653+ run_in_terminal (lambda :osx_copy (event .current_buffer .document .text [from_ :to + 1 ]))
645654
646655@r .add_binding (Keys .ControlX , Keys .ControlY )
647656def paste_from_clipboard (event ):
@@ -650,13 +659,14 @@ def get_paste():
650659 nonlocal paste_text
651660 paste_text = osx_paste ()
652661
653- event . cli . run_in_terminal (get_paste )
662+ run_in_terminal (get_paste )
654663
655664 event .current_buffer .cut_selection ()
656665 event .current_buffer .paste_clipboard_data (ClipboardData (paste_text ))
657666
658667# M-[ a b is set to C-S-/ (C-?) in iTerm2 settings
659- Keys .ControlQuestionmark = Key ("<C-?>" )
668+ Keys .ControlQuestionmark = "<C-?>"
669+ ALL_KEYS .append ("<C-?>" )
660670ANSI_SEQUENCES ['\x1b [ab' ] = Keys .ControlQuestionmark
661671
662672# This won't work until
0 commit comments