Exemple #1
0
def create_layout(python_input,
                  lexer=PythonLexer,
                  extra_body=None,
                  extra_toolbars=None,
                  extra_buffer_processors=None,
                  input_buffer_height=None):
    D = Dimension
    extra_body = [extra_body] if extra_body else []
    extra_toolbars = extra_toolbars or []
    extra_buffer_processors = extra_buffer_processors or []
    input_buffer_height = input_buffer_height or D(min=6)

    search_toolbar = SearchToolbar(python_input.search_buffer)

    def create_python_input_window():
        def menu_position():
            """
            When there is no autocompletion menu to be shown, and we have a signature,
            set the pop-up position at `bracket_start`.
            """
            b = python_input.default_buffer

            if b.complete_state is None and python_input.signatures:
                row, col = python_input.signatures[0].bracket_start
                index = b.document.translate_row_col_to_index(row - 1, col)
                return index

        return Window(
            BufferControl(
                buffer=python_input.default_buffer,
                search_buffer_control=search_toolbar.control,
                lexer=lexer,
                include_default_input_processors=False,
                input_processors=[
                    ConditionalProcessor(
                        processor=HighlightIncrementalSearchProcessor(),
                        filter=has_focus(SEARCH_BUFFER)
                        | has_focus(search_toolbar.control),
                    ),
                    HighlightSelectionProcessor(),
                    DisplayMultipleCursors(),
                    # Show matching parentheses, but only while editing.
                    ConditionalProcessor(
                        processor=HighlightMatchingBracketProcessor(
                            chars='[](){}'),
                        filter=has_focus(DEFAULT_BUFFER) & ~is_done
                        & Condition(lambda: python_input.
                                    highlight_matching_parenthesis)),
                    ConditionalProcessor(processor=AppendAutoSuggestion(),
                                         filter=~is_done)
                ] + extra_buffer_processors,
                menu_position=menu_position,

                # Make sure that we always see the result of an reverse-i-search:
                preview_search=True,
            ),
            left_margins=[PythonPromptMargin(python_input)],
            # Scroll offsets. The 1 at the bottom is important to make sure the
            # cursor is never below the "Press [Meta+Enter]" message which is a float.
            scroll_offsets=ScrollOffsets(bottom=1, left=4, right=4),
            # As long as we're editing, prefer a minimal height of 6.
            height=(lambda: (None if get_app().is_done or python_input.
                             show_exit_confirmation else input_buffer_height)),
            wrap_lines=Condition(lambda: python_input.wrap_lines),
        )

    root_container = HSplit([
        VSplit([
            HSplit([
                FloatContainer(
                    content=HSplit([create_python_input_window()] +
                                   extra_body),
                    floats=[
                        Float(xcursor=True,
                              ycursor=True,
                              content=ConditionalContainer(
                                  content=CompletionsMenu(scroll_offset=(
                                      lambda: python_input.
                                      completion_menu_scroll_offset),
                                                          max_height=12),
                                  filter=show_completions_menu(python_input))),
                        Float(xcursor=True,
                              ycursor=True,
                              content=ConditionalContainer(
                                  content=MultiColumnCompletionsMenu(),
                                  filter=show_multi_column_completions_menu(
                                      python_input))),
                        Float(xcursor=True,
                              ycursor=True,
                              content=signature_toolbar(python_input)),
                        Float(left=2,
                              bottom=1,
                              content=exit_confirmation(python_input)),
                        Float(bottom=0,
                              right=0,
                              height=1,
                              content=meta_enter_message(python_input),
                              hide_when_covering_content=True),
                        Float(bottom=1,
                              left=1,
                              right=0,
                              content=python_sidebar_help(python_input)),
                    ]),
                ArgToolbar(),
                search_toolbar,
                SystemToolbar(),
                ValidationToolbar(),
                ConditionalContainer(content=CompletionsToolbar(),
                                     filter=show_completions_toolbar(
                                         python_input)),

                # Docstring region.
                ConditionalContainer(content=Window(
                    height=D.exact(1), char='\u2500', style='class:separator'),
                                     filter=HasSignature(python_input)
                                     & ShowDocstring(python_input) & ~is_done),
                ConditionalContainer(
                    content=Window(
                        BufferControl(
                            buffer=python_input.docstring_buffer,
                            lexer=SimpleLexer(style='class:docstring'),
                            #lexer=PythonLexer,
                        ),
                        height=D(max=12)),
                    filter=HasSignature(python_input)
                    & ShowDocstring(python_input) & ~is_done),
            ]),
            ConditionalContainer(content=HSplit([
                python_sidebar(python_input),
                Window(style='class:sidebar,separator', height=1),
                python_sidebar_navigation(python_input),
            ]),
                                 filter=ShowSidebar(python_input) & ~is_done)
        ]),
    ] + extra_toolbars + [
        VSplit([
            status_bar(python_input),
            show_sidebar_button_info(python_input),
        ])
    ])

    return Layout(root_container)
