summaryrefslogtreecommitdiff
path: root/point.c
diff options
context:
space:
mode:
authorJuan Manuel Tomás <jtomas1815@gmail.com>2020-04-29 12:23:36 -0300
committerJuan Manuel Tomás <jtomas1815@gmail.com>2020-04-29 12:23:36 -0300
commit171ace02cb9a57a6552c1534d7dbf5ab5f037754 (patch)
treeffa5035e50c40f872d45ddaec6cf3b69f981179d /point.c
parentc6c21c2c1c811508f4cb467dcd5288af20870334 (diff)
downloadjet-171ace02cb9a57a6552c1534d7dbf5ab5f037754.tar.gz
jet-171ace02cb9a57a6552c1534d7dbf5ab5f037754.zip
Extract seeking functions
Diffstat (limited to 'point.c')
-rw-r--r--point.c40
1 files changed, 35 insertions, 5 deletions
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);