Example #1
0
    def __init__(
            self,
            registry=None,  # XXX: not used anymore.
            enable_vi_mode=None,  # (`enable_vi_mode` is deprecated.)
            enable_all=True,  #
            get_search_state=None,
            enable_abort_and_exit_bindings=False,
            enable_system_bindings=False,
            enable_search=False,
            enable_open_in_editor=False,
            enable_extra_page_navigation=False,
            enable_auto_suggest_bindings=False):

        assert registry is None or isinstance(registry, Registry)
        assert get_search_state is None or callable(get_search_state)
        enable_all = to_cli_filter(enable_all)

        defaults = load_key_bindings(
            get_search_state=get_search_state,
            enable_abort_and_exit_bindings=enable_abort_and_exit_bindings,
            enable_system_bindings=enable_system_bindings,
            enable_search=enable_search,
            enable_open_in_editor=enable_open_in_editor,
            enable_extra_page_navigation=enable_extra_page_navigation,
            enable_auto_suggest_bindings=enable_auto_suggest_bindings)

        # Note, we wrap this whole thing again in a MergedRegistry, because we
        # don't want the `enable_all` settings to apply on items that were
        # added to the registry as a whole.
        self.registry = MergedRegistry(
            [ConditionalRegistry(defaults, enable_all)])
Example #2
0
    def __init__(self, pymux):
        self.pymux = pymux

        def get_search_state(cli):
            " Return the currently active SearchState. (The one for the focussed pane.) "
            return pymux.arrangement.get_active_pane(cli).search_state

        # Start from this KeyBindingManager from prompt_toolkit, to have basic
        # editing functionality for the command line. These key binding are
        # however only active when the following `enable_all` condition is met.
        self.registry = MergedRegistry([
            ConditionalRegistry(
                registry=load_key_bindings(
                    enable_auto_suggest_bindings=True,
                    enable_search=
                    False,  # We have our own search bindings, that support multiple panes.
                    enable_extra_page_navigation=True,
                    get_search_state=get_search_state),
                filter=(HasFocus(COMMAND) | HasFocus(PROMPT)
                        | InScrollBuffer(pymux)) & ~HasPrefix(pymux),
            ),
            load_mouse_bindings(),
            self._load_builtins(),
            _load_search_bindings(pymux),
        ])

        self._prefix = (Keys.ControlB, )
        self._prefix_binding = None

        # Load initial bindings.
        self._load_prefix_binding()

        # Custom user configured key bindings.
        # { (needs_prefix, key) -> (command, handler) }
        self.custom_bindings = {}
Example #3
0
def _load_key_bindings(*registries):
    registry = MergedRegistry([
        load_abort_and_exit_bindings(),
        load_basic_system_bindings(),
        load_mouse_bindings(),
        load_cpr_bindings(),
    ] + list(registries))
    return registry
Example #4
0
def get_key_bindings(tosh):
    global_registry = KeyBindingManager.for_prompt(enable_all=Condition(_in_prompt_tab)).registry

    mouse_registry = ConditionalRegistry(load_mouse_bindings(), Condition(_in_interactive_tab))
    prompt_registry = ConditionalRegistry(filter=Condition(_in_prompt_tab))
    interactive_registry = ConditionalRegistry(filter=Condition(_in_interactive_tab))

    @global_registry.add_binding(Keys.BracketedPaste)
    def _paste(event):
        tosh.window.active_tab().paste(event)

    @prompt_registry.add_binding(Keys.ControlW)
    @interactive_registry.add_binding(Keys.ControlB, 'w')
    def _close_tab(event):
        tosh.window.active_tab().close()

    @prompt_registry.add_binding(Keys.ControlT)
    @interactive_registry.add_binding(Keys.ControlB, 't')
    def _new_prompt_tab(event):
        from .tosh_tab import ToshTab
        tosh.window.add_tab(ToshTab(tosh))

    @interactive_registry.add_binding(Keys.Any)
    def _forward_to_session(event):
        tosh.window.active_tab().write_to_ssh(prompt_toolkit_key_to_vt100_key(event.key_sequence[0].key, True))

    @global_registry.add_binding(Keys.ControlB, '1')
    @global_registry.add_binding(Keys.ControlB, '2')
    @global_registry.add_binding(Keys.ControlB, '3')
    @global_registry.add_binding(Keys.ControlB, '4')
    @global_registry.add_binding(Keys.ControlB, '5')
    @global_registry.add_binding(Keys.ControlB, '6')
    @global_registry.add_binding(Keys.ControlB, '7')
    @global_registry.add_binding(Keys.ControlB, '8')
    @global_registry.add_binding(Keys.ControlB, '9')
    def _change_tab(event):
        tosh.window.switch_tab(int(event.key_sequence[-1].key) - 1)

    @global_registry.add_binding(Keys.ControlB, Keys.Left)
    def _prev_tab(_):
        tosh.window.prev_tab()

    @global_registry.add_binding(Keys.ControlB, Keys.Right)
    def _next_tab(_):
        tosh.window.next_tab()

    return MergedRegistry([global_registry, prompt_registry, interactive_registry, mouse_registry])
