Insert new lines

This commit is contained in:
Timothy Warren 2019-08-20 12:02:40 -04:00
parent 94dc8ca331
commit 9248b9d560

36
kilo.c
View File

@ -303,11 +303,16 @@ void editorUpdateRow(erow *row)
row->rsize = idx;
}
void editorAppendRow(char *s, size_t len)
void editorInsertRow(int at, char *s, size_t len)
{
E.row = realloc(E.row, sizeof(erow) * (E.numrows + 1));
if (at < 0 || at > E.numrows)
{
return;
}
E.row = realloc(E.row, sizeof(erow) * (E.numrows + 1));
memmove(&E.row[at + 1], &E.row[at], sizeof(erow) * (E.numrows - at));
int at = E.numrows;
E.row[at].size = len;
E.row[at].chars = malloc(len + 1);
memcpy(E.row[at].chars, s, len);
@ -383,12 +388,31 @@ void editorInsertChar(int c)
{
if (E.cy == E.numrows)
{
editorAppendRow("", 0);
editorInsertRow(E.numrows, "", 0);
}
editorRowInsertChar(&E.row[E.cy], E.cx, c);
E.cx++;
}
void editorInsertNewline()
{
if (E.cx == 0)
{
editorInsertRow(E.cy, "", 0);
}
else
{
erow *row = &E.row[E.cy];
editorInsertRow(E.cy + 1, &row->chars[E.cx], row->size - E.cx);
row = &E.row[E.cy];
row->size = E.cx;
row->chars[row->size] = '\0';
editorUpdateRow(row);
}
E.cy++;
E.cx = 0;
}
void editorDelChar()
{
if (E.cy == E.numrows)
@ -463,7 +487,7 @@ void editorOpen(char *filename)
{
linelen--;
}
editorAppendRow(line, linelen);
editorInsertRow(E.numrows, line, linelen);
}
free(line);
@ -766,7 +790,7 @@ void editorProcessKeypress()
switch (c)
{
case '\r':
/* TODO */
editorInsertNewline();
break;
case CTRL_KEY('q'):