Exemple #2
0
    def _create_layout(self):
        """
        Create `Layout` for this prompt.
        """
        dyncond = self._dyncond

        # Create functions that will dynamically split the prompt. (If we have
        # a multiline prompt.)
        has_before_fragments, get_prompt_text_1, get_prompt_text_2 = \
            _split_multiline_prompt(self._get_prompt)

        default_buffer = self.default_buffer
        search_buffer = self.search_buffer

        # Create processors list.
        all_input_processors = [
            HighlightIncrementalSearchProcessor(),
            HighlightSelectionProcessor(),
            ConditionalProcessor(AppendAutoSuggestion(),
                                 has_focus(default_buffer) & ~is_done),
            ConditionalProcessor(PasswordProcessor(), dyncond('is_password')),
            DisplayMultipleCursors(),

            # Users can insert processors here.
            DynamicProcessor(
                lambda: merge_processors(self.input_processors or [])),

            # For single line mode, show the prompt before the input.
            ConditionalProcessor(
                merge_processors([
                    BeforeInput(get_prompt_text_2),
                    ShowArg(),
                ]), ~dyncond('multiline'))
        ]

        # Create bottom toolbars.
        bottom_toolbar = ConditionalContainer(
            Window(FormattedTextControl(lambda: self.bottom_toolbar,
                                        style='class:bottom-toolbar.text'),
                   style='class:bottom-toolbar',
                   dont_extend_height=True,
                   height=Dimension(min=1)),
            filter=~is_done & renderer_height_is_known
            & Condition(lambda: self.bottom_toolbar is not None))

        search_toolbar = SearchToolbar(
            search_buffer, ignore_case=dyncond('search_ignore_case'))

        search_buffer_control = SearchBufferControl(
            buffer=search_buffer,
            input_processors=[
                ReverseSearchProcessor(),
                ShowArg(),
            ],
            ignore_case=dyncond('search_ignore_case'))

        system_toolbar = SystemToolbar(
            enable_global_bindings=dyncond('enable_system_prompt'))

        def get_search_buffer_control():
            " Return the UIControl to be focused when searching start. "
            if _true(self.multiline):
                return search_toolbar.control
            else:
                return search_buffer_control

        default_buffer_control = BufferControl(
            buffer=default_buffer,
            search_buffer_control=get_search_buffer_control,
            input_processors=all_input_processors,
            include_default_input_processors=False,
            lexer=DynamicLexer(lambda: self.lexer),
            preview_search=True)

        default_buffer_window = Window(
            default_buffer_control,
            height=self._get_default_buffer_control_height,
            left_margins=[
                # In multiline mode, use the window margin to display
                # the prompt and continuation fragments.
                ConditionalMargin(
                    PromptMargin(get_prompt_text_2, self._get_continuation),
                    filter=dyncond('multiline'),
                )
            ],
            wrap_lines=dyncond('wrap_lines'))

        @Condition
        def multi_column_complete_style():
            return self.complete_style == CompleteStyle.MULTI_COLUMN

        # Build the layout.
        layout = HSplit([
            # The main input, with completion menus floating on top of it.
            FloatContainer(
                HSplit([
                    ConditionalContainer(
                        Window(FormattedTextControl(get_prompt_text_1),
                               dont_extend_height=True),
                        Condition(has_before_fragments)),
                    ConditionalContainer(
                        default_buffer_window,
                        Condition(lambda: get_app().layout.current_control !=
                                  search_buffer_control),
                    ),
                    ConditionalContainer(
                        Window(search_buffer_control),
                        Condition(lambda: get_app().layout.current_control ==
                                  search_buffer_control),
                    ),
                ]),
                [
                    # Completion menus.
                    Float(xcursor=True,
                          ycursor=True,
                          content=CompletionsMenu(
                              max_height=16,
                              scroll_offset=1,
                              extra_filter=has_focus(default_buffer)
                              & ~multi_column_complete_style)),
                    Float(xcursor=True,
                          ycursor=True,
                          content=MultiColumnCompletionsMenu(
                              show_meta=True,
                              extra_filter=has_focus(default_buffer)
                              & multi_column_complete_style)),
                    # The right prompt.
                    Float(right=0,
                          top=0,
                          hide_when_covering_content=True,
                          content=_RPrompt(lambda: self.rprompt)),
                ]),
            ConditionalContainer(ValidationToolbar(), filter=~is_done),
            ConditionalContainer(system_toolbar,
                                 dyncond('enable_system_prompt') & ~is_done),

            # In multiline mode, we use two toolbars for 'arg' and 'search'.
            ConditionalContainer(
                Window(FormattedTextControl(self._get_arg_text), height=1),
                dyncond('multiline') & has_arg),
            ConditionalContainer(search_toolbar,
                                 dyncond('multiline') & ~is_done),
            bottom_toolbar,
        ])

        return Layout(layout, default_buffer_window)
