Compare commits
18
Commits
1fe8355928
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bdf359e2ff | ||
|
|
07ad789f65 | ||
|
|
051ff4ccde | ||
|
|
c65fcc5985 | ||
|
|
8821964375 | ||
|
|
10e3427740 | ||
|
|
779b0808e0 | ||
|
|
ad1f197450 | ||
|
|
691da8ec8c | ||
|
|
d12f61f7c4 | ||
|
|
b06cab286c | ||
|
|
9c20e2d463 | ||
|
|
b85d656a74 | ||
|
|
6b59986fca | ||
|
|
ef1f3c69ac | ||
|
|
e27f9c08de | ||
|
|
9fe7f35932 | ||
|
|
49cb6ab62a |
@@ -1,23 +1,55 @@
|
||||
/*** includes ***/
|
||||
|
||||
#include <asm-generic/ioctls.h>
|
||||
#define _DEFAULT_SOURCE
|
||||
#define _BSD_SOURCE
|
||||
#define _GNU_SOURCE
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/types.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/*** defines ***/
|
||||
|
||||
#define KILO_VERSION "0.0.1"
|
||||
#define KILO_TAB_STOP 8
|
||||
|
||||
#define CTRL_KEY(k) ((k) & 0x1f)
|
||||
|
||||
enum editorKey {
|
||||
ARROW_LEFT = 1000,
|
||||
ARROW_RIGHT,
|
||||
ARROW_UP,
|
||||
ARROW_DOWN,
|
||||
DEL_KEY,
|
||||
HOME_KEY,
|
||||
END_KEY,
|
||||
PAGE_UP,
|
||||
PAGE_DOWN
|
||||
};
|
||||
|
||||
/*** data ***/
|
||||
|
||||
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;
|
||||
erow *row;
|
||||
struct termios orig_termios;
|
||||
};
|
||||
|
||||
@@ -26,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);
|
||||
}
|
||||
@@ -38,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() {
|
||||
@@ -58,9 +89,13 @@ 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");
|
||||
}
|
||||
|
||||
char editorReadKey() {
|
||||
int editorReadKey() {
|
||||
int nread;
|
||||
char c;
|
||||
while ((nread = read(STDIN_FILENO, &c, 1)) != 1) {
|
||||
@@ -68,6 +103,55 @@ char editorReadKey() {
|
||||
die("read");
|
||||
}
|
||||
}
|
||||
|
||||
// Read arrow key escape sequences as single characters.
|
||||
if (c == '\x1b') {
|
||||
char seq[3];
|
||||
|
||||
if (read(STDIN_FILENO, &seq[0], 1) != 1)
|
||||
return '\x1b';
|
||||
if (read(STDIN_FILENO, &seq[1], 1) != 1)
|
||||
return '\x1b';
|
||||
|
||||
if (seq[0] == '[') {
|
||||
if (seq[1] >= '0' && seq[1] <= '9') {
|
||||
// Parse a single digit escape seq arg:
|
||||
if (read(STDIN_FILENO, &seq[2], 1) != 1)
|
||||
return '\x1b';
|
||||
// `~` is the universal terminator for extended numeric key codes.
|
||||
if (seq[2] == '~') {
|
||||
// NOTE: On MacOS and Ghostty specifically, fn+arrow[up/down] is
|
||||
// not sent as a page up/dow. Because it can mess up system keybindings.
|
||||
switch (seq[1]) {
|
||||
case '1': return HOME_KEY;
|
||||
case '3': return DEL_KEY;
|
||||
case '4': return END_KEY;
|
||||
case '5': return PAGE_UP;
|
||||
case '6': return PAGE_DOWN;
|
||||
case '7': return HOME_KEY;
|
||||
case '8': return END_KEY;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Parse no arg seq otherwise:
|
||||
switch (seq[1]) {
|
||||
case 'A': return ARROW_UP;
|
||||
case 'B': return ARROW_DOWN;
|
||||
case 'C': return ARROW_RIGHT;
|
||||
case 'D': return ARROW_LEFT;
|
||||
case 'H': return HOME_KEY;
|
||||
case 'F': return END_KEY;
|
||||
}
|
||||
}
|
||||
} else if (seq[0] == 'O') {
|
||||
switch (seq[1]) {
|
||||
case 'H': return HOME_KEY;
|
||||
case 'F': return END_KEY;
|
||||
}
|
||||
}
|
||||
|
||||
return '\x1b';
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
@@ -118,59 +202,316 @@ int getWindowSize(int *rows, int *cols) {
|
||||
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 ***/
|
||||
|
||||
struct abuf {
|
||||
char *b;
|
||||
int len;
|
||||
};
|
||||
|
||||
#define ABUF_INIT {NULL, 0}
|
||||
|
||||
void abAppend(struct abuf *ab, const char *s, int len) {
|
||||
char *new = realloc(ab->b, ab->len + len);
|
||||
if (new == NULL)
|
||||
return;
|
||||
// Copy s starting at the end of the existing content.
|
||||
memcpy(&new[ab->len], s, len);
|
||||
ab->b = new;
|
||||
ab->len += len;
|
||||
}
|
||||
|
||||
void abFree(struct abuf *ab) {
|
||||
free(ab->b);
|
||||
}
|
||||
|
||||
/*** output ***/
|
||||
|
||||
void editorDrawRows() {
|
||||
for (int y = 0; y < E.screenrows; y++) {
|
||||
write(STDOUT_FILENO, "~", 1);
|
||||
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++) {
|
||||
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];
|
||||
int welcomelen = snprintf(
|
||||
welcome,
|
||||
sizeof(welcome),
|
||||
"Kilo editor -- version %s",
|
||||
KILO_VERSION
|
||||
);
|
||||
if (welcomelen > E.screencols)
|
||||
welcomelen = E.screencols;
|
||||
int padding = (E.screencols - welcomelen) / 2;
|
||||
if (padding) {
|
||||
abAppend(ab, "~", 1);
|
||||
padding--;
|
||||
}
|
||||
while (padding--)
|
||||
abAppend(ab, " ", 1);
|
||||
abAppend(ab, welcome, welcomelen);
|
||||
} else {
|
||||
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.
|
||||
abAppend(ab, "\x1b[K", 3);
|
||||
// No new line on the last line, the terminal scrolls past the
|
||||
// first line otherwise.
|
||||
if (y < E.screenrows - 1) {
|
||||
write(STDOUT_FILENO, "\r\n", 2);
|
||||
abAppend(ab, "\r\n", 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void editorRefreshScreen() {
|
||||
// Clear the whole screen.
|
||||
write(STDOUT_FILENO, "\x1b[2J", 4);
|
||||
// Move cursor to top left.
|
||||
write(STDOUT_FILENO, "\x1b[H", 3);
|
||||
editorScroll();
|
||||
|
||||
editorDrawRows();
|
||||
struct abuf ab = ABUF_INIT;
|
||||
// Hide cursor.
|
||||
abAppend(&ab, "\x1b[?25l", 6);
|
||||
// Move cursor to top left to begin drawing.
|
||||
abAppend(&ab, "\x1b[H", 3);
|
||||
|
||||
write(STDOUT_FILENO, "\x1b[H", 3);
|
||||
editorDrawRows(&ab);
|
||||
|
||||
// Move cursor to its saved position.
|
||||
char posbuf[32];
|
||||
snprintf(posbuf, sizeof(posbuf), "\x1b[%d;%dH",
|
||||
(E.cy - E.rowoff) + 1,
|
||||
(E.rx - E.coloff) + 1
|
||||
);
|
||||
abAppend(&ab, posbuf, strlen(posbuf));
|
||||
|
||||
|
||||
// Show cursor again.
|
||||
abAppend(&ab, "\x1b[?25h", 6);
|
||||
|
||||
write(STDOUT_FILENO, ab.b, ab.len);
|
||||
abFree(&ab);
|
||||
}
|
||||
|
||||
/*** 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 (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:
|
||||
if (E.cy != 0) {
|
||||
E.cy--;
|
||||
}
|
||||
break;
|
||||
case ARROW_DOWN:
|
||||
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() {
|
||||
char c = editorReadKey();
|
||||
int c = editorReadKey();
|
||||
|
||||
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;
|
||||
|
||||
case HOME_KEY:
|
||||
E.cx = 0;
|
||||
break;
|
||||
case END_KEY:
|
||||
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);
|
||||
}
|
||||
} break;
|
||||
|
||||
case ARROW_UP:
|
||||
case ARROW_LEFT:
|
||||
case ARROW_DOWN:
|
||||
case ARROW_RIGHT:
|
||||
editorMoveCursor(c);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*** init ***/
|
||||
|
||||
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) {
|
||||
die("getWindowSize");
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main(int argc, char *argv[]) {
|
||||
enableRawMode();
|
||||
initEditor();
|
||||
if (argc > 1) {
|
||||
editorOpen(argv[1]);
|
||||
}
|
||||
|
||||
while(1) {
|
||||
editorRefreshScreen();
|
||||
|
||||
Reference in New Issue
Block a user