tImplement horizontal 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 b6a1dc1daa16f2ce59d88fba5bf3bfdb36e61136 (DIR) parent ee8ccbb69c46ce38a5b115821e1dc6816e1dd0dc (HTM) Author: Anders Damsgaard <anders@adamsgaard.dk> Date: Tue, 6 Aug 2019 13:14:03 +0200 Implement horizontal scrolling Diffstat: M byote.h | 4 ++-- M input.c | 3 +-- M output.c | 30 +++++++++++++++++++----------- M terminal.c | 3 ++- 4 files changed, 24 insertions(+), 16 deletions(-) --- (DIR) diff --git a/byote.h b/byote.h t@@ -14,10 +14,10 @@ typedef struct eRow { struct editor_config { int cursor_x, cursor_y; - int screen_rows, screen_cols; + int screen_rows, screen_columns; int num_rows; eRow *row; - int row_offset; + int row_offset, column_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@@ -22,8 +22,7 @@ editor_move_cursor(char key) E.cursor_y--; break; case 'l': - if (E.cursor_x < E.screen_cols - 1) - E.cursor_x++; + E.cursor_x++; break; } } (DIR) diff --git a/output.c b/output.c t@@ -51,15 +51,15 @@ draw_status(struct abuf *ab) "%s editor -- version %s", PROGNAME, VERSION); - if (left_status_len > E.screen_cols) - left_status_len = E.screen_cols; + if (left_status_len > E.screen_columns) + left_status_len = E.screen_columns; - padding = E.screen_cols - left_status_len - right_status_len; + padding = E.screen_columns - left_status_len - right_status_len; if (padding < 0) { - if (left_status_len < E.screen_cols) + if (left_status_len < E.screen_columns) ab_append(ab, left_status, left_status_len); else - ab_append(ab, left_status, E.screen_cols); + ab_append(ab, left_status, E.screen_columns); } else { ab_append(ab, left_status, left_status_len); while (padding--) t@@ -76,6 +76,11 @@ editor_scroll() 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 + E.status_height + 1; + + if (E.cursor_x < E.column_offset) + E.column_offset = E.cursor_x; + else if (E.cursor_x >= E.column_offset + E.screen_columns) + E.column_offset = E.cursor_x - E.screen_columns + 1; } /* draw editor screen. t@@ -89,10 +94,12 @@ editor_draw_rows(struct abuf *ab) 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[file_row].chars, len); + len = E.row[file_row].size - E.column_offset; + if (len < 0) + len = 0; + if (len > E.screen_columns) + len = E.screen_columns; + ab_append(ab, &E.row[file_row].chars[E.column_offset], len); } else { ab_append(ab, "~", 1); } t@@ -121,8 +128,9 @@ editor_refresh_screen() editor_draw_rows(&ab); char buf[32]; - snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cursor_y - E.row_offset)+1, - E.cursor_x+1); + snprintf(buf, sizeof(buf), "\x1b[%d;%dH", + (E.cursor_y - E.row_offset)+1, + (E.cursor_x - E.column_offset)+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@@ -114,6 +114,7 @@ init_editor() { E.num_rows = 0; E.row = NULL; E.row_offset = 0; - if (get_window_size(&E.screen_rows, &E.screen_cols) == -1) + E.column_offset = 0; + if (get_window_size(&E.screen_rows, &E.screen_columns) == -1) die("get_window_size"); }