Exemple #3
0
    def __init__(self, history):
        search_toolbar = SearchToolbar()

        self.help_buffer_control = BufferControl(
            buffer=history.help_buffer,
            lexer=PygmentsLexer(RstLexer))

        help_window = _create_popup_window(
            title='History Help',
            body=Window(
                content=self.help_buffer_control,
                right_margins=[ScrollbarMargin(display_arrows=True)],
                scroll_offsets=ScrollOffsets(top=2, bottom=2)))

        self.default_buffer_control = BufferControl(
            buffer=history.default_buffer,
            input_processors=[GrayExistingText(history.history_mapping)],
            lexer=PygmentsLexer(PythonLexer))

        self.history_buffer_control = BufferControl(
            buffer=history.history_buffer,
            lexer=PygmentsLexer(PythonLexer),
            search_buffer_control=search_toolbar.control,
            preview_search=True)

        history_window = Window(
            content=self.history_buffer_control,
            wrap_lines=False,
            left_margins=[HistoryMargin(history)],
            scroll_offsets=ScrollOffsets(top=2, bottom=2))

        self.root_container = HSplit([
            #  Top title bar.
            Window(
                content=FormattedTextControl(_get_top_toolbar_fragments),
                align=WindowAlign.CENTER,
                style='class:status-toolbar'),
            FloatContainer(
                content=VSplit([
                    # Left side: history.
                    history_window,
                    # Separator.
                    Window(width=D.exact(1),
                           char=BORDER.LIGHT_VERTICAL,
                           style='class:separator'),
                    # Right side: result.
                    Window(
                        content=self.default_buffer_control,
                        wrap_lines=False,
                        left_margins=[ResultMargin(history)],
                        scroll_offsets=ScrollOffsets(top=2, bottom=2)),
                ]),
                floats=[
                    # Help text as a float.
                    Float(width=60, top=3, bottom=2,
                          content=ConditionalContainer(
                              content=help_window, filter=has_focus(history.help_buffer))),
                ]
            ),
            # Bottom toolbars.
            ArgToolbar(),
            search_toolbar,
            Window(
                content=FormattedTextControl(
                    partial(_get_bottom_toolbar_fragments, history=history)),
                style='class:status-toolbar'),
        ])

        self.layout = Layout(self.root_container, history_window)
