From 171ace02cb9a57a6552c1534d7dbf5ab5f037754 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Wed, 29 Apr 2020 12:23:36 -0300 Subject: Extract seeking functions --- point.c | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) (limited to 'point.c') diff --git a/point.c b/point.c index 82f27d2..755b99f 100644 --- a/point.c +++ b/point.c @@ -20,11 +20,7 @@ bool same_point(struct point *a, struct point *b) { uint8_t element(struct point *point) { if (point->index == point->page->element_count) { - if (!point->page->next) { - return EOF; - } else { - return point->page->next->elements[0]; - } + return !point->page->next ? 0 : point->page->next->elements[0]; } else { return point->page->elements[index_to_offset(point)]; } @@ -50,6 +46,40 @@ void move_point_backward(struct point *point) { } } +uint64_t seek(struct point *point, uint8_t c, int limit) { + uint64_t travel_distance = 0; + while (element(point) && element(point) != c && travel_distance < limit) { + move_point_forward(point); + travel_distance++; + } + return travel_distance; +} + +uint64_t rseek(struct point *point, uint8_t c, int limit) { + uint64_t travel_distance = 0; + while (point->index != 0 && element(point) != c && travel_distance < limit) { + move_point_backward(point); + travel_distance++; + } + return travel_distance; +} + +void prev_line(struct point *point, int window_width) { + move_point_backward(point); + move_point_backward(point); + rseek(point, '\n', window_width); + if (element(point) == '\n') { + move_point_forward(point); + } +} + +void next_line(struct point *point, int window_width) { + seek(point, '\n', window_width); + if (element(point) == '\n') { + move_point_forward(point); + } +} + void align_gap(struct point *point) { while (point->page->gap_end < index_to_offset(point)) { move_gap_forward(point->page); -- cgit v1.2.3