Exemple #1
0
 def __init__(self, *args, **kwargs):
     # If the lexor is loaded as a pygment plugin, we have to mock
     # __xonsh_env__ and __xonsh_commands_cache__
     if not hasattr(builtins, '__xonsh_env__'):
         setattr(builtins, '__xonsh_env__', {})
         if ON_WINDOWS:
             pathext = os_environ.get('PATHEXT', ['.EXE', '.BAT', '.CMD'])
             builtins.__xonsh_env__['PATHEXT'] = pathext.split(os.pathsep)
     if not hasattr(builtins, '__xonsh_commands_cache__'):
         setattr(builtins, '__xonsh_commands_cache__', CommandsCache())
     _ = builtins.__xonsh_commands_cache__.all_commands  # NOQA
     super().__init__(*args, **kwargs)
Exemple #2
0
 def __init__(self, *args, **kwargs):
     # If the lexor is loaded as a pygment plugin, we have to mock
     # __xonsh_env__ and __xonsh_commands_cache__
     if not hasattr(builtins, '__xonsh_env__'):
         setattr(builtins, '__xonsh_env__', {})
         if ON_WINDOWS:
             pathext = os_environ.get('PATHEXT', ['.EXE', '.BAT', '.CMD'])
             builtins.__xonsh_env__['PATHEXT'] = pathext.split(os.pathsep)
     if not hasattr(builtins, '__xonsh_commands_cache__'):
         setattr(builtins, '__xonsh_commands_cache__', CommandsCache())
     _ = builtins.__xonsh_commands_cache__.all_commands  # NOQA
     super().__init__(*args, **kwargs)
Exemple #3
0
 def __init__(self, *args, **kwargs):
     # If the lexor is loaded as a pygment plugin, we have to mock
     # __xonsh_env__ and __xonsh_commands_cache__
     if not hasattr(builtins, "__xonsh_env__"):
         setattr(builtins, "__xonsh_env__", {})
         if ON_WINDOWS:
             pathext = os_environ.get("PATHEXT", [".EXE", ".BAT", ".CMD"])
             builtins.__xonsh_env__["PATHEXT"] = pathext.split(os.pathsep)
     if not hasattr(builtins, "__xonsh_commands_cache__"):
         setattr(builtins, "__xonsh_commands_cache__", CommandsCache())
     _ = builtins.__xonsh_commands_cache__.all_commands  # NOQA
     super().__init__(*args, **kwargs)
Exemple #4
0
    def __init__(self, *args, **kwargs):
        # If the lexer is loaded as a pygment plugin, we have to mock
        # __xonsh__.env and __xonsh__.commands_cache
        if not hasattr(builtins, "__xonsh__"):
            from argparse import Namespace

            setattr(builtins, "__xonsh__", Namespace())
        if not hasattr(builtins.__xonsh__, "env"):
            setattr(builtins.__xonsh__, "env", {})
            if ON_WINDOWS:
                pathext = os_environ.get("PATHEXT", [".EXE", ".BAT", ".CMD"])
                builtins.__xonsh__.env["PATHEXT"] = pathext.split(os.pathsep)
        if not hasattr(builtins.__xonsh__, "commands_cache"):
            setattr(builtins.__xonsh__, "commands_cache", CommandsCache())
        _ = builtins.__xonsh__.commands_cache.all_commands  # NOQA
        super().__init__(*args, **kwargs)
Exemple #5
0
    def __init__(self, *args, **kwargs):
        # If the lexer is loaded as a pygment plugin, we have to mock
        # __xonsh__.env and __xonsh__.commands_cache
        if not hasattr(builtins, "__xonsh__"):
            from argparse import Namespace

            setattr(builtins, "__xonsh__", Namespace())
        if not hasattr(builtins.__xonsh__, "env"):
            setattr(builtins.__xonsh__, "env", {})
            if ON_WINDOWS:
                pathext = os_environ.get("PATHEXT", [".EXE", ".BAT", ".CMD"])
                builtins.__xonsh__.env["PATHEXT"] = pathext.split(os.pathsep)
        if not hasattr(builtins.__xonsh__, "commands_cache"):
            setattr(builtins.__xonsh__, "commands_cache", CommandsCache())
        _ = builtins.__xonsh__.commands_cache.all_commands  # NOQA
        super().__init__(*args, **kwargs)