Exemple #4
0
    def __init__(self,
                 command=['/bin/bash'],
                 before_exec_func=None,
                 bell_func=None,
                 style='',
                 width=None,
                 height=None,
                 done_callback=None):

        self.terminal_control = _TerminalControl(
            command=command,
            before_exec_func=before_exec_func,
            bell_func=bell_func,
            done_callback=done_callback)

        self.terminal_window = _Window(terminal_control=self.terminal_control,
                                       content=self.terminal_control,
                                       wrap_lines=False)

        # Key bindigns for copy buffer.
        kb = KeyBindings()

        @kb.add('c-c')
        def _(event):
            self.exit_copy_mode()

        @kb.add('space')
        def _(event):
            " Reset selection. "
            event.current_buffer.start_selection()

        @kb.add('enter', filter=has_selection)
        def _(event):
            " Reset selection. "
            data = event.current_buffer.copy_selection()
            event.app.clipboard.set_data(data)

        self.search_toolbar = SearchToolbar(
            forward_search_prompt='Search down: ',
            backward_search_prompt='Search up: ')

        self.copy_buffer = Buffer(read_only=True)
        self.copy_buffer_control = BufferControl(
            buffer=self.copy_buffer,
            search_buffer_control=self.search_toolbar.control,
            include_default_input_processors=False,
            input_processors=[
                _UseStyledTextProcessor(self),
                HighlightSelectionProcessor(),
                HighlightSearchProcessor(),
                HighlightIncrementalSearchProcessor(),
            ],
            preview_search=
            True,  # XXX: not sure why we need twice preview_search.
            key_bindings=kb)

        self.copy_window = Window(content=self.copy_buffer_control,
                                  wrap_lines=False)

        self.is_copying = False
        self.styled_copy_lines = [
        ]  # List of lists of (style, text) tuples, for each line.

        @Condition
        def is_copying():
            return self.is_copying

        self.container = FloatContainer(
            content=HSplit(
                [
                    # Either show terminal window or copy buffer.
                    VSplit([  # XXX: this nested VSplit should not have been necessary,
                        # but the ConditionalContainer which width can become
                        # zero will collapse the other elements.
                        ConditionalContainer(self.terminal_window,
                                             filter=~is_copying),
                        ConditionalContainer(self.copy_window,
                                             filter=is_copying),
                    ]),
                    ConditionalContainer(self.search_toolbar,
                                         filter=is_copying),
                ],
                style=style,
                width=width,
                height=height),
            floats=[
                Float(top=0,
                      right=0,
                      height=1,
                      content=ConditionalContainer(Window(
                          content=FormattedTextControl(
                              text=self._copy_position_formatted_text),
                          style='class:copy-mode-cursor-position'),
                                                   filter=is_copying))
            ])
Exemple #5
0
    def __init__(self, editor, window_arrangement):
        self.editor = editor  # Back reference to editor.
        self.window_arrangement = window_arrangement

        # Mapping from (`window_arrangement.Window`, `EditorBuffer`) to a frame
        # (Layout instance).
        # We keep this as a cache in order to easily reuse the same frames when
        # the layout is updated. (We don't want to create new frames on every
        # update call, because that way, we would loose some state, like the
        # vertical scroll offset.)
        self._frames = {}

        self._fc = FloatContainer(
            content=VSplit([
                Window(BufferControl())  # Dummy window
            ]),
            floats=[
                Float(xcursor=True,
                      ycursor=True,
                      content=CompletionsMenu(
                          max_height=12,
                          scroll_offset=2,
                          extra_filter=~has_focus(editor.command_buffer))),
                Float(content=BufferListOverlay(editor), bottom=1, left=0),
                Float(bottom=1,
                      left=0,
                      right=0,
                      height=1,
                      content=ConditionalContainer(
                          CompletionsToolbar(),
                          filter=has_focus(editor.command_buffer)
                          & ~_bufferlist_overlay_visible(editor)
                          & Condition(lambda: editor.show_wildmenu))),
                Float(bottom=1,
                      left=0,
                      right=0,
                      height=1,
                      content=ValidationToolbar()),
                Float(bottom=1,
                      left=0,
                      right=0,
                      height=1,
                      content=MessageToolbarBar(editor)),
                Float(content=WelcomeMessageWindow(editor),
                      height=WELCOME_MESSAGE_HEIGHT,
                      width=WELCOME_MESSAGE_WIDTH),
            ])

        search_toolbar = SearchToolbar(vi_mode=True,
                                       search_buffer=editor.search_buffer)
        self.search_control = search_toolbar.control

        self.layout = Layout(
            FloatContainer(content=HSplit([
                TabsToolbar(editor),
                self._fc,
                CommandLine(editor),
                ReportMessageToolbar(editor),
                SystemToolbar(),
                search_toolbar,
            ]),
                           floats=[
                               Float(right=0,
                                     height=1,
                                     bottom=0,
                                     width=5,
                                     content=SimpleArgToolbar()),
                           ]))
