readline parse and bind case sensitive dynamic

This commit is contained in:
Hugo Wang 2016-06-28 01:36:08 +08:00
parent fd677dbdc6
commit 9b0eb19a71

View file

@ -26,7 +26,7 @@ from xonsh.base_shell import BaseShell
from xonsh.ansi_colors import partial_color_format, color_style_names, color_style
from xonsh.environ import partial_format_prompt, multiline_prompt
from xonsh.tools import print_exception
from xonsh.platform import HAS_PYGMENTS, ON_WINDOWS, ON_CYGWIN, ON_DARWIN
from xonsh.platform import ON_WINDOWS, ON_CYGWIN, ON_DARWIN
pygments = LazyObject(lambda: importlib.import_module('pygments'),
globals(), 'pygments')
@ -43,6 +43,7 @@ RL_DONE = None
RL_VARIABLE_VALUE = None
_RL_STATE_DONE = 0x1000000
_RL_STATE_ISEARCH = 0x0000080
_RL_PREV_CASE_SENSITIVE_COMPLETIONS = None
def setup_readline():
"""Sets up the readline module and completion suppression, if available."""
@ -121,12 +122,6 @@ def setup_readline():
# this seems to fail with libedit
print_exception('xonsh: could not load readline default init file.')
# handle case sensitive, see Github issue #1342 for details
if env.get('CASE_SENSITIVE_COMPLETIONS'):
readline.parse_and_bind("set completion-ignore-case off")
else:
readline.parse_and_bind("set completion-ignore-case on")
def teardown_readline():
"""Tears down up the readline module, if available."""
@ -238,9 +233,24 @@ class ReadlineShell(BaseShell, Cmd):
"""Overridden to no-op."""
return '', line, line
def _rebind_case_sensitive_completions(self):
# handle case sensitive, see Github issue #1342 for details
global _RL_PREV_CASE_SENSITIVE_COMPLETIONS
env = builtins.__xonsh_env__
case_sensitive = env.get('CASE_SENSITIVE_COMPLETIONS')
if case_sensitive == _RL_PREV_CASE_SENSITIVE_COMPLETIONS:
return
if case_sensitive:
readline.parse_and_bind("set completion-ignore-case off")
else:
readline.parse_and_bind("set completion-ignore-case on")
_RL_PREV_CASE_SENSITIVE_COMPLETIONS = case_sensitive
def completedefault(self, text, line, begidx, endidx):
"""Implements tab-completion for text."""
rl_completion_suppress_append() # this needs to be called each time
self._rebind_case_sensitive_completions()
mline = line.partition(' ')[2]
offs = len(mline) - len(text)
if self.completer is None: