Skip to content

Commit

Permalink
Implement "ENTER" key to insert newline.
Browse files Browse the repository at this point in the history
  • Loading branch information
Arsenic-ATG committed Sep 7, 2021
1 parent dc03939 commit 114e30f
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/editor.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,14 @@ void editor_update_row(e_row *row)
row->r_size = idx;
}

void editor_append_row(char *s, size_t len)
void editor_insert_row(int at, char *s, size_t len)
{
if(at < 0 || at > E.num_rows)
return;

E.row = realloc(E.row, sizeof(e_row) * (E.num_rows + 1));
memmove(&E.row[at + 1], &E.row[at], sizeof(e_row) * (E.num_rows - at));

int at = E.num_rows;
E.row[at].size = len;
E.row[at].text = malloc(len + 1);
memcpy(E.row[at].text, s, len);
Expand All @@ -259,6 +262,12 @@ void editor_append_row(char *s, size_t len)
E.modified = 1;
}

void editor_append_row(char *s, size_t len)
{
int at = E.num_rows;
editor_insert_row(at, s, len);
}

void editor_row_insert_char(e_row *row, int at, int c)
{
if (at < 0 || at > row->size)
Expand Down Expand Up @@ -339,6 +348,27 @@ void editor_delete_char()
}
}

void editor_insert_newline()
{
if(E.cursor_x == 0)
editor_insert_row(E.cursor_y, "", 0);
else
{
e_row *row = &E.row[E.cursor_y];
editor_insert_row(E.cursor_y + 1, &row->text[E.cursor_x],
row->size - E.cursor_x);

// reassigning pointer as editor_insert_row() reallocates E.row
row = &E.row[E.cursor_y];
row->size = E.cursor_x;
row->text[row->size] = '\0';
editor_update_row(row);
}

E.cursor_y++;
E.cursor_x = 0;
}

/************************ file i/o ********************/

void editor_open(const char* file_name)
Expand Down Expand Up @@ -707,7 +737,9 @@ void editor_process_keypress()
editor_navigate_cursor(c);
break;

// Newline
case '\r':
editor_insert_newline();
break;

// Deletion keys
Expand Down

0 comments on commit 114e30f

Please sign in to comment.