Exemple #6
0
    def __init__(self, pager: "Pager") -> None:
        self.pager = pager
        self.dynamic_body = _DynamicBody(pager)

        # Build an interface.
        has_colon = HasColon(pager)

        self.examine_control: BufferControl = BufferControl(
            buffer=pager.examine_buffer,
            lexer=SimpleLexer(style="class:examine,examine-text"),
            input_processors=[
                BeforeInput(lambda: [("class:examine", " Examine: ")])
            ],
        )

        self.search_toolbar = SearchToolbar(vi_mode=True,
                                            search_buffer=pager.search_buffer)

        self.container = FloatContainer(
            content=HSplit([
                ConditionalContainer(
                    content=Titlebar(pager),
                    filter=Condition(lambda: pager.display_titlebar),
                ),
                self.dynamic_body,
                self.search_toolbar,
                SystemToolbar(),
                ConditionalContainer(
                    content=VSplit([
                        Window(
                            height=1,
                            content=FormattedTextControl(
                                self._get_statusbar_left_tokens),
                            style="class:statusbar",
                        ),
                        Window(
                            height=1,
                            content=FormattedTextControl(
                                self._get_statusbar_right_tokens),
                            style="class:statusbar.cursorposition",
                            align=WindowAlign.RIGHT,
                        ),
                    ]),
                    filter=~HasSearch()
                    & ~has_focus(SYSTEM_BUFFER)
                    & ~has_colon
                    & ~has_focus("EXAMINE"),
                ),
                ConditionalContainer(
                    content=Window(FormattedTextControl(" :"),
                                   height=1,
                                   style="class:examine"),
                    filter=has_colon,
                ),
                ConditionalContainer(
                    content=Window(self.examine_control,
                                   height=1,
                                   style="class:examine"),
                    filter=has_focus(pager.examine_buffer),
                ),
            ]),
            floats=[
                Float(right=0, height=1, bottom=1, content=_Arg()),
                Float(
                    bottom=1,
                    left=0,
                    right=0,
                    height=1,
                    content=ConditionalContainer(
                        content=MessageToolbarBar(pager),
                        filter=Condition(lambda: bool(pager.message)),
                    ),
                ),
                Float(
                    right=0,
                    height=1,
                    bottom=1,
                    content=ConditionalContainer(
                        content=FormattedTextToolbar(
                            lambda: [("class:loading", " Loading... ")], ),
                        filter=Condition(lambda: pager.current_source_info.
                                         waiting_for_input_stream),
                    ),
                ),
                Float(xcursor=True,
                      ycursor=True,
                      content=MultiColumnCompletionsMenu()),
            ],
        )
Exemple #7
0
from prompt_toolkit.widgets.toolbars import CompletionsToolbar, ArgToolbar, SearchToolbar, ValidationToolbar, SystemToolbar
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.layout.processors import AppendAutoSuggestion
from .input_area import UrlArea
from ..completer import HttpPromptCompleter
from prompt_toolkit.lexers import PygmentsLexer
from pygments.lexers.data import JsonLexer
from ..context import Context

