Esempio n. 1
0
def new_window(
    nvim: Nvim,
    *,
    last_used: Mapping[int, None],
    win_local: Mapping[str, Union[bool, str]],
    open_left: bool,
    width: Optional[int],
) -> Window:
    split_r = nvim.options["splitright"]

    wins = tuple(
        find_windows_in_tab(nvim, last_used=last_used, no_secondary=False))
    focus_win = wins[0] if open_left else wins[-1]
    direction = False if open_left else True

    nvim.options["splitright"] = direction
    set_cur_win(nvim, win=focus_win)
    nvim.command(f"{width}vnew" if width else "vnew")
    nvim.options["splitright"] = split_r

    win = cur_win(nvim)
    buf = win_get_buf(nvim, win)
    for key, val in win_local.items():
        win_set_option(nvim, win=win, key=key, val=val)
    buf_set_option(nvim, buf=buf, key="bufhidden", val="wipe")
    return win
Esempio n. 2
0
def kill_buffers(
    nvim: Nvim,
    last_used: Mapping[int, None],
    paths: AbstractSet[PurePath],
    reopen: Mapping[PurePath, PurePath],
) -> None:
    active = ({
        win_get_buf(nvim, win=win): win
        for win in find_non_fm_windows_in_tab(
            nvim,
            last_used=last_used,
        )
    } if reopen else {})

    for buf in list_bufs(nvim, listed=True):
        name = PurePath(buf_name(nvim, buf=buf))
        buf_paths = ancestors(name) | {name}

        if not buf_paths.isdisjoint(paths):
            win = active.get(buf)
            new_path = reopen.get(name)
            if reopen and win and new_path:
                p = Path(name)
                p.touch()
                with hold_win_pos(nvim):
                    set_cur_win(nvim, win=win)
                    escaped = nvim.funcs.fnameescape(normcase(new_path))
                    nvim.command(f"edit! {escaped}")
                    p.unlink(missing_ok=True)
            buf_close(nvim, buf=buf)
Esempio n. 3
0
def _open_fm_window(nvim: Nvim, settings: Settings, opts: _Args,
                    width: int) -> None:
    cwin = cur_win(nvim)
    win = next(find_fm_windows_in_tab(nvim), None)
    if win:
        if opts.toggle:
            wins = list_wins(nvim)
            if len(wins) > 1:
                win_close(nvim, win=win)
        else:
            set_cur_win(nvim, win=win)
    else:
        buf = next(find_fm_buffers(nvim), None)
        if buf is None:
            buf = new_fm_buffer(nvim, settings=settings)

        win = new_window(
            nvim,
            win_local=settings.win_actual_opts,
            open_left=settings.open_left,
            width=width,
        )
        for key, val in settings.win_local_opts.items():
            win_set_option(nvim, win=win, key=key, val=val)
        win_set_buf(nvim, win=win, buf=buf)

        _ensure_side_window(nvim, window=win, settings=settings, width=width)
        if not opts.focus:
            set_cur_win(nvim, win=cwin)
Esempio n. 4
0
def _show_file(nvim: Nvim, *, state: State, settings: Settings,
               click_type: ClickType) -> None:
    if click_type is ClickType.tertiary:
        nvim.api.command("tabnew")
        win = cur_win(nvim)
        for key, val in settings.win_actual_opts.items():
            win_set_option(nvim, win=win, key=key, val=val)

    path = state.current
    if path:
        hold = click_type is ClickType.secondary
        mgr = hold_win_pos(nvim) if hold else nullcontext()
        with mgr:
            non_fm_windows = tuple(
                find_non_fm_windows_in_tab(nvim, last_used=state.window_order))
            buf = next(find_buffers_with_file(nvim, file=path), None)
            win = next(
                chain(
                    find_window_with_file_in_tab(
                        nvim, last_used=state.window_order, file=path),
                    non_fm_windows,
                ),
                None,
            ) or new_window(
                nvim,
                last_used=state.window_order,
                win_local=settings.win_actual_opts,
                open_left=not settings.open_left,
                width=None if len(non_fm_windows) else
                nvim.options["columns"] - state.width - 1,
            )

            set_cur_win(nvim, win=win)
            non_fm_count = len(non_fm_windows)

            if click_type is ClickType.v_split and non_fm_count:
                nvim.api.command("vnew")
                temp_buf = cur_buf(nvim)
                buf_set_option(nvim, buf=temp_buf, key="bufhidden", val="wipe")
            elif click_type is ClickType.h_split and non_fm_count:
                nvim.api.command("new")
                temp_buf = cur_buf(nvim)
                buf_set_option(nvim, buf=temp_buf, key="bufhidden", val="wipe")

            win = cur_win(nvim)

            if buf is None:
                escaped = nvim.funcs.fnameescape(normcase(path))
                nvim.command(f"edit! {escaped}")
            else:
                win_set_buf(nvim, win=win, buf=buf)

            resize_fm_windows(nvim,
                              last_used=state.window_order,
                              width=state.width)
            nvim.api.command("filetype detect")