Example #5
0
def load_key_bindings(get_search_state=None,
                      enable_abort_and_exit_bindings=False,
                      enable_system_bindings=False,
                      enable_search=False,
                      enable_open_in_editor=False,
                      enable_extra_page_navigation=False,
                      enable_auto_suggest_bindings=False):
    """
    Create a Registry object that contains the default key bindings.

    :param enable_abort_and_exit_bindings: Filter to enable Ctrl-C and Ctrl-D.
    :param enable_system_bindings: Filter to enable the system bindings (meta-!
            prompt and Control-Z suspension.)
    :param enable_search: Filter to enable the search bindings.
    :param enable_open_in_editor: Filter to enable open-in-editor.
    :param enable_open_in_editor: Filter to enable open-in-editor.
    :param enable_extra_page_navigation: Filter for enabling extra page
        navigation. (Bindings for up/down scrolling through long pages, like in
        Emacs or Vi.)
    :param enable_auto_suggest_bindings: Filter to enable fish-style suggestions.
    """

    assert get_search_state is None or callable(get_search_state)

    # Accept both Filters and booleans as input.
    enable_abort_and_exit_bindings = to_cli_filter(
        enable_abort_and_exit_bindings)
    enable_system_bindings = to_cli_filter(enable_system_bindings)
    enable_search = to_cli_filter(enable_search)
    enable_open_in_editor = to_cli_filter(enable_open_in_editor)
    enable_extra_page_navigation = to_cli_filter(enable_extra_page_navigation)
    enable_auto_suggest_bindings = to_cli_filter(enable_auto_suggest_bindings)

    registry = MergedRegistry([
        # Load basic bindings.
        load_basic_bindings(),
        load_mouse_bindings(),
        ConditionalRegistry(load_abort_and_exit_bindings(),
                            enable_abort_and_exit_bindings),
        ConditionalRegistry(load_basic_system_bindings(),
                            enable_system_bindings),

        # Load emacs bindings.
        load_emacs_bindings(),
        ConditionalRegistry(load_emacs_open_in_editor_bindings(),
                            enable_open_in_editor),
        ConditionalRegistry(
            load_emacs_search_bindings(get_search_state=get_search_state),
            enable_search),
        ConditionalRegistry(load_emacs_system_bindings(),
                            enable_system_bindings),
        ConditionalRegistry(load_extra_emacs_page_navigation_bindings(),
                            enable_extra_page_navigation),

        # Load Vi bindings.
        load_vi_bindings(get_search_state=get_search_state),
        ConditionalRegistry(load_vi_open_in_editor_bindings(),
                            enable_open_in_editor),
        ConditionalRegistry(
            load_vi_search_bindings(get_search_state=get_search_state),
            enable_search),
        ConditionalRegistry(load_vi_system_bindings(), enable_system_bindings),
        ConditionalRegistry(load_extra_vi_page_navigation_bindings(),
                            enable_extra_page_navigation),

        # Suggestion bindings.
        # (This has to come at the end, because the Vi bindings also have an
        # implementation for the "right arrow", but we really want the
        # suggestion binding when a suggestion is available.)
        ConditionalRegistry(load_auto_suggestion_bindings(),
                            enable_auto_suggest_bindings),
    ])

    return registry