Exemple #6
0
def setup_readline():
    """Sets up the readline module and completion suppression, if available."""
    global RL_COMPLETION_SUPPRESS_APPEND, RL_LIB, RL_CAN_RESIZE, RL_STATE, readline, RL_COMPLETION_QUERY_ITEMS
    if RL_COMPLETION_SUPPRESS_APPEND is not None:
        return
    for _rlmod_name in ("gnureadline", "readline"):
        try:
            readline = importlib.import_module(_rlmod_name)
            sys.modules["readline"] = readline
        except ImportError:
            pass
        else:
            break

    if readline is None:
        print(
            """Skipping setup. Because no `readline` implementation available.
            Please install a backend (`readline`, `prompt-toolkit`, etc) to use
            `xonsh` interactively.
            See https://github.com/xonsh/xonsh/issues/1170""")
        return

    import ctypes
    import ctypes.util

    uses_libedit = readline.__doc__ and "libedit" in readline.__doc__
    readline.set_completer_delims(" \t\n")
    # Cygwin seems to hang indefinitely when querying the readline lib
    if (not ON_CYGWIN) and (not ON_MSYS) and (
            not readline.__file__.endswith(".py")):
        RL_LIB = lib = ctypes.cdll.LoadLibrary(readline.__file__)
        try:
            RL_COMPLETION_SUPPRESS_APPEND = ctypes.c_int.in_dll(
                lib, "rl_completion_suppress_append")
        except ValueError:
            # not all versions of readline have this symbol, ie Macs sometimes
            RL_COMPLETION_SUPPRESS_APPEND = None
        try:
            RL_COMPLETION_QUERY_ITEMS = ctypes.c_int.in_dll(
                lib, "rl_completion_query_items")
        except ValueError:
            # not all versions of readline have this symbol, ie Macs sometimes
            RL_COMPLETION_QUERY_ITEMS = None
        try:
            RL_STATE = ctypes.c_int.in_dll(lib, "rl_readline_state")
        except Exception:
            pass
        RL_CAN_RESIZE = hasattr(lib, "rl_reset_screen_size")
    env = XSH.env
    # reads in history
    readline.set_history_length(-1)
    ReadlineHistoryAdder()
    # sets up IPython-like history matching with up and down
    readline.parse_and_bind('"\\e[B": history-search-forward')
    readline.parse_and_bind('"\\e[A": history-search-backward')
    # Setup Shift-Tab to indent
    readline.parse_and_bind('"\\e[Z": "{0}"'.format(env.get("INDENT")))

    # handle tab completion differences found in libedit readline compatibility
    # as discussed at http://stackoverflow.com/a/7116997
    if uses_libedit and ON_DARWIN:
        readline.parse_and_bind("bind ^I rl_complete")
        print(
            "\n".join([
                "",
                "*" * 78,
                "libedit detected - readline will not be well behaved, including but not limited to:",
                "   * crashes on tab completion",
                "   * incorrect history navigation",
                "   * corrupting long-lines",
                "   * failure to wrap or indent lines properly",
                "",
                "It is highly recommended that you install gnureadline, which is installable with:",
                "     xpip install gnureadline",
                "*" * 78,
            ]),
            file=sys.stderr,
        )
    else:
        readline.parse_and_bind("tab: complete")
    # try to load custom user settings
    inputrc_name = os_environ.get("INPUTRC")
    if inputrc_name is None:
        if uses_libedit:
            inputrc_name = ".editrc"
        else:
            inputrc_name = ".inputrc"
        inputrc_name = os.path.join(os.path.expanduser("~"), inputrc_name)
    if (not ON_WINDOWS) and (not os.path.isfile(inputrc_name)):
        inputrc_name = "/etc/inputrc"
    if ON_WINDOWS:
        winutils.enable_virtual_terminal_processing()
    if os.path.isfile(inputrc_name):
        try:
            readline.read_init_file(inputrc_name)
        except Exception:
            # this seems to fail with libedit
            print_exception(
                "xonsh: could not load readline default init file.")

    # Protection against paste jacking (issue #1154)
    # This must be set after the init file is loaded since read_init_file()
    # automatically disables bracketed paste
    # (https://github.com/python/cpython/pull/24108)
    readline.parse_and_bind("set enable-bracketed-paste on")

    # properly reset input typed before the first prompt
    readline.set_startup_hook(carriage_return)