Esempio n. 5
0
def _show_file(
    nvim: Nvim, *, state: State, settings: Settings, click_type: ClickType
) -> None:
    if click_type is ClickType.tertiary:
        nvim.api.command("tabnew")

    path = state.current
    if path:
        hold = click_type is ClickType.secondary
        mgr = hold_win_pos(nvim) if hold else nullcontext()
        with mgr:
            non_fm_windows = tuple(find_non_fm_windows_in_tab(nvim))
            buf = next(find_buffers_with_file(nvim, file=path), None)
            win = next(
                chain(
                    find_window_with_file_in_tab(nvim, file=path),
                    (win for win in non_fm_windows if _win_nochange(nvim, win=win)),
                ),
                None,
            ) or new_window(
                nvim,
                open_left=not settings.open_left,
                width=None
                if len(non_fm_windows)
                else nvim.options["columns"] - state.width - 1,
            )

            set_cur_win(nvim, win=win)
            non_fm_count = len(non_fm_windows)

            if click_type is ClickType.v_split and non_fm_count:
                nvim.api.command("vnew")
                temp_buf = cur_buf(nvim)
                buf_set_option(nvim, buf=temp_buf, key="bufhidden", val="wipe")
            elif click_type is ClickType.h_split and non_fm_count:
                nvim.api.command("new")
                temp_buf = cur_buf(nvim)
                buf_set_option(nvim, buf=temp_buf, key="bufhidden", val="wipe")

            win = cur_win(nvim)

            if buf is None:
                nvim.command(f"edit {path}")
            else:
                win_set_buf(nvim, win=win, buf=buf)

            resize_fm_windows(nvim, state.width)
            nvim.api.command("filetype detect")
Esempio n. 6
0
def new_window(nvim: Nvim, *, open_left: bool, width: Optional[int]) -> Window:
    split_r = nvim.options["splitright"]

    wins = tuple(find_windows_in_tab(nvim, no_secondary=False))
    focus_win = wins[0] if open_left else wins[-1]
    direction = False if open_left else True

    nvim.options["splitright"] = direction
    set_cur_win(nvim, win=focus_win)
    nvim.command(f"{width}vnew" if width else "vnew")
    nvim.options["splitright"] = split_r

    win = cur_win(nvim)
    buf = win_get_buf(nvim, win)
    buf_set_option(nvim, buf=buf, key="bufhidden", val="wipe")
    return win
Esempio n. 7
0
def _toggle_fm_window(
    nvim: Nvim, state: State, settings: Settings, opts: _Args
) -> None:
    cwin = cur_win(nvim)
    win = next(find_fm_windows_in_tab(nvim), None)
    if win:
        wins = list_wins(nvim)
        if len(wins) <= 1:
            pass
        else:
            win_close(nvim, win=win)
    else:
        buf = next(find_fm_buffers(nvim), None)
        if buf is None:
            buf = new_fm_buffer(nvim, settings=settings)

        win = new_window(nvim, open_left=settings.open_left, width=state.width)
        for key, val in settings.win_local_opts.items():
            win_set_option(nvim, win=win, key=key, val=val)
        win_set_buf(nvim, win=win, buf=buf)

        _ensure_side_window(nvim, window=win, state=state, settings=settings)
        if not opts.focus:
            set_cur_win(nvim, win=cwin)