From 3dcd083d8d0e49238813e0835a05ae87e6bdb937 Mon Sep 17 00:00:00 2001 From: WormHeamer Date: Mon, 29 Dec 2025 20:41:07 -0500 Subject: fix some logic errors in line navigation --- txt.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'txt.c') diff --git a/txt.c b/txt.c index 62017c0..d5ee28d 100644 --- a/txt.c +++ b/txt.c @@ -418,8 +418,9 @@ TxtLoc prev_newline(TxtLoc l) { } TxtLoc start_of_line(TxtLoc l) { + if (at_start(l)) return l; l = prev_newline(l); - if (!at_start(l) && txt_byte(l) == '\n') l = cnext(l); + if (txt_byte(l) == '\n') l = cnext(l); return l; } @@ -436,24 +437,25 @@ u32 get_col(TxtLoc l) { TxtLoc at_col(TxtLoc l, u32 col) { l = start_of_line(l); - while (col--) { - if (txt_chr_next(&l) == '\n') { - l = bprev(l); - break; - } - } + while (col-- && txt_byte(l) != '\n') l = cnext(l); return l; } +TxtLoc next_line_start(TxtLoc l) { + if (txt_byte(l) == '\n') return cnext(l); + return cnext(next_newline(l)); +} + +TxtLoc prev_line_start(TxtLoc l) { + return start_of_line(prev_newline(l)); +} + TxtLoc prev_line(TxtLoc l) { - TxtLoc start = start_of_line(l); - return at_col(cprev(start), get_col(l)); + return at_col(prev_line_start(l), get_col(l)); } TxtLoc next_line(TxtLoc l) { - TxtLoc end = end_of_line(l); - if (at_end(end)) return end; - return at_col(cnext(end), get_col(l)); + return at_col(next_line_start(l), get_col(l)); } /* reading chars */ -- cgit v1.2.3