Add the remaining editor features

This commit is contained in:
Aaron Fischer 2018-10-08 14:13:00 +02:00
parent 01615ba5ef
commit 798f5895a0

79
kilo.c
View file

@ -224,10 +224,12 @@ void editorUpdateRow(erow *row) {
row->rsize = idx; 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].size = len;
E.row[at].chars = malloc(len + 1); E.row[at].chars = malloc(len + 1);
memcpy(E.row[at].chars, s, len); memcpy(E.row[at].chars, s, len);
@ -241,6 +243,19 @@ 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) at = row->size; if (at < 0 || at > row->size) at = row->size;
row->chars = realloc(row->chars, row->size + 2); row->chars = realloc(row->chars, row->size + 2);
@ -251,17 +266,66 @@ 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) {
if (at < 0 || at >= row->size) return;
memmove(&row->chars[at], &row->chars[at + 1], row->size - at);
row->size--;
editorUpdateRow(row);
E.dirty++;
}
/*** editor operations ***/ /*** editor operations ***/
void editorInsertChar(int c) { void editorInsertChar(int c) {
if (E.cy == E.numrows) { if (E.cy == E.numrows) {
editorAppendRow("", 0); editorInsertRow(E.numrows, "", 0);
} }
editorRowInsertChar(&E.row[E.cy], E.cx, c); editorRowInsertChar(&E.row[E.cy], E.cx, c);
E.cx++; 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) return;
if (E.cx == 0 && E.cy == 0) return;
erow *row = &E.row[E.cy];
if (E.cx > 0) {
editorRowDelChar(row, E.cx - 1);
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 ***/
char *editorRowsToString(int *buflen) { char *editorRowsToString(int *buflen) {
@ -298,7 +362,7 @@ void editorOpen(char *filename) {
while (linelen > 0 && (line[linelen - 1] == '\n' || while (linelen > 0 && (line[linelen - 1] == '\n' ||
line[linelen - 1] == '\r')) line[linelen - 1] == '\r'))
linelen--; linelen--;
editorAppendRow(line, linelen); editorInsertRow(E.numrows, line, linelen);
} }
free(line); free(line);
fclose(fp); fclose(fp);
@ -514,7 +578,7 @@ void editorProcessKeypress() {
switch (c) { switch (c) {
case '\r': case '\r':
/* TODO */ editorInsertNewLine();
break; break;
case CTRL_KEY('q'): case CTRL_KEY('q'):
@ -546,7 +610,8 @@ void editorProcessKeypress() {
case BACKSPACE: case BACKSPACE:
case CTRL_KEY('h'): case CTRL_KEY('h'):
case DEL_KEY: case DEL_KEY:
/* TODO */ if (c == DEL_KEY) editorMoveCursor(ARROW_RIGHT);
editorDelChar();
break; break;
case PAGE_UP: case PAGE_UP: