def _single_mark( nvim: Nvim, mark: ExtMark, marks: Sequence[ExtMark], ns: int, win: Window, buf: Buffer, ) -> None: row, col = mark.begin nvim.options["undolevels"] = nvim.options["undolevels"] try: apply(nvim, buf=buf, instructions=_trans("", marks=(mark, ))) win_set_cursor(nvim, win=win, row=row, col=col) nvim.command("startinsert") except NvimError as e: msg = f""" bad mark location {mark} {e} """ log.warn("%s", dedent(msg)) else: nvim.command("startinsert") state(inserted_pos=(row, col)) msg = LANG("applied mark", marks_left=len(marks)) write(nvim, msg) finally: _del_marks(nvim, buf=buf, id=ns, marks=(mark, ))
def _new_window(nvim: Nvim, vertical: bool) -> None: nvim.command("vnew" if vertical else "new") win = cur_win(nvim) buf = create_buf(nvim, listed=False, scratch=True, wipe=True, nofile=True, noswap=True) win_set_buf(nvim, win=win, buf=buf)
def _norm_mv(nvim: Nvim, up: bool) -> None: win = cur_win(nvim) buf = win_get_buf(nvim, win=win) row, _ = win_get_cursor(nvim, win=win) lines = buf_line_count(nvim, buf=buf) if not writable(nvim, buf=buf): return else: if up: if row: nvim.command(f"move -2") else: if row < lines - 1: nvim.command(f"move +1")
def _toggle_qf(nvim: Nvim) -> None: tab = cur_tab(nvim) wins = tab_list_wins(nvim, tab=tab) closed = False for win in wins: buf = win_get_buf(nvim, win=win) ft = buf_filetype(nvim, buf=buf) if ft == "qf": win_close(nvim, win=win) closed = True if not closed: nvim.command("copen") win = cur_win(nvim) height = nvim.options["previewheight"] nvim.api.win_set_height(win, height)
def _toggle_preview(nvim: Nvim) -> None: tab = cur_tab(nvim) wins = tab_list_wins(nvim, tab=tab) closed = False for win in wins: is_preview: bool = win_get_option(nvim, win=win, key="previewwindow") if is_preview: win_close(nvim, win=win) closed = True if not closed: nvim.command("new") win = cur_win(nvim) win_set_option(nvim, win=win, key="previewwindow", val=True) height = nvim.options["previewheight"] nvim.api.win_set_height(win, height)
def _go_replace(nvim: Nvim, visual: VisualTypes) -> None: buf = cur_buf(nvim) if not writable(nvim, buf=buf): return else: linefeed = buf_linefeed(nvim, buf=buf) (r1, c1), (r2, c2) = operator_marks(nvim, buf=buf, visual_type=visual) lines = buf_get_lines(nvim, buf=buf, lo=r1, hi=r2 + 1) if len(lines) > 1: h, *_, t = lines else: h, *_ = t, *_ = lines begin = (r1, min(c1, max(0, len(encode(h)) - 1))) end = (r2, min(len(encode(t)), c2 + 1)) text: str = nvim.funcs.getreg() new_lines = text.split(linefeed) if new_lines: n = new_lines.pop() if n: new_lines.append(n) nvim.options["undolevels"] = nvim.options["undolevels"] buf_set_text(nvim, buf=buf, begin=begin, end=end, text=new_lines)
def _check_time(nvim: Nvim) -> None: check = lambda: nvim.command("silent! checktime") async def cont() -> None: async for _ in aticker(3, immediately=False): go(async_call(nvim, check)) go(cont())
def _linked_marks( nvim: Nvim, mark: ExtMark, linked: Sequence[ExtMark], ns: int, win: Window, buf: Buffer, ) -> bool: marks = tuple(chain((mark, ), linked)) place_holders = tuple( text for _, text in extmarks_text(nvim, buf=buf, marks=marks)) texts = dumps(place_holders, check_circular=False, ensure_ascii=False) resp = ask(nvim, question=LANG("expand marks", texts=texts), default="") if resp is not None: row, col = mark.begin nvim.options["undolevels"] = nvim.options["undolevels"] apply(nvim, buf=buf, instructions=_trans(resp, marks=marks)) _del_marks(nvim, buf=buf, id=ns, marks=marks) win_set_cursor(nvim, win=win, row=row, col=col) nvim.command("startinsert") state(inserted_pos=(row, col - 1)) return True else: return False
def _visual_mv(nvim: Nvim, up: bool) -> None: win = cur_win(nvim) buf = win_get_buf(nvim, win=win) (row1, col1), (row2, col2) = operator_marks(nvim, buf=buf, visual_type=None) lines = buf_line_count(nvim, buf=buf) if not writable(nvim, buf=buf): return else: if up: if row1 <= 0: nvim.command("norm! gv") else: nvim.command(f"{row1+1},{row2+1}move {row1-1}") set_visual_selection( nvim, win=win, mode="v", mark1=(row1 - 1, col1), mark2=(row2 - 1, col2), reverse=True, ) else: if row2 >= lines - 1: nvim.command("norm! gv") else: nvim.command(f"{row1+1},{row2+1}move {row2+2}") set_visual_selection( nvim, win=win, mode="v", mark1=(row1 + 1, col1), mark2=(row2 + 1, col2), )
def _op_rg(nvim: Nvim, visual: VisualTypes) -> None: text = _hl_selected(nvim, visual=visual) escaped = escape(text).replace(r"\ ", " ") cont = lambda: nvim.command(f"Rg {escaped}") go(nvim, aw=async_call(nvim, cont))
def _op_fzf(nvim: Nvim, visual: VisualTypes) -> None: text = _hl_selected(nvim, visual=visual) cont = lambda: nvim.command(f"BLines {text}") go(nvim, aw=async_call(nvim, cont))
def _op_rg(nvim: Nvim, visual: VisualTypes = None) -> None: text = _hl_selected(nvim, visual=visual) cont = lambda: nvim.command(f"Rg {text}") go(async_call(nvim, cont))
def _new_tab(nvim: Nvim) -> None: nvim.command("tabnew") buf = cur_buf(nvim) buf_set_var(nvim, buf=buf, key="buftype", val="nofile")
def _clear_qf(nvim: Nvim) -> None: nvim.funcs.setqflist(()) with suppress(NvimError): nvim.command("cclose")
def _clear_qf(nvim: Nvim) -> None: nvim.funcs.setqflist(()) nvim.command("cclose")
def _update_pumheight(nvim: Nvim) -> None: lines: int = nvim.options["lines"] nvim.options["pumheight"] = max(round(lines * 0.3), 15)