diff options
author | Juan Manuel Tomás <jtomas1815@gmail.com> | 2020-04-29 12:23:36 -0300 |
---|---|---|
committer | Juan Manuel Tomás <jtomas1815@gmail.com> | 2020-04-29 12:23:36 -0300 |
commit | 171ace02cb9a57a6552c1534d7dbf5ab5f037754 (patch) | |
tree | ffa5035e50c40f872d45ddaec6cf3b69f981179d | |
parent | c6c21c2c1c811508f4cb467dcd5288af20870334 (diff) | |
download | jet-171ace02cb9a57a6552c1534d7dbf5ab5f037754.tar.gz jet-171ace02cb9a57a6552c1534d7dbf5ab5f037754.zip |
Extract seeking functions
-rw-r--r-- | jet.c | 44 | ||||
-rw-r--r-- | point.c | 40 |
2 files changed, 50 insertions, 34 deletions
@@ -8,26 +8,6 @@ #include "page.c" #include "point.c" -void scroll_window_up(struct point *start, int window_width) { - int travel_distance = 0; - move_point_backward(start); - move_point_backward(start); - while (start->index != 0 && element(start) != '\n' && travel_distance < window_width) { - move_point_backward(start); - travel_distance++; - } - if (element(start) == '\n') move_point_forward(start); -} - -void scroll_window_down(struct point *start, int window_width) { - int travel_distance = 0; - while (element(start) != EOF && element(start) != '\n' && travel_distance < window_width) { - move_point_forward(start); - travel_distance++; - } - if (element(start) == '\n') move_point_forward(start); -} - int main(int argc, char *argv[]) { initscr(); cbreak(); @@ -35,9 +15,9 @@ int main(int argc, char *argv[]) { intrflush(stdscr, FALSE); keypad(stdscr, TRUE); - struct page *page = new_page(); - struct point current_line = {page, 0}; - struct point cursor = current_line; + struct point buffer_start = {new_page()}; + struct point window_start = buffer_start; + struct point cursor = window_start; if (argc > 1) { FILE *f = fopen(argv[1], "r"); @@ -45,7 +25,7 @@ int main(int argc, char *argv[]) { while ((c = fgetc(f)) != EOF) { insert_at_point(&cursor, c); } - cursor = current_line; + cursor = window_start; fclose(f); } @@ -56,11 +36,13 @@ int main(int argc, char *argv[]) { clear(); int x = -1, y = -1; - for (struct point i = current_line; element(&i) != EOF && getcury(stdscr) < window_height - 1; move_point_forward(&i)) { - if (same_point(&i, &cursor)) { + struct point window_end = window_start; + while (element(&window_end) && getcury(stdscr) < window_height - 1) { + if (same_point(&window_end, &cursor)) { getyx(stdscr, y, x); } - addch(element(&i)); + addch(element(&window_end)); + move_point_forward(&window_end); } if (x > -1 && y > -1) { move(y, x); @@ -70,10 +52,14 @@ int main(int argc, char *argv[]) { switch (input) { case KEY_UP: - scroll_window_up(¤t_line, window_width); + prev_line(&window_start, window_width); + prev_line(&cursor, window_width); break; case KEY_DOWN: - scroll_window_down(¤t_line, window_width); + if (element(&window_end)) { + next_line(&window_start, window_width); + } + next_line(&cursor, window_width); break; case KEY_LEFT: move_point_backward(&cursor); @@ -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); |