2023-08-24 12:57:25 +00:00
|
|
|
#include <ctype.h>
|
2023-08-24 16:16:36 +00:00
|
|
|
#include <errno.h>
|
2023-08-24 12:57:25 +00:00
|
|
|
#include <stdbool.h>
|
2023-08-22 21:32:16 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2023-08-24 16:16:36 +00:00
|
|
|
#include <string.h>
|
2023-08-24 11:09:03 +00:00
|
|
|
#include <sys/ioctl.h>
|
2023-08-22 21:32:16 +00:00
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
#define CTRL_KEY(key) ((key) & 0x1f)
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2023-08-24 16:16:36 +00:00
|
|
|
|
|
|
|
struct {
|
|
|
|
struct termios orig_termios;
|
|
|
|
int rows;
|
|
|
|
int cols;
|
|
|
|
int cx, cy;
|
2023-08-25 18:13:33 +00:00
|
|
|
struct input_buf *ib;
|
|
|
|
} E;
|
2023-08-24 16:16:36 +00:00
|
|
|
|
|
|
|
enum keys {
|
2023-08-25 18:13:33 +00:00
|
|
|
HOME = 0x100 + 1,
|
|
|
|
DEL,
|
|
|
|
PG_UP,
|
|
|
|
PG_DOWN,
|
|
|
|
END,
|
|
|
|
ARROW_UP,
|
2023-08-24 16:16:36 +00:00
|
|
|
ARROW_DOWN,
|
|
|
|
ARROW_LEFT,
|
2023-08-25 18:13:33 +00:00
|
|
|
ARROW_RIGHT,
|
|
|
|
NOP
|
2023-08-24 16:16:36 +00:00
|
|
|
};
|
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
struct screen_buf;
|
|
|
|
struct input_buf;
|
2023-08-24 12:57:25 +00:00
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
/*****************************************************************************/
|
2023-08-23 23:27:25 +00:00
|
|
|
|
2023-08-24 11:09:03 +00:00
|
|
|
void editor_init();
|
2023-08-23 23:27:25 +00:00
|
|
|
void editor_redraw_screen();
|
2023-08-25 18:13:33 +00:00
|
|
|
void editor_draw_rows(struct screen_buf *);
|
2023-08-23 23:27:25 +00:00
|
|
|
void editor_process_key();
|
2023-08-24 16:16:36 +00:00
|
|
|
int editor_read_key();
|
2023-08-23 23:27:25 +00:00
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
int term_enable_raw();
|
|
|
|
void term_disable_raw();
|
|
|
|
int term_clear();
|
|
|
|
int term_cursor_hidden(bool);
|
|
|
|
int term_get_win_size(int *, int *);
|
|
|
|
int term_get_cursor_pos(int *, int *);
|
|
|
|
int term_set_cursor_pos(int, int);
|
|
|
|
|
|
|
|
struct screen_buf *sb_init();
|
|
|
|
void sb_append(struct screen_buf *, const char *);
|
|
|
|
void sb_free(struct screen_buf *);
|
|
|
|
void sb_write(struct screen_buf *);
|
|
|
|
|
|
|
|
struct input_buf *ib_init(size_t);
|
|
|
|
int ib_read(struct input_buf *);
|
|
|
|
void ib_write(struct input_buf *, int);
|
|
|
|
bool ib_empty(struct input_buf *);
|
|
|
|
void ib_free(struct input_buf *);
|
|
|
|
|
2023-08-24 11:09:03 +00:00
|
|
|
void die(const char *);
|
2023-08-23 23:27:25 +00:00
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
/*****************************************************************************/
|
2023-08-22 21:32:16 +00:00
|
|
|
|
2023-08-23 23:27:25 +00:00
|
|
|
int main() {
|
2023-08-24 11:09:03 +00:00
|
|
|
editor_init();
|
|
|
|
|
2023-08-23 23:27:25 +00:00
|
|
|
while (1) {
|
|
|
|
editor_redraw_screen();
|
|
|
|
editor_process_key();
|
|
|
|
}
|
2023-08-22 21:32:16 +00:00
|
|
|
|
2023-08-23 23:27:25 +00:00
|
|
|
return 0;
|
2023-08-22 21:32:16 +00:00
|
|
|
}
|
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
2023-08-24 12:57:25 +00:00
|
|
|
void editor_init() {
|
|
|
|
if (!isatty(STDIN_FILENO)) {
|
|
|
|
printf("kilo only supports a terminal at standard in. Exiting.");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
if (term_enable_raw() == -1) die("term_enable_raw");
|
|
|
|
if (term_get_win_size(&E.rows, &E.cols) == -1) die("term_get_win_size");
|
|
|
|
|
|
|
|
E.cx = E.cy = 0;
|
|
|
|
E.ib = ib_init(128);
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_redraw_screen() {
|
|
|
|
if (term_cursor_hidden(true) == -1) die("term_cursor_hidden");
|
|
|
|
|
|
|
|
struct screen_buf *sb = sb_init();
|
|
|
|
editor_draw_rows(sb);
|
|
|
|
sb_write(sb);
|
|
|
|
sb_free(sb);
|
|
|
|
|
|
|
|
if (term_set_cursor_pos(E.cy + 1, E.cx + 1) == -1) die("term_set_cursor_pos");
|
|
|
|
if (term_cursor_hidden(false) == -1) die("term_cursor_hidden");
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_draw_rows(struct screen_buf *sb) {
|
|
|
|
term_set_cursor_pos(1, 1);
|
|
|
|
|
|
|
|
for (int y = 0; y < E.rows; y++) {
|
|
|
|
sb_append(sb, "~");
|
2023-08-24 12:57:25 +00:00
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
sb_append(sb, "\x1b[K"); // Clear rest of the line
|
|
|
|
if (y < E.rows - 1) sb_append(sb, "\r\n");
|
|
|
|
}
|
2023-08-24 12:57:25 +00:00
|
|
|
}
|
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
|
|
|
|
void editor_process_key() {
|
|
|
|
int c = editor_read_key();
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case CTRL_KEY('Q'):
|
|
|
|
term_clear();
|
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARROW_LEFT:
|
|
|
|
if (E.cx > 0) E.cx--;
|
|
|
|
break;
|
|
|
|
case ARROW_DOWN:
|
|
|
|
if (E.cy < E.rows - 1) E.cy++;
|
|
|
|
break;
|
|
|
|
case ARROW_UP:
|
|
|
|
if (E.cy > 0) E.cy--;
|
|
|
|
break;
|
|
|
|
case ARROW_RIGHT:
|
|
|
|
if (E.cx < E.cols - 1) E.cx++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int editor_read_key() {
|
|
|
|
// TODO: There's better ways to do this.
|
|
|
|
// Somehow make everything go through the input buffer
|
|
|
|
if (!ib_empty(E.ib))
|
|
|
|
return ib_read(E.ib);
|
|
|
|
|
|
|
|
char c;
|
|
|
|
while (read(STDIN_FILENO, &c, 1) == 0);
|
|
|
|
|
|
|
|
if (c == '\x1b') {
|
|
|
|
char buf[8];
|
|
|
|
|
|
|
|
for (size_t i = 0; i < sizeof(buf); i++) {
|
|
|
|
if (read(STDIN_FILENO, buf+i, 1) == 0) {
|
|
|
|
buf[i] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char escape_char;
|
|
|
|
if (sscanf(buf, "[%c~", &escape_char) != EOF) {
|
|
|
|
switch (escape_char) {
|
|
|
|
case 'A': return ARROW_UP;
|
|
|
|
case 'B': return ARROW_DOWN;
|
|
|
|
case 'C': return ARROW_RIGHT;
|
|
|
|
case 'D': return ARROW_LEFT;
|
|
|
|
case 'H': return HOME;
|
|
|
|
case 'F': return END;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int escape_int;
|
|
|
|
if (sscanf(buf, "[%d~", &escape_int) != EOF) {
|
|
|
|
switch (escape_int) {
|
|
|
|
case 1:
|
|
|
|
case 7:
|
|
|
|
return HOME;
|
|
|
|
case 3:
|
|
|
|
return DEL;
|
|
|
|
case 4:
|
|
|
|
case 8:
|
|
|
|
return END;
|
|
|
|
case 5:
|
|
|
|
return PG_UP;
|
|
|
|
case 6:
|
|
|
|
return PG_DOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int) c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
int term_enable_raw() {
|
|
|
|
if (tcgetattr(STDIN_FILENO, &E.orig_termios) == -1) return -1;
|
|
|
|
atexit(term_disable_raw);
|
2023-08-22 21:32:16 +00:00
|
|
|
|
2023-08-24 11:09:03 +00:00
|
|
|
struct termios raw = E.orig_termios;
|
2023-08-23 16:36:42 +00:00
|
|
|
cfmakeraw(&raw);
|
2023-08-22 21:32:16 +00:00
|
|
|
|
2023-08-24 12:57:25 +00:00
|
|
|
raw.c_cc[VMIN] = 0;
|
|
|
|
raw.c_cc[VTIME] = 1;
|
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) return -1;
|
|
|
|
|
|
|
|
return 0;
|
2023-08-22 21:32:16 +00:00
|
|
|
}
|
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
void term_disable_raw() {
|
2023-08-24 11:09:03 +00:00
|
|
|
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &E.orig_termios) == -1)
|
2023-08-25 18:13:33 +00:00
|
|
|
die ("term_disable_raw");
|
2023-08-23 23:27:25 +00:00
|
|
|
}
|
2023-08-22 21:32:16 +00:00
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
int term_clear() {
|
|
|
|
if (write(STDOUT_FILENO, "\x1b[2J", 4) != 4) return -1;
|
|
|
|
if (write(STDOUT_FILENO, "\x1b[H", 3) != 3) return -1;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int term_cursor_hidden(bool hidden) {
|
|
|
|
if (write(STDOUT_FILENO, (hidden ? "\x1b[?25l" : "\x1b[?25h"), 6) != 6)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int term_get_win_size(int *row, int *col) {
|
2023-08-24 11:09:03 +00:00
|
|
|
struct winsize ws;
|
|
|
|
|
2023-08-24 12:57:25 +00:00
|
|
|
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1 && ws.ws_col != 0) {
|
2023-08-25 18:13:33 +00:00
|
|
|
*row = ws.ws_row;
|
|
|
|
*col = ws.ws_col;
|
2023-08-24 11:09:03 +00:00
|
|
|
|
|
|
|
return 0;
|
2023-08-24 12:57:25 +00:00
|
|
|
} else {
|
|
|
|
if (write(STDOUT_FILENO, "\x1b[999C\x1b[999B", 12) != 12) return -1;
|
2023-08-24 16:16:36 +00:00
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
return term_get_cursor_pos(row, col);
|
2023-08-24 11:09:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
int term_get_cursor_pos(int *row, int *col) {
|
2023-08-24 12:57:25 +00:00
|
|
|
char buf[32];
|
2023-08-24 11:09:03 +00:00
|
|
|
|
2023-08-24 12:57:25 +00:00
|
|
|
if (write(STDOUT_FILENO, "\x1b[6n", 4) != 4) return -1;
|
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
for (size_t i = 0; i < sizeof(buf); i++) {
|
|
|
|
if (read(STDIN_FILENO, buf+i, 1) == 0 || buf[i] == 'R') {
|
|
|
|
buf[i] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-08-24 12:57:25 +00:00
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
if (sscanf(buf, "\x1b[%d;%d", row, col) == EOF)
|
|
|
|
return -1;
|
2023-08-24 16:16:36 +00:00
|
|
|
|
2023-08-24 12:57:25 +00:00
|
|
|
return 0;
|
2023-08-24 11:09:03 +00:00
|
|
|
}
|
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
int term_set_cursor_pos(int row, int col) {
|
|
|
|
char buf[16];
|
2023-08-22 21:32:16 +00:00
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
size_t len = snprintf(buf, sizeof(buf), "\x1b[%d;%dH", row, col);
|
|
|
|
len = (len >= sizeof(buf) ? sizeof(buf) - 1 : len);
|
2023-08-23 23:27:25 +00:00
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
if (write(STDOUT_FILENO, buf, len) != (ssize_t) len) return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
2023-08-24 16:16:36 +00:00
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
/*****************************************************************************/
|
2023-08-24 16:16:36 +00:00
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
struct screen_buf {
|
|
|
|
char *s;
|
|
|
|
size_t size;
|
|
|
|
};
|
2023-08-24 16:16:36 +00:00
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
struct screen_buf *sb_init() {
|
|
|
|
struct screen_buf *sb = (struct screen_buf *) malloc(sizeof(struct screen_buf));
|
|
|
|
|
|
|
|
sb->s = NULL;
|
|
|
|
sb->size = 0;
|
2023-08-23 23:27:25 +00:00
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
return sb;
|
2023-08-23 16:36:42 +00:00
|
|
|
}
|
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
void sb_append(struct screen_buf *sb, const char *s) {
|
|
|
|
int size = strlen(s);
|
2023-08-24 16:16:36 +00:00
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
sb->s = realloc(sb->s, sb->size + size);
|
|
|
|
memcpy(sb->s + sb->size, s, size);
|
2023-08-23 16:36:42 +00:00
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
sb->size += size;
|
|
|
|
}
|
2023-08-24 16:16:36 +00:00
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
void sb_write(struct screen_buf *sb) {
|
|
|
|
write(STDOUT_FILENO, sb->s, sb->size);
|
|
|
|
}
|
2023-08-24 16:16:36 +00:00
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
void sb_free(struct screen_buf *sb) {
|
|
|
|
free(sb->s);
|
|
|
|
free(sb);
|
2023-08-23 16:36:42 +00:00
|
|
|
}
|
2023-08-22 21:32:16 +00:00
|
|
|
|
2023-08-24 16:16:36 +00:00
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
struct input_buf {
|
|
|
|
int *carr; // Circular array
|
|
|
|
int read_i;
|
|
|
|
int write_i;
|
|
|
|
size_t capacity; // Actual capacity is one less
|
|
|
|
};
|
2023-08-24 16:16:36 +00:00
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
struct input_buf *ib_init(size_t capacity) {
|
|
|
|
struct input_buf *ib = (struct input_buf *) malloc(sizeof(struct input_buf));
|
|
|
|
|
|
|
|
ib->carr = (int *) malloc(sizeof(int) * capacity);
|
|
|
|
ib->write_i = 0;
|
|
|
|
ib->read_i = 0;
|
|
|
|
ib->capacity = capacity;
|
|
|
|
|
|
|
|
return ib;
|
2023-08-24 12:57:25 +00:00
|
|
|
}
|
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
void ib_write(struct input_buf *ib, int key) {
|
|
|
|
if ((ib->write_i + 1) % (int) ib->capacity == ib->read_i) // Buffer full
|
|
|
|
die("ib_write");
|
2023-08-22 21:32:16 +00:00
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
ib->carr[ib->write_i] = key;
|
|
|
|
ib->write_i = (ib->write_i + 1) % ib->capacity;
|
2023-08-22 21:32:16 +00:00
|
|
|
}
|
2023-08-24 16:16:36 +00:00
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
int ib_read(struct input_buf *ib) {
|
|
|
|
if (ib_empty(ib)) die("ib_read");
|
2023-08-24 16:16:36 +00:00
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
int key = ib->carr[ib->read_i];
|
|
|
|
ib->read_i = (ib->read_i + 1) % ib->capacity;
|
2023-08-24 16:16:36 +00:00
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
return key;
|
2023-08-24 16:16:36 +00:00
|
|
|
}
|
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
bool ib_empty(struct input_buf *ib) {
|
|
|
|
return ib->write_i == ib->read_i;
|
2023-08-24 16:16:36 +00:00
|
|
|
}
|
|
|
|
|
2023-08-25 18:13:33 +00:00
|
|
|
void ib_free(struct input_buf *ib) {
|
|
|
|
free(ib->carr);
|
|
|
|
free(ib);
|
2023-08-24 16:16:36 +00:00
|
|
|
}
|
2023-08-25 18:13:33 +00:00
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
void die(const char *s) {
|
|
|
|
term_clear();
|
|
|
|
|
|
|
|
perror(s);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|