summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWormHeamer2025-12-29 20:41:07 -0500
committerWormHeamer2025-12-29 20:41:07 -0500
commit3dcd083d8d0e49238813e0835a05ae87e6bdb937 (patch)
treef32c4e2abd5ead7d3c9c7e943adc94410a80f616
parent9f2ce437ab6657152effe9f5ed64c8f9daaa2e73 (diff)
fix some logic errors in line navigation
-rw-r--r--txt.c26
1 files changed, 14 insertions, 12 deletions
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 */