From f3fc25115db7951abdd06601c348f232142fe6c8 2017-05-04 16:33:56 From: meeseeksdev[bot] Date: 2017-05-04 16:33:56 Subject: [PATCH] Merge pull request #10500 from takluyver/config-return-handler Allow configuring a function to handle return in the terminal --- diff --git a/IPython/terminal/interactiveshell.py b/IPython/terminal/interactiveshell.py index 9bcc21b..cce643e 100644 --- a/IPython/terminal/interactiveshell.py +++ b/IPython/terminal/interactiveshell.py @@ -10,7 +10,10 @@ from IPython.utils import io from IPython.utils.py3compat import cast_unicode_py2, input from IPython.utils.terminal import toggle_set_term_title, set_term_title from IPython.utils.process import abbrev_cwd -from traitlets import Bool, Unicode, Dict, Integer, observe, Instance, Type, default, Enum, Union +from traitlets import ( + Bool, Unicode, Dict, Integer, observe, Instance, Type, default, Enum, Union, + Any, +) from prompt_toolkit.document import Document from prompt_toolkit.enums import DEFAULT_BUFFER, EditingMode @@ -192,6 +195,12 @@ class TerminalInteractiveShell(InteractiveShell): "This is in addition to the F2 binding, which is always enabled." ).tag(config=True) + handle_return = Any(None, + help="Provide an alternative handler to be called when the user presses " + "Return. This is an advanced option intended for debugging, which " + "may be changed or removed in later releases." + ).tag(config=True) + @observe('term_title') def init_term_title(self, change=None): # Enable or disable the terminal title. diff --git a/IPython/terminal/shortcuts.py b/IPython/terminal/shortcuts.py index e626ccf..63c14a7 100644 --- a/IPython/terminal/shortcuts.py +++ b/IPython/terminal/shortcuts.py @@ -31,12 +31,17 @@ def register_ipython_shortcuts(registry, shell): """Set up the prompt_toolkit keyboard shortcuts for IPython""" insert_mode = ViInsertMode() | EmacsInsertMode() + if getattr(shell, 'handle_return', None): + return_handler = shell.handle_return(shell) + else: + return_handler = newline_or_execute_outer(shell) + # Ctrl+J == Enter, seemingly registry.add_binding(Keys.ControlJ, filter=(HasFocus(DEFAULT_BUFFER) & ~HasSelection() & insert_mode - ))(newline_or_execute_outer(shell)) + ))(return_handler) registry.add_binding(Keys.ControlBackslash)(force_exit)