def setup_readline():
    """Sets up the readline module and completion suppression, if available."""
    global RL_COMPLETION_SUPPRESS_APPEND, RL_LIB, RL_CAN_RESIZE, RL_STATE, readline, RL_COMPLETION_QUERY_ITEMS
    if RL_COMPLETION_SUPPRESS_APPEND is not None:
        return
    for _rlmod_name in ("gnureadline", "readline"):
        try:
            readline = importlib.import_module(_rlmod_name)
            sys.modules["readline"] = readline
        except ImportError:
            pass
        else:
            break

    if readline is None:
        print(
            """Skipping setup. Because no `readline` implementation available.
            Please install a backend (`readline`, `prompt-toolkit`, etc) to use
            `xonsh` interactively.
            See https://github.com/xonsh/xonsh/issues/1170"""
        )
        return

    import ctypes
    import ctypes.util

    uses_libedit = readline.__doc__ and "libedit" in readline.__doc__
    readline.set_completer_delims(" \t\n")
    # Cygwin seems to hang indefinitely when querying the readline lib
    if (not ON_CYGWIN) and (not ON_MSYS) and (not readline.__file__.endswith(".py")):
        RL_LIB = lib = ctypes.cdll.LoadLibrary(readline.__file__)
        try:
            RL_COMPLETION_SUPPRESS_APPEND = ctypes.c_int.in_dll(
                lib, "rl_completion_suppress_append"
            )
        except ValueError:
            # not all versions of readline have this symbol, ie Macs sometimes
            RL_COMPLETION_SUPPRESS_APPEND = None
        try:
            RL_COMPLETION_QUERY_ITEMS = ctypes.c_int.in_dll(
                lib, "rl_completion_query_items"
            )
        except ValueError:
            # not all versions of readline have this symbol, ie Macs sometimes
            RL_COMPLETION_QUERY_ITEMS = None
        try:
            RL_STATE = ctypes.c_int.in_dll(lib, "rl_readline_state")
        except Exception:
            pass
        RL_CAN_RESIZE = hasattr(lib, "rl_reset_screen_size")
    env = builtins.__xonsh__.env
    # reads in history
    readline.set_history_length(-1)
    ReadlineHistoryAdder()
    # sets up IPython-like history matching with up and down
    readline.parse_and_bind('"\\e[B": history-search-forward')
    readline.parse_and_bind('"\\e[A": history-search-backward')
    # Setup Shift-Tab to indent
    readline.parse_and_bind('"\\e[Z": "{0}"'.format(env.get("INDENT")))

    # handle tab completion differences found in libedit readline compatibility
    # as discussed at http://stackoverflow.com/a/7116997
    if uses_libedit and ON_DARWIN:
        readline.parse_and_bind("bind ^I rl_complete")
        print(
            "\n".join(
                [
                    "",
                    "*" * 78,
                    "libedit detected - readline will not be well behaved, including but not limited to:",
                    "   * crashes on tab completion",
                    "   * incorrect history navigation",
                    "   * corrupting long-lines",
                    "   * failure to wrap or indent lines properly",
                    "",
                    "It is highly recommended that you install gnureadline, which is installable with:",
                    "     xpip install gnureadline",
                    "*" * 78,
                ]
            ),
            file=sys.stderr,
        )
    else:
        readline.parse_and_bind("tab: complete")
    # try to load custom user settings
    inputrc_name = os_environ.get("INPUTRC")
    if inputrc_name is None:
        if uses_libedit:
            inputrc_name = ".editrc"
        else:
            inputrc_name = ".inputrc"
        inputrc_name = os.path.join(os.path.expanduser("~"), inputrc_name)
    if (not ON_WINDOWS) and (not os.path.isfile(inputrc_name)):
        inputrc_name = "/etc/inputrc"
    if ON_WINDOWS:
        winutils.enable_virtual_terminal_processing()
    if os.path.isfile(inputrc_name):
        try:
            readline.read_init_file(inputrc_name)
        except Exception:
            # this seems to fail with libedit
            print_exception("xonsh: could not load readline default init file.")
    # properly reset input typed before the first prompt
    readline.set_startup_hook(carriage_return)