Compare commits

...
5 Commits
+91 -18
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)
@@ -36,11 +37,14 @@ 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 rx;
int rowoff, coloff; int rowoff, coloff;
int screenrows; int screenrows;
int screencols; int screencols;
@@ -54,11 +58,6 @@ struct editorConfig E;
/*** terminal ***/ /*** terminal ***/
void die(const char* s) { 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); perror(s);
exit(1); exit(1);
} }
@@ -66,6 +65,10 @@ void die(const char* s) {
void disableRawMode() { void disableRawMode() {
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &E.orig_termios) == -1) if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &E.orig_termios) == -1)
die("tcsetattr"); 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() { void enableRawMode() {
@@ -86,6 +89,10 @@ void enableRawMode() {
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1)
die("tcsetattr"); 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() { int editorReadKey() {
@@ -197,6 +204,53 @@ int getWindowSize(int *rows, int *cols) {
/*** row operations ***/ /*** 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) { void editorAppendRow(char *r, size_t len) {
E.row = realloc(E.row, sizeof(erow) * (E.numrows + 1)); E.row = realloc(E.row, sizeof(erow) * (E.numrows + 1));
@@ -206,6 +260,10 @@ void editorAppendRow(char *r, size_t len) {
memcpy(E.row[at].chars, r, len); memcpy(E.row[at].chars, r, 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++;
} }
@@ -255,6 +313,12 @@ void abFree(struct abuf *ab) {
/*** output ***/ /*** output ***/
void editorScroll() { 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. // Scrolled up passed the top.
if (E.cy < E.rowoff) if (E.cy < E.rowoff)
E.rowoff = E.cy; E.rowoff = E.cy;
@@ -262,11 +326,11 @@ 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;
// Scrolled passed the left. // Scrolled passed the left.
if (E.cx < E.coloff) if (E.rx < E.coloff)
E.coloff = E.cx; E.coloff = E.rx;
// Scrolled passed the right. // Scrolled passed the right.
if (E.cx >= E.coloff + E.screencols) if (E.rx >= E.coloff + E.screencols)
E.coloff = E.cx - E.screencols + 1; E.coloff = E.rx - E.screencols + 1;
} }
void editorDrawRows(struct abuf *ab) { void editorDrawRows(struct abuf *ab) {
@@ -296,10 +360,12 @@ void editorDrawRows(struct abuf *ab) {
abAppend(ab, "~", 1); abAppend(ab, "~", 1);
} }
} else { } else {
int len = E.row[filerow].size - E.coloff; int len = E.row[filerow].rsize - E.coloff;
if (len < 0) if (len < 0)
len = 0; len = 0;
abAppend(ab, &E.row[filerow].chars[E.coloff], len); if (len > E.screencols)
len = E.screencols;
abAppend(ab, &E.row[filerow].render[E.coloff], len);
} }
// Erase the line content to the right of the cursor. // Erase the line content to the right of the cursor.
@@ -327,7 +393,7 @@ void editorRefreshScreen() {
char posbuf[32]; char posbuf[32];
snprintf(posbuf, sizeof(posbuf), "\x1b[%d;%dH", snprintf(posbuf, sizeof(posbuf), "\x1b[%d;%dH",
(E.cy - E.rowoff) + 1, (E.cy - E.rowoff) + 1,
(E.cx - E.coloff) + 1 (E.rx - E.coloff) + 1
); );
abAppend(&ab, posbuf, strlen(posbuf)); abAppend(&ab, posbuf, strlen(posbuf));
@@ -387,11 +453,6 @@ void editorProcessKeypress() {
switch (c) { switch (c) {
case CTRL_KEY('q'): 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); exit(0);
break; break;
@@ -399,11 +460,22 @@ void editorProcessKeypress() {
E.cx = 0; E.cx = 0;
break; break;
case END_KEY: case END_KEY:
E.cx = E.screencols - 1; if (E.cy < E.numrows)
E.cx = E.row[E.cy].size;
break; break;
case PAGE_UP: case PAGE_UP:
case PAGE_DOWN: { 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; int times = E.screenrows;
while (times--) { while (times--) {
editorMoveCursor(c == PAGE_UP ? ARROW_UP : ARROW_DOWN); editorMoveCursor(c == PAGE_UP ? ARROW_UP : ARROW_DOWN);
@@ -424,6 +496,7 @@ void editorProcessKeypress() {
void initEditor() { void initEditor() {
E.cx = 0; E.cx = 0;
E.cy = 0; E.cy = 0;
E.rx = 0;
E.rowoff = 0; E.rowoff = 0;
E.coloff = 0; E.coloff = 0;
E.row = NULL; E.row = NULL;