add workaround for ctrl-c not quitting reverse-i-search in readline shell

This commit is contained in:
adam j hartz 2016-05-17 23:52:33 -04:00
parent fbd61b0c20
commit baf08e35bf

View file

@ -19,14 +19,15 @@ if HAVE_PYGMENTS:
import pygments import pygments
from pygments.formatters.terminal256 import Terminal256Formatter from pygments.formatters.terminal256 import Terminal256Formatter
RL_COMPLETION_SUPPRESS_APPEND = RL_LIB = None RL_COMPLETION_SUPPRESS_APPEND = RL_LIB = RL_STATE = None
RL_CAN_RESIZE = False RL_CAN_RESIZE = False
RL_DONE = None RL_DONE = None
_RL_STATE_DONE = 0x1000000
_RL_STATE_ISEARCH = 0x0000080
def setup_readline(): def setup_readline():
"""Sets up the readline module and completion suppression, if available.""" """Sets up the readline module and completion suppression, if available."""
global RL_COMPLETION_SUPPRESS_APPEND, RL_LIB, RL_CAN_RESIZE global RL_COMPLETION_SUPPRESS_APPEND, RL_LIB, RL_CAN_RESIZE, RL_STATE
if RL_COMPLETION_SUPPRESS_APPEND is not None: if RL_COMPLETION_SUPPRESS_APPEND is not None:
return return
try: try:
@ -44,6 +45,7 @@ def setup_readline():
except ValueError: except ValueError:
# not all versions of readline have this symbol, ie Macs sometimes # not all versions of readline have this symbol, ie Macs sometimes
RL_COMPLETION_SUPPRESS_APPEND = None RL_COMPLETION_SUPPRESS_APPEND = None
RL_STATE = ctypes.c_int.in_dll(lib, 'rl_readline_state')
RL_CAN_RESIZE = hasattr(lib, 'rl_reset_screen_size') RL_CAN_RESIZE = hasattr(lib, 'rl_reset_screen_size')
env = builtins.__xonsh_env__ env = builtins.__xonsh_env__
# reads in history # reads in history
@ -71,6 +73,20 @@ def teardown_readline():
return return
def fix_readline_state_after_ctrl_c():
"""
Fix to allow Ctrl-C to exit reverse-i-search.
Based on code from:
http://bugs.python.org/file39467/raw_input__workaround_demo.py
"""
if RL_STATE.value & _RL_STATE_ISEARCH:
RL_STATE.value &= ~_RL_STATE_ISEARCH
if not RL_STATE.value & _RL_STATE_DONE:
RL_STATE.value |= _RL_STATE_DONE
def rl_completion_suppress_append(val=1): def rl_completion_suppress_append(val=1):
"""Sets the rl_completion_suppress_append varaiable, if possible. """Sets the rl_completion_suppress_append varaiable, if possible.
A value of 1 (default) means to suppress, a value of 0 means to enable. A value of 1 (default) means to suppress, a value of 0 means to enable.
@ -263,6 +279,7 @@ class ReadlineShell(BaseShell, Cmd):
self._cmdloop(intro=intro) self._cmdloop(intro=intro)
except KeyboardInterrupt: except KeyboardInterrupt:
print() # Gives a newline print() # Gives a newline
fix_readline_state_after_ctrl_c()
self.reset_buffer() self.reset_buffer()
intro = None intro = None