Backspace to previous line

This commit is contained in:
Timothy Warren 2019-08-20 11:55:11 -04:00
parent e14432eae4
commit 94dc8ca331

40
kilo.c
View File

@ -321,6 +321,24 @@ void editorAppendRow(char *s, size_t len)
E.dirty++; E.dirty++;
} }
void editorFreeRow(erow *row)
{
free(row->render);
free(row->chars);
}
void editorDelRow(int at)
{
if (at < 0 || at >= E.numrows)
{
return;
}
editorFreeRow(&E.row[at]);
memmove(&E.row[at], &E.row[at + 1], sizeof(erow) * (E.numrows - at - 1));
E.numrows--;
E.dirty++;
}
void editorRowInsertChar(erow *row, int at, int c) void editorRowInsertChar(erow *row, int at, int c)
{ {
if (at < 0 || at > row->size) if (at < 0 || at > row->size)
@ -336,6 +354,16 @@ void editorRowInsertChar(erow *row, int at, int c)
E.dirty++; E.dirty++;
} }
void editorRowAppendString(erow *row, char *s, size_t len)
{
row->chars = realloc(row->chars, row->size + len + 1);
memcpy(&row->chars[row->size], s, len);
row->size += len;
row->chars[row->size] = '\0';
editorUpdateRow(row);
E.dirty++;
}
void editorRowDelChar(erow *row, int at) void editorRowDelChar(erow *row, int at)
{ {
if (at < 0 || at >= row->size) if (at < 0 || at >= row->size)
@ -368,12 +396,24 @@ void editorDelChar()
return; return;
} }
if (E.cx == 0 && E.cy == 0)
{
return;
}
erow *row = &E.row[E.cy]; erow *row = &E.row[E.cy];
if (E.cx > 0) if (E.cx > 0)
{ {
editorRowDelChar(row, E.cx - 1); editorRowDelChar(row, E.cx - 1);
E.cx--; E.cx--;
} }
else
{
E.cx = E.row[E.cy - 1].size;
editorRowAppendString(&E.row[E.cy - 1], row->chars, row->size);
editorDelRow(E.cy);
E.cy--;
}
} }
/*** file i/o ***/ /*** file i/o ***/