http_completer = HttpPromptCompleter(Context())

buffer = Buffer()
help_text = "Http"

search_field = SearchToolbar(
    text_if_not_searching=[('class:not-searching',
                            "Press '/' to start searching.")])

output_field = TextArea(style='class:output-field',
                        scrollbar=True,
                        line_numbers=True,
                        text=help_text,
                        search_field=search_field,
                        lexer=PygmentsLexer(JsonLexer))

input_field = UrlArea(height=3,
                      prompt='>>> ',
                      style='class:input-field',
                      completer=http_completer,
                      accept_handler=True)
Exemple #8
0
    def main(self, data):
        fr = FileReader(data)
        bc = BufferCache()

        all_input_processors = [
            ConditionalProcessor(HighlightSearchProcessor(), ~is_searching),
            HighlightIncrementalSearchProcessor(),
            HighlightSelectionProcessor(),
            DisplayMultipleCursors(),
        ]

        search_toolbar = SearchToolbar(vi_mode=True)

        lst_window = Window(
            BufferControl(
                Buffer(
                    name="file_list",
                    document=Document(fr.get_list_text(), cursor_position=0),
                    read_only=True,
                ),
                search_buffer_control=search_toolbar.control,
                preview_search=True,
                include_default_input_processors=False,
                input_processors=all_input_processors,
            ),
            cursorline=True,
        )

        txt_window = Window(
            BufferControl(
                bc.current_buffer,
                search_buffer_control=search_toolbar.control,
                preview_search=True,
                include_default_input_processors=False,
                input_processors=all_input_processors,
            ),
            wrap_lines=False,
        )

        toolbar = FormattedTextToolbar(text="hello world!",
                                       style="class:buf_name")

        body = HSplit([lst_window])
        # Key bind
        kb = KeyBindings()
        kb.add(Keys.ControlD)(scroll_half_page_down)
        kb.add(Keys.ControlU)(scroll_half_page_up)

        @kb.add(Keys.ControlQ)
        def _(event):
            event.app.exit()

        @kb.add(Keys.Enter, filter=has_focus("file_list"))
        def _(event):
            idx = event.current_buffer.document.cursor_position_row
            path = str(fr.get_path(idx))
            origin_data = str(fr.get_relative_path(idx))
            origin_data2 = origin_data.split("|")[0]
            number = origin_data2.split(":")[-1]
            command = "vim " + "+" + number + " " + path
            res = subprocess.call(command.split())

        @kb.add("k")
        def _(event):
            event.current_buffer.cursor_up()
            idx = event.current_buffer.document.cursor_position_row
            path = str(fr.get_relative_path(idx))
            try:
                set_buffer_txt_window(bc.get_or_append_buffer(path, "AAAAA"))
                set_text_toolbar(bc.current_buffer_name)
            except:
                pass

        @kb.add("j")
        def _(event):
            event.current_buffer.cursor_down()
            idx = event.current_buffer.document.cursor_position_row
            path = str(fr.get_relative_path(idx))
            set_buffer_txt_window(bc.get_or_append_buffer(path, "AAAAA"))
            set_text_toolbar(bc.current_buffer_name)

        def set_buffer_txt_window(buf):
            txt_window.content.buffer = buf

        def set_text_toolbar(text):
            toolbar.content.text = text

        @Condition
        def search_buffer_is_empty():
            return get_app().current_buffer.text == ""

        style = Style([
            ("buf_name", "fg:#dddddd bg:#8a2be2"),
            ("incsearch", "fg:ansibrightyellow reverse"),
        ])

        app = Application(layout=Layout(body),
                          key_bindings=kb,
                          full_screen=True,
                          style=style)

        app.run()
