tAdd vertical scrolling - ve - a minimal text editor (work in progress)
(HTM) git clone git://src.adamsgaard.dk/ve
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 65a91ff9999be9f55f6f57b1ca91afc65f8bdbf6
(DIR) parent fd944c194e43e14db7bf4fdaf5c5fbc726683c5e
(HTM) Author: Anders Damsgaard <anders@adamsgaard.dk>
Date: Tue, 6 Aug 2019 12:29:55 +0200
Add vertical scrolling
Diffstat:
M byote.h | 1 +
M input.c | 2 +-
M output.c | 32 ++++++++++++++++++++-----------
M terminal.c | 1 +
4 files changed, 24 insertions(+), 12 deletions(-)
---
(DIR) diff --git a/byote.h b/byote.h
t@@ -17,6 +17,7 @@ struct editor_config {
int screen_rows, screen_cols;
int num_rows;
eRow *row;
+ int row_offset;
struct termios orig_termios;
int status_height;
int mode; /* 0: normal, 1: insert, 2: visual */
(DIR) diff --git a/input.c b/input.c
t@@ -14,7 +14,7 @@ editor_move_cursor(char key)
E.cursor_x--;
break;
case 'j':
- if (E.cursor_y < E.screen_rows - 1 - E.status_height)
+ if (E.cursor_y < E.num_rows - E.status_height)
E.cursor_y++;
break;
case 'k':
(DIR) diff --git a/output.c b/output.c
t@@ -68,25 +68,32 @@ draw_status(struct abuf *ab)
}
}
+void
+editor_scroll()
+{
+ if (E.cursor_y < E.row_offset)
+ E.row_offset = E.cursor_y;
+ else if (E.cursor_y >= E.row_offset + E.screen_rows - E.status_height)
+ E.row_offset = E.cursor_y - E.screen_rows + 1;
+}
+
/* draw editor screen.
* show tilde characters after EOF, and show status on last line */
void
editor_draw_rows(struct abuf *ab)
{
- int y, len;
+ int y, len, file_row;
for (y = 0; y < E.screen_rows; ++y) {
-
- if (y < E.num_rows) {
- len = E.row[y].size;
+ file_row = y + E.row_offset;
+ if (y == E.screen_rows-1) {
+ draw_status(ab);
+ } else if (file_row < E.num_rows) {
+ len = E.row[file_row].size;
if (len > E.screen_cols)
len = E.screen_cols;
- ab_append(ab, E.row[y].chars, len);
-
+ ab_append(ab, E.row[file_row].chars, len);
} else {
- if (y == E.screen_rows-1)
- draw_status(ab);
- else
- ab_append(ab, "~", 1);
+ ab_append(ab, "~", 1);
}
ab_append(ab, "\x1b[K", 3); /* erase to end of line */
t@@ -104,6 +111,8 @@ editor_draw_rows(struct abuf *ab)
void
editor_refresh_screen()
{
+ editor_scroll();
+
struct abuf ab = ABUF_INIT;
ab_append(&ab, "\x1b[?25l", 6); /* hide cursor */
ab_append(&ab, "\x1b[H", 3); /* cursor to home */
t@@ -111,7 +120,8 @@ editor_refresh_screen()
editor_draw_rows(&ab);
char buf[32];
- snprintf(buf, sizeof(buf), "\x1b[%d;%dH", E.cursor_y+1, E.cursor_x+1);
+ snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cursor_y - E.row_offset)+1,
+ E.cursor_x+1);
ab_append(&ab, buf, strlen(buf));
ab_append(&ab, "\x1b[?25h", 6); /* show cursor */
(DIR) diff --git a/terminal.c b/terminal.c
t@@ -113,6 +113,7 @@ init_editor() {
E.mode = 0;
E.num_rows = 0;
E.row = NULL;
+ E.row_offset = 0;
if (get_window_size(&E.screen_rows, &E.screen_cols) == -1)
die("get_window_size");
}