Compare commits
11
Commits
9c20e2d463
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bdf359e2ff | ||
|
|
07ad789f65 | ||
|
|
051ff4ccde | ||
|
|
c65fcc5985 | ||
|
|
8821964375 | ||
|
|
10e3427740 | ||
|
|
779b0808e0 | ||
|
|
ad1f197450 | ||
|
|
691da8ec8c | ||
|
|
d12f61f7c4 | ||
|
|
b06cab286c |
@@ -1,18 +1,23 @@
|
|||||||
/*** includes ***/
|
/*** includes ***/
|
||||||
|
|
||||||
#include <asm-generic/ioctls.h>
|
#define _DEFAULT_SOURCE
|
||||||
|
#define _BSD_SOURCE
|
||||||
|
#define _GNU_SOURCE
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
|
#include <sys/types.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
/*** 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)
|
||||||
|
|
||||||
@@ -21,6 +26,7 @@ enum editorKey {
|
|||||||
ARROW_RIGHT,
|
ARROW_RIGHT,
|
||||||
ARROW_UP,
|
ARROW_UP,
|
||||||
ARROW_DOWN,
|
ARROW_DOWN,
|
||||||
|
DEL_KEY,
|
||||||
HOME_KEY,
|
HOME_KEY,
|
||||||
END_KEY,
|
END_KEY,
|
||||||
PAGE_UP,
|
PAGE_UP,
|
||||||
@@ -29,10 +35,21 @@ enum editorKey {
|
|||||||
|
|
||||||
/*** data ***/
|
/*** data ***/
|
||||||
|
|
||||||
|
typedef struct erow {
|
||||||
|
int size;
|
||||||
|
int rsize;
|
||||||
|
char *chars;
|
||||||
|
char *render;
|
||||||
|
} erow;
|
||||||
|
|
||||||
struct editorConfig {
|
struct editorConfig {
|
||||||
int cx, cy;
|
int cx, cy;
|
||||||
|
int rx;
|
||||||
|
int rowoff, coloff;
|
||||||
int screenrows;
|
int screenrows;
|
||||||
int screencols;
|
int screencols;
|
||||||
|
int numrows;
|
||||||
|
erow *row;
|
||||||
struct termios orig_termios;
|
struct termios orig_termios;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -41,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);
|
||||||
}
|
}
|
||||||
@@ -53,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() {
|
||||||
@@ -73,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() {
|
||||||
@@ -104,6 +124,7 @@ int editorReadKey() {
|
|||||||
// not sent as a page up/dow. Because it can mess up system keybindings.
|
// not sent as a page up/dow. Because it can mess up system keybindings.
|
||||||
switch (seq[1]) {
|
switch (seq[1]) {
|
||||||
case '1': return HOME_KEY;
|
case '1': return HOME_KEY;
|
||||||
|
case '3': return DEL_KEY;
|
||||||
case '4': return END_KEY;
|
case '4': return END_KEY;
|
||||||
case '5': return PAGE_UP;
|
case '5': return PAGE_UP;
|
||||||
case '6': return PAGE_DOWN;
|
case '6': return PAGE_DOWN;
|
||||||
@@ -181,6 +202,91 @@ int getWindowSize(int *rows, int *cols) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*** 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));
|
||||||
|
|
||||||
|
int at = E.numrows;
|
||||||
|
E.row[at].size = len;
|
||||||
|
E.row[at].chars = malloc(len+1);
|
||||||
|
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++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*** file i/o ***/
|
||||||
|
|
||||||
|
void editorOpen(char *filename) {
|
||||||
|
FILE *fp = fopen(filename, "r");
|
||||||
|
if (!fp)
|
||||||
|
die("fopen");
|
||||||
|
|
||||||
|
char *line = NULL;
|
||||||
|
size_t linecap = 0;
|
||||||
|
ssize_t linelen;
|
||||||
|
while ((linelen = getline(&line, &linecap, fp)) != -1) {
|
||||||
|
while (linelen > 0 && (line[linelen-1] == '\n' ||
|
||||||
|
line[linelen-1] == '\r'))
|
||||||
|
linelen--;
|
||||||
|
editorAppendRow(line, linelen);
|
||||||
|
}
|
||||||
|
free(line);
|
||||||
|
fclose(fp);
|
||||||
|
}
|
||||||
|
|
||||||
/*** append buffer ***/
|
/*** append buffer ***/
|
||||||
|
|
||||||
struct abuf {
|
struct abuf {
|
||||||
@@ -206,9 +312,33 @@ void abFree(struct abuf *ab) {
|
|||||||
|
|
||||||
/*** output ***/
|
/*** 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) {
|
void editorDrawRows(struct abuf *ab) {
|
||||||
for (int y = 0; y < E.screenrows; y++) {
|
for (int y = 0; y < E.screenrows; y++) {
|
||||||
if (y == E.screenrows / 3) {
|
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];
|
char welcome[80];
|
||||||
int welcomelen = snprintf(
|
int welcomelen = snprintf(
|
||||||
welcome,
|
welcome,
|
||||||
@@ -229,6 +359,14 @@ void editorDrawRows(struct abuf *ab) {
|
|||||||
} else {
|
} else {
|
||||||
abAppend(ab, "~", 1);
|
abAppend(ab, "~", 1);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
int len = E.row[filerow].rsize - E.coloff;
|
||||||
|
if (len < 0)
|
||||||
|
len = 0;
|
||||||
|
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.
|
||||||
abAppend(ab, "\x1b[K", 3);
|
abAppend(ab, "\x1b[K", 3);
|
||||||
@@ -241,6 +379,8 @@ void editorDrawRows(struct abuf *ab) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void editorRefreshScreen() {
|
void editorRefreshScreen() {
|
||||||
|
editorScroll();
|
||||||
|
|
||||||
struct abuf ab = ABUF_INIT;
|
struct abuf ab = ABUF_INIT;
|
||||||
// Hide cursor.
|
// Hide cursor.
|
||||||
abAppend(&ab, "\x1b[?25l", 6);
|
abAppend(&ab, "\x1b[?25l", 6);
|
||||||
@@ -251,7 +391,10 @@ void editorRefreshScreen() {
|
|||||||
|
|
||||||
// Move cursor to its saved position.
|
// Move cursor to its saved position.
|
||||||
char posbuf[32];
|
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));
|
abAppend(&ab, posbuf, strlen(posbuf));
|
||||||
|
|
||||||
|
|
||||||
@@ -265,15 +408,24 @@ void editorRefreshScreen() {
|
|||||||
/*** input ***/
|
/*** input ***/
|
||||||
|
|
||||||
void editorMoveCursor(int key) {
|
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) {
|
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) { // Go to previous line on left at start.
|
||||||
|
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) { // Go to next line on right at end.
|
||||||
|
E.cy++;
|
||||||
|
E.cx = 0;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ARROW_UP:
|
case ARROW_UP:
|
||||||
@@ -282,11 +434,18 @@ void editorMoveCursor(int key) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ARROW_DOWN:
|
case ARROW_DOWN:
|
||||||
if (E.cy != E.screenrows - 1) {
|
if (E.cy < E.numrows) {
|
||||||
E.cy++;
|
E.cy++;
|
||||||
}
|
}
|
||||||
break;
|
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() {
|
void editorProcessKeypress() {
|
||||||
@@ -294,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;
|
||||||
|
|
||||||
@@ -306,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);
|
||||||
@@ -331,14 +496,22 @@ 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.coloff = 0;
|
||||||
|
E.row = NULL;
|
||||||
|
E.numrows = 0;
|
||||||
if (getWindowSize(&E.screenrows, &E.screencols) == -1) {
|
if (getWindowSize(&E.screenrows, &E.screencols) == -1) {
|
||||||
die("getWindowSize");
|
die("getWindowSize");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main(int argc, char *argv[]) {
|
||||||
enableRawMode();
|
enableRawMode();
|
||||||
initEditor();
|
initEditor();
|
||||||
|
if (argc > 1) {
|
||||||
|
editorOpen(argv[1]);
|
||||||
|
}
|
||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
editorRefreshScreen();
|
editorRefreshScreen();
|
||||||
|
|||||||
Reference in New Issue
Block a user