X-Git-Url: https://git.distorted.org.uk/~mdw/profile/blobdiff_plain/bff12eb1383898f758fcf19f3fc1a62846e4e963..6ada84f4e0acb4149b95b02ab35ec7a3f73ac1c8:/dot/ipython-key-bindings.py?ds=sidebyside diff --git a/dot/ipython-key-bindings.py b/dot/ipython-key-bindings.py index 3f392fe..8795404 100644 --- a/dot/ipython-key-bindings.py +++ b/dot/ipython-key-bindings.py @@ -1,29 +1,35 @@ ### -*-python -*- -import IPython as IPY -import prompt_toolkit as PTK - -def ding(): - with open('/dev/tty', 'w') as f: f.write('\a') - -## Key bindings. Alas, IPython's attempt at Emacs keybindings is abysmal. -K = PTK.keys.Keys -F = PTK.filters -BUF = PTK.enums.DEFAULT_BUFFER -ipy = IPY.get_ipython() -try: pt = ipy.pt_cli -except AttributeError: pass -else: +def __mdw_hack_bindings(): + + import IPython as IPY + import prompt_toolkit as PTK + + def ding(): + with open('/dev/tty', 'w') as f: f.write('\a') + + ## Key bindings. Alas, IPython's attempt at Emacs keybindings is abysmal. + K = PTK.keys.Keys + F = PTK.filters + BUF = PTK.enums.DEFAULT_BUFFER + ipy = IPY.get_ipython() + try: pt = ipy.pt_cli + except AttributeError: return + reg = pt.application.key_bindings_registry - bind = PTK.key_binding.bindings.utils.create_handle_decorator(reg) + try: bind = reg.add_binding + except AttributeError: + bind = PTK.key_binding.bindings.utils.create_handle_decorator(reg) def inhibit_history_search(buf, fn): - searchp, searchtext = buf.enable_history_search, buf.history_search_text + searchp, searchtext = \ + buf.enable_history_search, buf.history_search_text buf.enable_history_search = F.Never() try: fn() finally: - buf.enable_history_search, buf.history_search_text = searchp, searchtext + buf.enable_history_search, buf.history_search_text = \ + searchp, searchtext @bind(K.ControlP) def prev_line(ev): buf = ev.current_buffer @@ -41,3 +47,6 @@ else: bind(K.Escape, u'p')(lambda ev: ev.current_buffer.history_backward()) bind(K.Escape, u'n')(lambda ev: ev.current_buffer.history_forward()) + +__mdw_hack_bindings() +del __mdw_hack_bindings