def _word(nvim: Nvim, is_inside: bool) -> None: win = cur_win(nvim) buf = win_get_buf(nvim, win=win) row, c = win_get_cursor(nvim, win=win) lines = buf_get_lines(nvim, buf=buf, lo=row, hi=row + 1) line = next(iter(lines)) bline = line.encode() # position under cursor c = min(len(bline) - 1, c) col = len(bline[:c].decode()) + 1 lhs, rhs = line[:col], line[col:] # undo col + 1 offset = len(next(reversed(lhs), "").encode()) ctx = gen_split(lhs, rhs, unifying_chars=UNIFIYING_CHARS) if not (ctx.word_lhs + ctx.word_rhs): words_lhs, words_rhs = ctx.syms_lhs, ctx.syms_rhs else: words_lhs, words_rhs = ctx.word_lhs, ctx.word_rhs c_lhs = c + offset - len(words_lhs.encode()) c_rhs = c + offset + len(words_rhs.encode()) - 1 if is_inside: mark1 = (row, c_lhs) mark2 = (row, c_rhs) else: mark1 = (row, c_lhs - 1) mark2 = (row, c_rhs + 1) set_visual_selection(nvim, win=win, mode="v", mark1=mark1, mark2=mark2)
def _word(nvim: Nvim, is_inside: bool) -> None: win = cur_win(nvim) buf = win_get_buf(nvim, win=win) row, col = win_get_cursor(nvim, win=win) line, *_ = buf_get_lines(nvim, buf=buf, lo=row, hi=row + 1) bline = encode(line) lhs, rhs = decode(bline[:col]), decode(bline[col:]) ctx = gen_split(lhs, rhs, unifying_chars=UNIFIYING_CHARS) if not (ctx.word_lhs + ctx.word_rhs): words_lhs, words_rhs = ctx.syms_lhs, ctx.syms_rhs else: words_lhs, words_rhs = ctx.word_lhs, ctx.word_rhs c_lhs = max(col - len(encode(words_lhs)), 0) c_rhs = max(col + len(encode(words_rhs)) - 1, 0) if is_inside: mark1 = (row, c_lhs) mark2 = (row, c_rhs) else: mark1 = (row, max(0, c_lhs - 1)) mark2 = (row, min(len(bline), c_rhs + 1)) set_visual_selection(nvim, win=win, mode="v", mark1=mark1, mark2=mark2)
def _entire(nvim: Nvim) -> None: win = cur_win(nvim) buf = win_get_buf(nvim, win=win) count = buf_line_count(nvim, buf=buf) last_line, *_ = buf_get_lines(nvim, buf=buf, lo=-2, hi=-1) mark1 = (0, 0) mark2 = (count - 1, len(encode(last_line))) set_visual_selection(nvim, win=win, mode="V", mark1=mark1, mark2=mark2)
def _line(nvim: Nvim, is_inside: bool) -> None: win = cur_win(nvim) buf = win_get_buf(nvim, win=win) row, _ = win_get_cursor(nvim, win=win) lines = buf_get_lines(nvim, buf=buf, lo=row, hi=row + 1) line = next(iter(lines)) lhs, rhs = (_p_inside if is_inside else _p_around)(line) set_visual_selection(nvim, win=win, mode="v", mark1=(row, lhs), mark2=(row, rhs))
def _indent(nvim: Nvim) -> None: win = cur_win(nvim) buf = win_get_buf(nvim, win) row, _ = win_get_cursor(nvim, win) tabsize: int = buf_get_option(nvim, buf=buf, key="tabstop") lines = buf_get_lines(nvim, buf=buf, lo=0, hi=-1) before, curr, after = lines[:row], lines[row], lines[row + 1:] init_lv = p_indent(curr, tabsize=tabsize) top = row - _p_inside(init_lv, tabsize=tabsize, lines=reversed(before)) btm = row + _p_inside(init_lv, tabsize=tabsize, lines=after) set_visual_selection(nvim, win=win, mode="V", mark1=(top, 0), mark2=(btm, 0))
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), )
top = row - _p_inside(init_lv, tabsize=tabsize, lines=reversed(before)) btm = row + _p_inside(init_lv, tabsize=tabsize, lines=after) lines = deque(buf_get_lines(nvim, buf=buf, lo=top, hi=btm + 1)) while lines: if line := lines.popleft(): lines.appendleft(line) break else: top += 1 while lines: if line := lines.pop(): lines.append(line) break else: btm -= 1 if lines: *_, btm_line = lines mark1, mark2 = (top, 0), (btm, len(encode(btm_line))) else: mark1, mark2 = (row, 0), (row, len(encode(curr))) set_visual_selection(nvim, win=win, mode="V", mark1=mark1, mark2=mark2) keymap.o("ii") << f"<cmd>lua {NAMESPACE}.{_indent.name}()<cr>" keymap.v("ii") << rf"<c-\><c-n><cmd>lua {NAMESPACE}.{_indent.name}()<cr>"