This commit is contained in:
Aaron Fischer 2018-10-01 17:03:40 +02:00
parent b9f9aec88b
commit 0a8cb73290

62
kilo.c
View file

@ -17,6 +17,7 @@
/*** defines ***/ /*** defines ***/
#define KILO_VERSION "0.0.1" #define KILO_VERSION "0.0.1"
#define KILO_TAB_STOP 8
#define CTRL_KEY(k) ((k) & 0x1f) #define CTRL_KEY(k) ((k) & 0x1f)
@ -37,12 +38,15 @@ enum editorKey {
typedef struct erow { typedef struct erow {
int size; int size;
int rsize;
char *chars; char *chars;
char *render;
} erow; } erow;
struct editorConfig { struct editorConfig {
int cx, cy; int cx, cy;
int rowoff; int rowoff;
int coloff;
int screenrows; int screenrows;
int screencols; int screencols;
int numrows; int numrows;
@ -171,6 +175,29 @@ int getWindowSize(int *rows, int *cols) {
/*** row operations ***/ /*** row operations ***/
void editorUpdateRow(erow *row) {
int tabs = 0;
int j;
for (j=0; j<row->size; j++) {
if (row->chars[j] == '\t') tabs++;
}
free(row->render);
row->render = malloc(row->size + tabs*(KILO_TAB_STOP-1) + 1);
int idx = 0;
for (j=0; j<row->size; j++) {
if (row->chars[j] == '\t') {
row->render[idx++] = ' ';
while (idx%KILO_TAB_STOP != 0) row->render[idx++] = ' ';
} else {
row->render[idx++] = row->chars[j];
}
}
row->render[idx] = '\0';
row->rsize = idx;
}
void editorAppendRow(char *s, size_t len) { void editorAppendRow(char *s, size_t len) {
E.row = realloc(E.row, sizeof(erow) * (E.numrows +1)); E.row = realloc(E.row, sizeof(erow) * (E.numrows +1));
@ -179,6 +206,11 @@ void editorAppendRow(char *s, size_t 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);
E.row[at].chars[len] = '\0'; E.row[at].chars[len] = '\0';
E.row[at].rsize = 0;
E.row[at].render = NULL;
editorUpdateRow(&E.row[at]);
E.numrows++; E.numrows++;
} }
@ -230,6 +262,12 @@ void editorScroll() {
if (E.cy >= E.rowoff + E.screenrows) { if (E.cy >= E.rowoff + E.screenrows) {
E.rowoff = E.cy - E.screenrows + 1; E.rowoff = E.cy - E.screenrows + 1;
} }
if (E.cx < E.coloff) {
E.coloff = E.cx;
}
if (E.cx >= E.coloff + E.screencols) {
E.coloff = E.cx - E.screencols + 1;
}
} }
void editorDrawRows(struct abuf *ab) { void editorDrawRows(struct abuf *ab) {
@ -253,9 +291,10 @@ void editorDrawRows(struct abuf *ab) {
abAppend(ab, "~", 1); abAppend(ab, "~", 1);
} }
} else { } else {
int len = E.row[filerow].size; int len = E.row[filerow].rsize - E.coloff;
if (len < 0) len = 0;
if (len > E.screencols) len = E.screencols; if (len > E.screencols) len = E.screencols;
abAppend(ab, E.row[filerow].chars, len); abAppend(ab, &E.row[filerow].render[E.coloff], len);
} }
abAppend(ab, "\x1b[K", 3); // Erase one line abAppend(ab, "\x1b[K", 3); // Erase one line
@ -277,7 +316,7 @@ void editorRefreshScreen() {
// Update cursor position // Update cursor position
char buf[32]; char buf[32];
snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cy - E.rowoff) + 1, E.cx + 1); snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cy - E.rowoff) + 1, (E.cx - E.coloff) + 1);
abAppend(&ab, buf, strlen(buf)); abAppend(&ab, buf, strlen(buf));
abAppend(&ab, "\x1b[?25h", 6); // Show cursor abAppend(&ab, "\x1b[?25h", 6); // Show cursor
@ -289,15 +328,23 @@ void editorRefreshScreen() {
/*** input ***/ /*** input ***/
void editorMoveCursor(int key) { void editorMoveCursor(int key) {
erow *row = (E.cy >= E.numrows) ? NULL : &E.row[E.cy];
switch (key) { switch (key) {
case ARROW_LEFT: case ARROW_LEFT:
if (E.cx != 0) { if (E.cx != 0) {
E.cx--; E.cx--;
} else if (E.cy > 0) {
E.cy--;
E.cx = E.row[E.cy].size;
} }
break; break;
case ARROW_RIGHT: case ARROW_RIGHT:
if (E.cx != E.screencols -1) { if (row && E.cx < row->size) {
E.cx++; E.cx++;
} else if (row && E.cx == row->size) {
E.cy++;
E.cx = 0;
} }
break; break;
case ARROW_UP: case ARROW_UP:
@ -311,6 +358,12 @@ void editorMoveCursor(int key) {
} }
break; break;
} }
row = (E.cy >= E.numrows) ? NULL : &E.row[E.cy];
int rowlen = row ? row->size : 0;
if (E.cx > rowlen) {
E.cx = rowlen;
}
} }
@ -356,6 +409,7 @@ void initEditor() {
E.cx = 0; E.cx = 0;
E.cy = 0; E.cy = 0;
E.rowoff = 0; E.rowoff = 0;
E.coloff = 0;
E.numrows = 0; E.numrows = 0;
E.row = NULL; E.row = NULL;