diff --git a/.config/kak/scripts/nav.py b/.config/kak/scripts/nav.py index cf3cb834..d76dadb7 100755 --- a/.config/kak/scripts/nav.py +++ b/.config/kak/scripts/nav.py @@ -10,9 +10,12 @@ # Basically, look at the target line, and figure out the logical column # number that corresponds to the visual column number we see right now. +# ISSUE: does not work if the line has indentation + # Also TODO: once this is ironed out, re-write it in Rust for le speed import sys +import re session_id = sys.argv[1] line_n = int(sys.argv[2]) @@ -30,8 +33,16 @@ with open(path, "r") as f: for n, l in enumerate(f, start=1): if n == line_n: line = l +match = re.match(r"^(\s*)", line) +if match: + indent = match.group(1) + indent_len = len(indent) +else: + indent_len = 0 -chunks = [line[i : i + wrap_width] for i in range(0, len(line), wrap_width)] +content = line[indent_len:] + +chunks = [content[i : i + wrap_width] for i in range(0, len(content), wrap_width)] visual_row = col // wrap_width visual_col = col % wrap_width @@ -39,7 +50,7 @@ new_visual_row = visual_row + (-1 if direction == "up" else 1) if 0 <= new_visual_row < len(chunks): target_chunk = chunks[new_visual_row] new_col = min(visual_col, len(target_chunk) - 1) - new_abs_col = new_visual_row * wrap_width + new_col + new_abs_col = indent_len + (new_visual_row * wrap_width + new_col) diff = abs(new_abs_col - col) key = "h" if direction == "up" else "l" print(f"{diff}{key}")