Skip to content

Commit

Permalink
Implement deletion at the begining of the line
Browse files Browse the repository at this point in the history
Fixme: deletion at the begining of the line containing text in previous line
causes a memory fault to trigger
  • Loading branch information
Arsenic-ATG committed Sep 6, 2021
1 parent 2736ee5 commit e373967
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions src/editor.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,34 @@ void editor_row_delete_char(e_row *row, int at)
E.modified = 1;
}

void editor_row_append_string(e_row *row, char *str, size_t length)
{
row->text = realloc(row->text, row->size + length + 1);
memcpy(&row->text[row->size], str, length);
row->size += length;
row->text[row->size] = '\0';
editor_update_row(row);
E.modified = 1;
}

void editor_delete_row(int at)
{
if (at < 0 || at >= E.num_rows)
return;

// free row
free(E.row->text);
free(E.row->renderer);
memmove(&E.row[at], &E.row[at + 1], sizeof(e_row) * (E.num_rows - at - 1));
E.num_rows--;
E.modified = 1;
}

/************************ Editor operations ********************/

void editor_insert_char(char c)
{
// The the cursor is at the end of line
// The the cursor is at the end of file
if(E.cursor_y == E.num_rows)
editor_append_row("",0);

Expand All @@ -295,15 +318,26 @@ void editor_insert_char(char c)

void editor_delete_char()
{
// The the cursor is at the beging of line
if(E.cursor_y == E.num_rows)
return;

if(E.cursor_y == 0 && E.cursor_x == 0)
return;

if(E.cursor_x > 0)
{
editor_row_delete_char(&E.row[E.cursor_y], E.cursor_x - 1);
E.cursor_x--;
}
else
{
E.cursor_x = E.row[E.cursor_y - 1].size;
editor_row_append_string(&E.row[E.cursor_y - 1],
E.row[E.cursor_y].text,
E.row[E.cursor_y].size);
editor_delete_row(E.cursor_y);
E.cursor_y--;
}
}

/************************ file i/o ********************/
Expand Down

0 comments on commit e373967

Please sign in to comment.