Example #6
0
    def __init__(
            self,
            get_globals=None,
            get_locals=None,
            history_filename=None,
            vi_mode=False,

            # For internal use.
            _completer=None,
            _validator=None,
            _lexer=None,
            _extra_buffers=None,
            _extra_buffer_processors=None,
            _on_start=None,
            _extra_layout_body=None,
            _extra_toolbars=None,
            _input_buffer_height=None,
            _accept_action=AcceptAction.RETURN_DOCUMENT,
            _on_exit=AbortAction.RAISE_EXCEPTION):

        self.get_globals = get_globals or (lambda: {})
        self.get_locals = get_locals or self.get_globals

        self._completer = _completer or PythonCompleter(
            self.get_globals, self.get_locals)
        self._validator = _validator or PythonValidator(
            self.get_compiler_flags)
        self.history = FileHistory(
            history_filename) if history_filename else InMemoryHistory()
        self._lexer = _lexer or PygmentsLexer(PythonLexer)
        self._extra_buffers = _extra_buffers
        self._accept_action = _accept_action
        self._on_exit = _on_exit
        self._on_start = _on_start

        self._input_buffer_height = _input_buffer_height
        self._extra_layout_body = _extra_layout_body or []
        self._extra_toolbars = _extra_toolbars or []
        self._extra_buffer_processors = _extra_buffer_processors or []

        # Settings.
        self.show_signature = False
        self.show_docstring = False
        self.show_meta_enter_message = True
        self.completion_visualisation = CompletionVisualisation.MULTI_COLUMN
        self.completion_menu_scroll_offset = 1

        self.show_line_numbers = False
        self.show_status_bar = True
        self.wrap_lines = True
        self.complete_while_typing = True
        self.vi_mode = vi_mode
        self.paste_mode = False  # When True, don't insert whitespace after newline.
        self.confirm_exit = True  # Ask for confirmation when Control-D is pressed.
        self.accept_input_on_enter = 2  # Accept when pressing Enter 'n' times.
        # 'None' means that meta-enter is always required.
        self.enable_open_in_editor = True
        self.enable_system_bindings = True
        self.enable_input_validation = True
        self.enable_auto_suggest = False
        self.enable_mouse_support = False
        self.enable_history_search = False  # When True, like readline, going
        # back in history will filter the
        # history on the records starting
        # with the current input.

        self.highlight_matching_parenthesis = False
        self.show_sidebar = False  # Currently show the sidebar.
        self.show_sidebar_help = True  # When the sidebar is visible, also show the help text.
        self.show_exit_confirmation = False  # Currently show 'Do you really want to exit?'
        self.terminal_title = None  # The title to be displayed in the terminal. (None or string.)
        self.exit_message = 'Do you really want to exit?'
        self.insert_blank_line_after_output = True  # (For the REPL.)

        # Tokens to be shown at the prompt.
        self.prompt_style = 'classic'  # The currently active style.

        self.all_prompt_styles = {  # Styles selectable from the menu.
            'ipython': IPythonPrompt(self),
            'classic': ClassicPrompt(),
        }

        self.get_input_prompt_tokens = lambda cli: \
            self.all_prompt_styles[self.prompt_style].in_tokens(cli)

        self.get_output_prompt_tokens = lambda cli: \
            self.all_prompt_styles[self.prompt_style].out_tokens(cli)

        #: Load styles.
        self.code_styles = get_all_code_styles()
        self.ui_styles = get_all_ui_styles()
        self._current_code_style_name = 'default'
        self._current_ui_style_name = 'default'

        if is_windows():
            self._current_code_style_name = 'win32'

        self._current_style = self._generate_style()
        self.true_color = False

        # Options to be configurable from the sidebar.
        self.options = self._create_options()
        self.selected_option_index = 0

        #: Incremeting integer counting the current statement.
        self.current_statement_index = 1

        # Code signatures. (This is set asynchronously after a timeout.)
        self.signatures = []

        # Create a Registry for the key bindings.
        self.key_bindings_registry = MergedRegistry([
            ConditionalRegistry(
                registry=load_key_bindings_for_prompt(
                    enable_abort_and_exit_bindings=True,
                    enable_search=True,
                    enable_open_in_editor=Condition(
                        lambda cli: self.enable_open_in_editor),
                    enable_system_bindings=Condition(
                        lambda cli: self.enable_system_bindings),
                    enable_auto_suggest_bindings=Condition(
                        lambda cli: self.enable_auto_suggest)),

                # Disable all default key bindings when the sidebar or the exit confirmation
                # are shown.
                filter=Condition(lambda cli: not (self.show_sidebar or self.
                                                  show_exit_confirmation))),
            load_mouse_bindings(),
            load_python_bindings(self),
            load_sidebar_bindings(self),
            load_confirm_exit_bindings(self),
        ])

        # Boolean indicating whether we have a signatures thread running.
        # (Never run more than one at the same time.)
        self._get_signatures_thread_running = False