Exemple #9
0
    def _create_application(self, editing_mode, erase_when_done):
        def dyncond(attr_name):
            """
            Dynamically take this setting from this 'Prompt' class.
            `attr_name` represents an attribute name of this class. Its value
            can either be a boolean or a `Filter`.

            This returns something that can be used as either a `Filter`
            or `Filter`.
            """
            @Condition
            def dynamic():
                value = getattr(self, attr_name)
                return to_filter(value)()

            return dynamic

        # Create functions that will dynamically split the prompt. (If we have
        # a multiline prompt.)
        has_before_fragments, get_prompt_text_1, get_prompt_text_2 = \
            _split_multiline_prompt(self._get_prompt)

        # Create buffers list.
        def accept(buff):
            """ Accept the content of the default buffer. This is called when
            the validation succeeds. """
            self.app.set_result(buff.document.text)

            # Reset content before running again.
            self.app.pre_run_callables.append(buff.reset)

        default_buffer = Buffer(
            name=DEFAULT_BUFFER,
            # Make sure that complete_while_typing is disabled when
            # enable_history_search is enabled. (First convert to Filter,
            # to avoid doing bitwise operations on bool objects.)
            complete_while_typing=Condition(
                lambda: _true(self.complete_while_typing) and not _true(
                    self.enable_history_search) and not self.complete_style ==
                CompleteStyle.READLINE_LIKE),
            validate_while_typing=dyncond('validate_while_typing'),
            enable_history_search=dyncond('enable_history_search'),
            validator=DynamicValidator(lambda: self.validator),
            completer=ThreadedCompleter(
                completer=DynamicCompleter(lambda: self.completer),
                in_thread=dyncond('complete_in_thread'),
            ),
            history=DynamicHistory(lambda: self.history),
            auto_suggest=DynamicAutoSuggest(lambda: self.auto_suggest),
            accept_handler=accept,
            get_tempfile_suffix=lambda: self.tempfile_suffix)

        search_buffer = Buffer(name=SEARCH_BUFFER)

        # Create processors list.
        input_processor = merge_processors([
            ConditionalProcessor(
                # By default, only highlight search when the search
                # input has the focus. (Note that this doesn't mean
                # there is no search: the Vi 'n' binding for instance
                # still allows to jump to the next match in
                # navigation mode.)
                HighlightSearchProcessor(preview_search=True),
                has_focus(search_buffer)),
            HighlightSelectionProcessor(),
            ConditionalProcessor(AppendAutoSuggestion(),
                                 has_focus(default_buffer) & ~is_done),
            ConditionalProcessor(PasswordProcessor(), dyncond('is_password')),
            DisplayMultipleCursors(),

            # Users can insert processors here.
            DynamicProcessor(lambda: self.extra_input_processor),

            # For single line mode, show the prompt before the input.
            ConditionalProcessor(
                merge_processors([
                    BeforeInput(get_prompt_text_2),
                    ShowArg(),
                ]), ~dyncond('multiline'))
        ])

        # Create bottom toolbars.
        bottom_toolbar = ConditionalContainer(
            Window(FormattedTextControl(lambda: self.bottom_toolbar,
                                        style='class:bottom-toolbar.text'),
                   style='class:bottom-toolbar',
                   dont_extend_height=True,
                   height=Dimension(min=1)),
            filter=~is_done & renderer_height_is_known
            & Condition(lambda: self.bottom_toolbar is not None))

        search_toolbar = SearchToolbar(
            search_buffer,
            get_search_state=lambda: default_buffer_control.get_search_state())
        search_buffer_control = BufferControl(buffer=search_buffer,
                                              input_processor=merge_processors(
                                                  [
                                                      ReverseSearchProcessor(),
                                                      ShowArg(),
                                                  ]))

        system_toolbar = SystemToolbar()

        def get_search_buffer_control():
            " Return the UIControl to be focused when searching start. "
            if _true(self.multiline):
                return search_toolbar.control
            else:
                return search_buffer_control

        default_buffer_control = BufferControl(
            buffer=default_buffer,
            get_search_buffer_control=get_search_buffer_control,
            input_processor=input_processor,
            lexer=DynamicLexer(lambda: self.lexer),
            preview_search=True)

        default_buffer_window = Window(
            default_buffer_control,
            height=self._get_default_buffer_control_height,
            left_margins=[
                # In multiline mode, use the window margin to display
                # the prompt and continuation fragments.
                ConditionalMargin(
                    PromptMargin(get_prompt_text_2, self._get_continuation),
                    filter=dyncond('multiline'),
                )
            ],
            wrap_lines=dyncond('wrap_lines'))

        @Condition
        def multi_column_complete_style():
            return self.complete_style == CompleteStyle.MULTI_COLUMN

        # Build the layout.
        layout = HSplit([
            # The main input, with completion menus floating on top of it.
            FloatContainer(
                HSplit([
                    ConditionalContainer(
                        Window(FormattedTextControl(get_prompt_text_1),
                               dont_extend_height=True),
                        Condition(has_before_fragments)),
                    ConditionalContainer(
                        default_buffer_window,
                        Condition(lambda: get_app().layout.current_control !=
                                  search_buffer_control),
                    ),
                    ConditionalContainer(
                        Window(search_buffer_control),
                        Condition(lambda: get_app().layout.current_control ==
                                  search_buffer_control),
                    ),
                ]),
                [
                    # Completion menus.
                    Float(xcursor=True,
                          ycursor=True,
                          content=CompletionsMenu(
                              max_height=16,
                              scroll_offset=1,
                              extra_filter=has_focus(default_buffer)
                              & ~multi_column_complete_style)),
                    Float(xcursor=True,
                          ycursor=True,
                          content=MultiColumnCompletionsMenu(
                              show_meta=True,
                              extra_filter=has_focus(default_buffer)
                              & multi_column_complete_style)),
                    # The right prompt.
                    Float(right=0,
                          top=0,
                          hide_when_covering_content=True,
                          content=_RPrompt(lambda: self.rprompt)),
                ]),
            ConditionalContainer(ValidationToolbar(), filter=~is_done),
            ConditionalContainer(system_toolbar,
                                 dyncond('enable_system_prompt') & ~is_done),

            # In multiline mode, we use two toolbars for 'arg' and 'search'.
            ConditionalContainer(
                Window(FormattedTextControl(self._get_arg_text), height=1),
                dyncond('multiline') & has_arg),
            ConditionalContainer(search_toolbar,
                                 dyncond('multiline') & ~is_done),
            bottom_toolbar,
        ])

        # Default key bindings.
        auto_suggest_bindings = load_auto_suggest_bindings()
        open_in_editor_bindings = load_open_in_editor_bindings()
        prompt_bindings = self._create_prompt_bindings()

        # Create application
        application = Application(
            layout=Layout(layout, default_buffer_window),
            style=DynamicStyle(lambda: self.style),
            include_default_pygments_style=dyncond(
                'include_default_pygments_style'),
            clipboard=DynamicClipboard(lambda: self.clipboard),
            key_bindings=merge_key_bindings([
                merge_key_bindings([
                    auto_suggest_bindings,
                    ConditionalKeyBindings(
                        open_in_editor_bindings,
                        dyncond('enable_open_in_editor')
                        & has_focus(DEFAULT_BUFFER)), prompt_bindings
                ]),
                ConditionalKeyBindings(
                    system_toolbar.get_global_key_bindings(),
                    dyncond('enable_system_prompt')),
                DynamicKeyBindings(lambda: self.extra_key_bindings),
            ]),
            mouse_support=dyncond('mouse_support'),
            editing_mode=editing_mode,
            erase_when_done=erase_when_done,
            reverse_vi_search_direction=True,

            # I/O.
            input=self.input,
            output=self.output)

        # During render time, make sure that we focus the right search control
        # (if we are searching). - This could be useful if people make the
        # 'multiline' property dynamic.
        '''
        def on_render(app):
            multiline = _true(self.multiline)
            current_control = app.layout.current_control

            if multiline:
                if current_control == search_buffer_control:
                    app.layout.current_control = search_toolbar.control
                    app.invalidate()
            else:
                if current_control == search_toolbar.control:
                    app.layout.current_control = search_buffer_control
                    app.invalidate()

        app.on_render += on_render
        '''

        return application, default_buffer, default_buffer_control