Example #1
0
def trailing_ws(nvim: Nvim) -> None:
    win = cur_win(nvim)
    buf = win_get_buf(nvim, win=win)
    if not writable(nvim, buf=buf):
        return
    else:
        _set_trimmed(nvim, win=win, buf=buf)
Example #2
0
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)
Example #3
0
def _comment_single(nvim: Nvim) -> None:
    win = cur_win(nvim)
    buf = win_get_buf(nvim, win=win)
    if not writable(nvim, buf=buf):
        return
    else:
        row, _ = win_get_cursor(nvim, win=win)
        lines = buf_get_lines(nvim, buf=buf, lo=row, hi=row + 1)
        lhs, rhs = buf_commentstr(nvim, buf=buf)
        new_lines = _toggle_comment(lhs, rhs, lines=lines)
        buf_set_lines(nvim, buf=buf, lo=row, hi=row + 1, lines=new_lines)
Example #4
0
def _sort_lines(nvim: Nvim, visual: VisualTypes) -> None:
    buf = cur_buf(nvim)
    if not writable(nvim, buf=buf):
        return
    else:
        (row1, _), (row2, _) = operator_marks(nvim,
                                              buf=buf,
                                              visual_type=visual)
        lines = buf_get_lines(nvim, buf=buf, lo=row1, hi=row2 + 1)
        new_lines = sorted(lines, key=strxfrm)
        buf_set_lines(nvim, buf=buf, lo=row1, hi=row2 + 1, lines=new_lines)
Example #5
0
def _comment(nvim: Nvim, visual: VisualTypes) -> None:
    buf = cur_buf(nvim)
    if not writable(nvim, buf=buf):
        return
    else:
        (row1, _), (row2, _) = operator_marks(nvim,
                                              buf=buf,
                                              visual_type=visual)
        lines = buf_get_lines(nvim, buf=buf, lo=row1, hi=row2 + 1)
        lhs, rhs = buf_commentstr(nvim, buf=buf)
        new_lines = _toggle_comment(lhs, rhs, lines=lines)
        buf_set_lines(nvim, buf=buf, lo=row1, hi=row2 + 1, lines=new_lines)
Example #6
0
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")
Example #7
0
def _go_replace_line(nvim: Nvim) -> None:
    win = cur_win(nvim)
    buf = win_get_buf(nvim, win=win)
    if not writable(nvim, buf=buf):
        return
    else:
        linefeed = buf_linefeed(nvim, buf=buf)
        row, _ = win_get_cursor(nvim, win=win)
        body: str = nvim.funcs.getreg()
        new_lines = body.split(linefeed)
        if new_lines:
            n = new_lines.pop()
            if n:
                new_lines.append(n)

        buf_set_lines(nvim, buf=buf, lo=row, hi=row + 1, lines=new_lines)
Example #8
0
def _toggle_case(nvim: Nvim) -> None:
    win = cur_win(nvim)
    row, col = win_get_cursor(nvim, win=win)
    buf = win_get_buf(nvim, win=win)
    if writable(nvim, buf=buf):
        line, *_ = buf_get_lines(nvim, buf=buf, lo=row, hi=row + 1)
        bline = encode(line)
        before, after = bline[:col], bline[col:]
        if after:
            cur, *post = after
            pt = decode(bytes((cur, )))
            swapped = _swap_case(pt)
            new = decode(before) + swapped + decode(bytes(post))
            pos = len(before) + len(encode(swapped))
            buf_set_lines(nvim, buf=buf, lo=row, hi=row + 1, lines=(new, ))
            win_set_cursor(nvim, win=win, row=row, col=pos)
Example #9
0
def _toggle_case(nvim: Nvim) -> None:
    win = cur_win(nvim)
    row, col = win_get_cursor(nvim, win=win)
    buf = win_get_buf(nvim, win=win)
    if not writable(nvim, buf=buf):
        return
    else:
        line, *_ = buf_get_lines(nvim, buf=buf, lo=row, hi=row + 1)
        bline = line.encode()
        before, after = bline[:col], bline[col:]
        cur, *post = after
        pt = bytes((cur,)).decode()
        swapped = _swap_case(pt)
        new = before.decode() + swapped + bytes(post).decode()
        pos = len(before) + len(swapped.encode())
        buf_set_lines(nvim, buf=buf, lo=row, hi=row + 1, lines=(new,))
        win_set_cursor(nvim, win=win, row=row, col=pos)
Example #10
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),
                )