diff options
Diffstat (limited to 'src/panes.zig')
| -rw-r--r-- | src/panes.zig | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/src/panes.zig b/src/panes.zig index 20618ccc..51710d08 100644 --- a/src/panes.zig +++ b/src/panes.zig @@ -619,14 +619,18 @@ pub const Pane = struct { const line = modal.lineSlice(f.content, @intCast(@max(0, pane.body.cur_row))); const raw_cur: usize = @intCast(@max(0, pane.body.cur_col)); const raw_scroll: usize = @intCast(@max(0, pane.hscroll)); - const cur = @as(i32, @intCast(File.rawDisplayCol(line, raw_cur))); - const visual_scroll = @as(i32, @intCast(File.rawDisplayCol(line, raw_scroll))); - var target = visual_scroll; - if (cur < visual_scroll + hmargin) - target = @max(0, cur - hmargin) - else if (cur > visual_scroll + w - 1 - hmargin) - target = cur - (w - 1 - hmargin); - if (target != visual_scroll) pane.hscroll = @intCast(File.rawAtDisplay(line, @intCast(target))); + // Measured from the view's left edge to the caret, never from the + // line's start: a 500 KB line was walked from byte 0 at every key + // (widths add, so the span alone is exact). + const apart = File.lineDisplayOffset(line, raw_scroll, raw_cur); + const back: ?i32 = if (apart < hmargin) hmargin else if (apart > w - 1 - hmargin) w - 1 - hmargin else null; + if (back) |cols| { + // The byte `cols` columns before the caret, looked for from a + // few bytes a column back (a grapheme is at most 4 a column). + const from = raw_cur -| @as(usize, @intCast(cols)) * 4; + const ahead = File.lineDisplayOffset(line, from, raw_cur) - cols; + pane.hscroll = @intCast(if (ahead <= 0) from else File.byteAtDisplayFrom(line, from, @intCast(ahead))); + } } } |
