summaryrefslogtreecommitdiff
path: root/editor.c
diff options
context:
space:
mode:
authorcflip <cflip@cflip.net>2023-01-12 10:37:09 -0700
committercflip <cflip@cflip.net>2023-01-12 10:37:09 -0700
commitab00e1e288743496e75c5e54c0e6abdecf439642 (patch)
treedb06dd60a6567524b44d7c7fe901142d745854e2 /editor.c
parent54d0e32bced43dc3d85b41e30bcb1fa430ae90dc (diff)
Move cursor movement into separate functions
Diffstat (limited to 'editor.c')
-rw-r--r--editor.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/editor.c b/editor.c
index d08a961..58f18c9 100644
--- a/editor.c
+++ b/editor.c
@@ -51,6 +51,49 @@ char* editor_prompt(struct editor_state* editor, char* prompt, void (*callback)(
return NULL;
}
+void editor_move_left(struct editor_state *editor)
+{
+ if (editor->cursor_x != 0) {
+ editor->cursor_x--;
+ } else if (editor->cursor_y > 0) {
+ editor->cursor_y--;
+ editor->cursor_x = editor->rows[editor->cursor_y].size;
+ }
+}
+
+void editor_move_right(struct editor_state *editor)
+{
+ struct editor_row* row = (editor->cursor_y >= editor->row_count) ? NULL : &editor->rows[editor->cursor_y];
+ if (row && editor->cursor_x < row->size) {
+ editor->cursor_x++;
+ } else if (row && editor->cursor_x == row->size) {
+ editor->cursor_y++;
+ editor->cursor_x = 0;
+ }
+}
+
+void editor_move_up(struct editor_state *editor)
+{
+ if (editor->cursor_y != 0)
+ editor->cursor_y--;
+
+ struct editor_row *row = (editor->cursor_y >= editor->row_count) ? NULL : &editor->rows[editor->cursor_y];
+ int row_length = row ? row->size : 0;
+ if (editor->cursor_x > row_length)
+ editor->cursor_x = row_length;
+}
+
+void editor_move_down(struct editor_state *editor)
+{
+ if (editor->cursor_y != editor->row_count - 1)
+ editor->cursor_y++;
+
+ struct editor_row *row = (editor->cursor_y >= editor->row_count) ? NULL : &editor->rows[editor->cursor_y];
+ int row_length = row ? row->size : 0;
+ if (editor->cursor_x > row_length)
+ editor->cursor_x = row_length;
+}
+
void editor_insert_char(struct editor_state* editor, int c)
{
if (editor->cursor_y == editor->row_count)