Compare commits

..
8 Commits
+130 -17
View File
@@ -17,6 +17,7 @@
/*** defines ***/
#define KILO_VERSION "0.0.1"
#define KILO_TAB_STOP 8
#define CTRL_KEY(k) ((k) & 0x1f)
@@ -36,11 +37,15 @@ enum editorKey {
typedef struct erow {
int size;
int rsize;
char *chars;
char *render;
} erow;
struct editorConfig {
int cx, cy;
int rx;
int rowoff, coloff;
int screenrows;
int screencols;
int numrows;
@@ -53,11 +58,6 @@ struct editorConfig E;
/*** terminal ***/
void die(const char* s) {
// Clear the whole screen.
write(STDOUT_FILENO, "\x1b[2J", 4);
// Move cursor to top left.
write(STDOUT_FILENO, "\x1b[H", 3);
perror(s);
exit(1);
}
@@ -65,6 +65,10 @@ void die(const char* s) {
void disableRawMode() {
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &E.orig_termios) == -1)
die("tcsetattr");
// Leave alternate screen, content and cursor will be restored.
if (write(STDOUT_FILENO, "\x1b[?1049l", 8) != 8)
die("leaving alternate screen");
}
void enableRawMode() {
@@ -85,6 +89,10 @@ void enableRawMode() {
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1)
die("tcsetattr");
// Enter alternate screen, needed for keys to work correctly on some environmnets (tmux/herdr).
if (write(STDOUT_FILENO, "\x1b[?1049h", 8) != 8)
die("entering alternate screen");
}
int editorReadKey() {
@@ -196,6 +204,53 @@ int getWindowSize(int *rows, int *cols) {
/*** row operations ***/
// editorRowCxToRx transforms an index pointing to the chars string
// into an index pointing into the render string.
//
// For now it reverts to the behavior of treating tabs as individual
// characters, even if they're actually rendered as spaces.
int editorRowCxToRx(erow *row, int cx) {
int rx = 0;
int j;
for (j = 0; j < cx; j++) {
if (row->chars[j] == '\t')
// Max size minus how many characters already on the left:
// Assume stop = 8:
// XXXXX\t--
// Second term represents the Xs, i.e. 5.
rx += (KILO_TAB_STOP - 1) - (rx % KILO_TAB_STOP);
rx++;
}
return rx;
}
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);
// tabs already counted as one char.
row->render = malloc(row->size + tabs*(KILO_TAB_STOP-1) + 1);
int rdx = 0;
for (j = 0; j < row->size; j++) {
if (row->chars[j] == '\t') {
row->render[rdx++] = ' ';
// This doesn't just implement a given number of characters per tab,
// but the actual behavior of adjusting the size based on positioning.
while (rdx % KILO_TAB_STOP != 0) row->render[rdx++] = ' ';
} else {
row->render[rdx++] = row->chars[j];
}
}
row->render[rdx] = '\0';
row->rsize = rdx;
}
void editorAppendRow(char *r, size_t len) {
E.row = realloc(E.row, sizeof(erow) * (E.numrows + 1));
@@ -205,6 +260,10 @@ void editorAppendRow(char *r, size_t len) {
memcpy(E.row[at].chars, r, len);
E.row[at].chars[len] = '\0';
E.row[at].rsize = 0;
E.row[at].render = NULL;
editorUpdateRow(&E.row[at]);
E.numrows++;
}
@@ -253,9 +312,31 @@ void abFree(struct abuf *ab) {
/*** output ***/
void editorScroll() {
// TODO: I _think_ this conversion should live in editorMoveCursor?
E.rx = 0;
// Last line (emtpy) is always at 0.
if (E.cy < E.numrows)
E.rx = editorRowCxToRx(&E.row[E.cy], E.cx);
// Scrolled up passed the top.
if (E.cy < E.rowoff)
E.rowoff = E.cy;
// Scrolled down passed then bottom.
if (E.cy >= E.rowoff + E.screenrows)
E.rowoff = E.cy - E.screenrows + 1;
// Scrolled passed the left.
if (E.rx < E.coloff)
E.coloff = E.rx;
// Scrolled passed the right.
if (E.rx >= E.coloff + E.screencols)
E.coloff = E.rx - E.screencols + 1;
}
void editorDrawRows(struct abuf *ab) {
for (int y = 0; y < E.screenrows; y++) {
if (y >= E.numrows) {
int filerow = y + E.rowoff;
if (filerow >= E.numrows) {
// We spilled over the file content.
if (E.numrows == 0 && y == E.screenrows / 3) {
char welcome[80];
@@ -279,10 +360,12 @@ void editorDrawRows(struct abuf *ab) {
abAppend(ab, "~", 1);
}
} else {
int len = E.row[y].size;
int len = E.row[filerow].rsize - E.coloff;
if (len < 0)
len = 0;
if (len > E.screencols)
len = E.screencols;
abAppend(ab, E.row[y].chars, len);
abAppend(ab, &E.row[filerow].render[E.coloff], len);
}
// Erase the line content to the right of the cursor.
@@ -296,6 +379,8 @@ void editorDrawRows(struct abuf *ab) {
}
void editorRefreshScreen() {
editorScroll();
struct abuf ab = ABUF_INIT;
// Hide cursor.
abAppend(&ab, "\x1b[?25l", 6);
@@ -306,7 +391,10 @@ void editorRefreshScreen() {
// Move cursor to its saved position.
char posbuf[32];
snprintf(posbuf, sizeof(posbuf), "\x1b[%d;%dH", E.cy+1, E.cx+1);
snprintf(posbuf, sizeof(posbuf), "\x1b[%d;%dH",
(E.cy - E.rowoff) + 1,
(E.rx - E.coloff) + 1
);
abAppend(&ab, posbuf, strlen(posbuf));
@@ -320,15 +408,24 @@ void editorRefreshScreen() {
/*** input ***/
void editorMoveCursor(int key) {
// Cursor can be one past the end of the file.
erow *row= (E.cy >= E.numrows) ? NULL : &E.row[E.cy];
switch (key) {
case ARROW_LEFT:
if (E.cx != 0) {
E.cx--;
} else if (E.cy > 0) { // Go to previous line on left at start.
E.cy--;
E.cx = E.row[E.cy].size;
}
break;
case ARROW_RIGHT:
if (E.cx != E.screencols - 1) {
if (row && E.cx < row->size) {
E.cx++;
} else if (row && E.cx == row->size) { // Go to next line on right at end.
E.cy++;
E.cx = 0;
}
break;
case ARROW_UP:
@@ -337,11 +434,18 @@ void editorMoveCursor(int key) {
}
break;
case ARROW_DOWN:
if (E.cy != E.screenrows - 1) {
if (E.cy < E.numrows) {
E.cy++;
}
break;
}
// Cursor could have moved past the end as a result of the scroll action.
row = (E.cy >= E.numrows) ? NULL : &E.row[E.cy];
int rowlen = row ? row->size : 0;
if (E.cx > rowlen) {
E.cx = rowlen;
}
}
void editorProcessKeypress() {
@@ -349,11 +453,6 @@ void editorProcessKeypress() {
switch (c) {
case CTRL_KEY('q'):
// Clear the whole screen.
write(STDOUT_FILENO, "\x1b[2J", 4);
// Move cursor to top left.
write(STDOUT_FILENO, "\x1b[H", 3);
exit(0);
break;
@@ -361,11 +460,22 @@ void editorProcessKeypress() {
E.cx = 0;
break;
case END_KEY:
E.cx = E.screencols - 1;
if (E.cy < E.numrows)
E.cx = E.row[E.cy].size;
break;
case PAGE_UP:
case PAGE_DOWN: {
// Position either at the top or bottom of the page.
if (c == PAGE_UP) {
E.cy = E.rowoff;
} else {
E.cy = E.rowoff + E.screenrows - 1;
if (E.cy > E.numrows)
E.cy = E.numrows;
}
// Then simulate an entire screen's worth of movement.
int times = E.screenrows;
while (times--) {
editorMoveCursor(c == PAGE_UP ? ARROW_UP : ARROW_DOWN);
@@ -386,6 +496,9 @@ void editorProcessKeypress() {
void initEditor() {
E.cx = 0;
E.cy = 0;
E.rx = 0;
E.rowoff = 0;
E.coloff = 0;
E.row = NULL;
E.numrows = 0;
if (getWindowSize(&E.screenrows, &E.screencols) == -1) {