kst/ke/main.c

651 lines
10 KiB
C
Raw Normal View History

2020-02-10 06:58:10 +00:00
/*
* kyle's editor
*
* first version is a run-through of the kilo editor walkthrough.
* https://viewsourcecode.org/snaptoken/kilo/
*/
2020-02-08 08:40:21 +00:00
#include <sys/ioctl.h>
#include <ctype.h>
2020-02-08 07:07:59 +00:00
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
2020-02-08 08:40:21 +00:00
#include <string.h>
#include <termios.h>
#include <unistd.h>
2020-02-08 10:22:31 +00:00
#define KE_VERSION "0.0.1-pre"
2020-02-08 08:40:21 +00:00
#define ESCSEQ "\x1b["
#define CTRL_KEY(key) ((key)&0x1f)
2020-02-10 15:43:36 +00:00
#define TAB_STOP 8
2020-02-08 08:40:21 +00:00
/*
* define the keyboard input modes
* normal: no special mode
* kcommand: ^k commands
*/
#define MODE_NORMAL 0
#define MODE_KCOMMAND 1
2020-02-08 10:22:31 +00:00
#define MODE_INSERT 2
2020-02-08 08:40:21 +00:00
2020-02-08 10:22:31 +00:00
enum KeyPress {
ARROW_UP = 1000,
ARROW_DOWN,
ARROW_LEFT,
ARROW_RIGHT,
2020-02-08 10:30:32 +00:00
DEL_KEY,
END_KEY,
HOME_KEY,
2020-02-08 10:22:31 +00:00
PG_UP,
PG_DN,
};
void disable_termraw();
2020-02-09 06:23:32 +00:00
typedef struct erow {
char *line;
2020-02-10 06:58:10 +00:00
char *render;
2020-02-09 06:23:32 +00:00
int size;
2020-02-10 06:58:10 +00:00
int rsize;
2020-02-09 06:23:32 +00:00
} erow;
2020-02-08 08:40:21 +00:00
struct {
2020-02-09 06:23:32 +00:00
struct termios entry_term;
int rows, cols;
int curx, cury;
2020-02-10 15:43:36 +00:00
int rx;
2020-02-09 06:23:32 +00:00
int mode;
int nrows;
2020-02-10 06:58:10 +00:00
int rowoffs, coloffs;
2020-02-09 06:23:32 +00:00
erow *row;
2020-02-08 08:40:21 +00:00
} editor;
/* append buffer */
struct abuf {
char *b;
int len;
};
#define ABUF_INIT {NULL, 0}
void
ab_append(struct abuf *buf, const char *s, int len)
{
char *nc = realloc(buf->b, buf->len + len);
if (nc == NULL) {
return;
}
memcpy(&nc[buf->len], s, len);
buf->b = nc;
buf->len += len; /* DANGER: overflow */
}
void
ab_free(struct abuf *buf)
{
free(buf->b);
}
void
die(const char *s)
{
/*
* NOTE(kyle): this is a duplication of the code in display.c
* but I would like to be able to import these files from there.
*/
write(STDOUT_FILENO, "\x1b[2J", 4);
write(STDOUT_FILENO, "\x1b[H", 3);
perror(s);
exit(1);
}
/*
* get_winsz uses the TIOCGWINSZ to get the window size.
*
* there's a fallback way to do this, too, that involves moving the
* cursor down and to the left \x1b[999C\x1b[999B. I'm going to skip
* on this for now because it's bloaty and this works on OpenBSD and
* Linux, at least.
*/
int
get_winsz(int *rows, int *cols)
{
struct winsize ws;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 ||
ws.ws_col == 0) {
return -1;
}
*cols = ws.ws_col;
*rows = ws.ws_row;
return 0;
}
2020-02-10 15:43:36 +00:00
void
update_row(erow *row)
{
int i = 0, j;
int tabs = 0;
/*
* TODO(kyle): I'm not thrilled with this double-render.
*/
for (j = 0; j < row->size; j++) {
if (row->line[j] == '\t') {
tabs++;
}
}
free(row->render);
row->render = malloc(row->size + (tabs * (TAB_STOP-1)) + 1);
for (j = 0; j < row->size; j++) {
if (row->line[j] == '\t') {
do {
row->render[i++] = ' ';
} while ((i % TAB_STOP) != 0);
} else {
row->render[i++] = row->line[j];
}
}
row->render[i] = '\0';
row->rsize = i;
}
2020-02-09 06:23:32 +00:00
void
append_row(char *s, size_t len)
{
int at = editor.nrows;
editor.row = realloc(editor.row, sizeof(erow) * (editor.nrows + 1));
editor.row[at].size = len;
editor.row[at].line = malloc(len+1);
memcpy(editor.row[at].line, s, len);
editor.row[at].line[len] = '\0';
2020-02-10 06:58:10 +00:00
editor.row[at].rsize = 0;
editor.row[at].render = NULL;
2020-02-10 15:43:36 +00:00
update_row(&editor.row[at]);
2020-02-09 06:23:32 +00:00
editor.nrows++;
}
void
open_file(const char *filename)
{
char *line = NULL;
size_t linecap = 0;
ssize_t linelen;
FILE *fp = fopen(filename, "r");
if (!fp) {
die("fopen");
}
while ((linelen = getline(&line, &linecap, fp)) != -1) {
if (linelen != -1) {
while ((linelen > 0) && ((line[linelen-1] == '\r') ||
(line[linelen-1] == '\n'))) {
linelen--;
}
append_row(line, linelen);
}
}
free(line);
fclose(fp);
}
2020-02-08 10:22:31 +00:00
uint16_t
is_arrow_key(int16_t c)
{
switch (c) {
case ARROW_UP:
case ARROW_DOWN:
case ARROW_LEFT:
case ARROW_RIGHT:
case CTRL_KEY('p'):
case CTRL_KEY('n'):
case CTRL_KEY('f'):
case CTRL_KEY('b'):
case PG_UP:
case PG_DN:
2020-02-08 10:30:32 +00:00
case HOME_KEY:
case END_KEY:
2020-02-08 10:22:31 +00:00
return 1;
};
return 0;
}
int16_t
2020-02-08 08:40:21 +00:00
get_keypress()
{
2020-02-08 10:22:31 +00:00
char seq[3];
char c = -1;
2020-02-08 08:40:21 +00:00
if (read(STDIN_FILENO, &c, 1) == -1) {
die("get_keypress:read");
}
2020-02-08 10:22:31 +00:00
if (c == 0x1b) {
if (read(STDIN_FILENO, &seq[0], 1) != 1) return c;
if (read(STDIN_FILENO, &seq[1], 1) != 1) return c;
if (seq[0] == '[') {
if (seq[1] < 'A') {
if (read(STDIN_FILENO, &seq[2], 1) != 1)
return c;
if (seq[2] == '~') {
switch (seq[1]) {
2020-02-08 10:30:32 +00:00
case '1': return HOME_KEY;
case '2': return END_KEY;
case '3': return DEL_KEY;
2020-02-08 10:22:31 +00:00
case '5': return PG_UP;
case '6': return PG_DN;
2020-02-08 10:30:32 +00:00
case '7': return HOME_KEY;
case '8': return END_KEY;
2020-02-08 10:22:31 +00:00
}
}
} else {
switch (seq[1]) {
case 'A': return ARROW_UP;
case 'B': return ARROW_DOWN;
case 'C': return ARROW_RIGHT;
case 'D': return ARROW_LEFT;
2020-02-08 10:30:32 +00:00
case 'F': return END_KEY;
case 'H': return HOME_KEY;
2020-02-08 10:22:31 +00:00
default:
/* nada */ ;
}
}
2020-02-08 10:30:32 +00:00
} else if (seq[0] == 'O') {
switch (seq[1]) {
case 'F': return END_KEY;
case 'H': return HOME_KEY;
}
2020-02-08 10:22:31 +00:00
}
return 0x1b;
}
2020-02-08 08:40:21 +00:00
return c;
}
2020-02-08 10:22:31 +00:00
void
move_cursor(int16_t c)
{
2020-02-10 06:58:10 +00:00
erow *row = (editor.cury >= editor.nrows) ?
NULL : &editor.row[editor.cury];
int reps;
2020-02-08 10:22:31 +00:00
switch (c) {
case ARROW_UP:
case CTRL_KEY('p'):
2020-02-10 06:58:10 +00:00
if (editor.cury > 0) {
editor.cury--;
}
2020-02-08 10:22:31 +00:00
break;
case ARROW_DOWN:
case CTRL_KEY('n'):
2020-02-10 06:58:10 +00:00
if (editor.cury < editor.nrows) {
editor.cury++;
}
2020-02-08 10:22:31 +00:00
break;
case ARROW_RIGHT:
case CTRL_KEY('f'):
2020-02-10 06:58:10 +00:00
if (row && editor.curx < row->size) {
editor.curx++;
} else if (row && editor.curx == row->size) {
editor.cury++;
editor.curx = 0;
}
2020-02-08 10:22:31 +00:00
break;
case ARROW_LEFT:
case CTRL_KEY('b'):
2020-02-10 06:58:10 +00:00
if (editor.curx > 0) {
editor.curx--;
} else if (editor.cury > 0) {
editor.cury--;
editor.curx = editor.row[editor.cury].size;
}
2020-02-08 10:22:31 +00:00
break;
case PG_UP:
case PG_DN: {
2020-02-10 15:43:36 +00:00
if (c == PG_UP) {
editor.cury = editor.rowoffs;
} else if (c == PG_DN) {
editor.cury = editor.rowoffs + editor.rows-1;
if (editor.cury > editor.nrows) {
editor.cury = editor.nrows;
}
}
reps = editor.rows;
2020-02-08 10:22:31 +00:00
while (--reps) {
move_cursor(c == PG_UP ? ARROW_UP : ARROW_DOWN);
2020-02-10 06:58:10 +00:00
}
2020-02-08 10:22:31 +00:00
}
2020-02-08 10:30:32 +00:00
case HOME_KEY:
editor.curx = 0;
break;
case END_KEY:
editor.curx = editor.cols - 1;
break;
2020-02-08 10:22:31 +00:00
default:
break;
}
2020-02-10 06:58:10 +00:00
row = (editor.cury >= editor.nrows) ?
NULL : &editor.row[editor.cury];
reps = row ? row->size : 0;
if (editor.curx > reps) {
editor.curx = reps;
}
2020-02-08 10:22:31 +00:00
}
void
process_kcommand(int16_t c)
{
switch (c) {
case 'q':
case CTRL_KEY('q'):
exit(0);
case 'd':
case CTRL_KEY('\\'):
/* sometimes it's nice to dump core */
disable_termraw();
abort();
}
editor.mode = MODE_NORMAL;
return;
}
2020-02-08 08:40:21 +00:00
void
process_keypress()
{
2020-02-08 10:22:31 +00:00
int16_t c = get_keypress();
2020-02-08 08:40:21 +00:00
2020-02-08 10:22:31 +00:00
/* we didn't actually read a key */
if (c <= 0) {
2020-02-08 08:40:21 +00:00
return;
}
2020-02-08 10:22:31 +00:00
switch (editor.mode) {
2020-02-08 08:40:21 +00:00
case MODE_KCOMMAND:
2020-02-08 10:22:31 +00:00
process_kcommand(c);
break;
case MODE_NORMAL:
if (is_arrow_key(c)) {
move_cursor(c);
2020-02-08 08:40:21 +00:00
}
}
if (c == CTRL_KEY('k')) {
2020-02-08 10:22:31 +00:00
editor.mode = MODE_KCOMMAND;
2020-02-08 08:40:21 +00:00
return;
}
}
/*
* A text editor needs the terminal to be in raw mode; but the default
* is to be in canonical (cooked) mode, which is a buffered input mode.
*/
static void
enable_termraw()
{
struct termios raw;
/* Read the current terminal parameters for standard input. */
if (tcgetattr(STDIN_FILENO, &raw) == -1) {
die("tcgetattr while enabling raw mode");
}
/*
* Put the terminal into raw mode.
*/
cfmakeraw(&raw);
/*
* Set timeout for read(2).
*
* VMIN: what is the minimum number of bytes required for read
* to return?
*
* VTIME: max time before read(2) returns in hundreds of milli-
* seconds.
*/
raw.c_cc[VMIN] = 0;
raw.c_cc[VTIME] = 1;
/*
* Now write the terminal parameters to the current terminal,
* after flushing any waiting input out.
*/
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) {
die("tcsetattr while enabling raw mode");
}
}
2020-02-08 10:22:31 +00:00
void
display_clear(struct abuf *ab)
{
if (ab == NULL) {
write(STDOUT_FILENO, ESCSEQ "2J", 4);
write(STDOUT_FILENO, ESCSEQ "H", 3);
} else {
ab_append(ab, ESCSEQ "2J", 4);
ab_append(ab, ESCSEQ "H", 3);
}
}
void
2020-02-08 08:40:21 +00:00
disable_termraw()
{
display_clear(NULL);
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &editor.entry_term) == -1) {
die("couldn't disable terminal raw mode");
}
}
void
setup_terminal()
{
if (tcgetattr(STDIN_FILENO, &editor.entry_term) == -1) {
die("can't snapshot terminal settings");
}
atexit(disable_termraw);
enable_termraw();
}
void
draw_rows(struct abuf *ab)
{
2020-02-08 10:22:31 +00:00
char buf[editor.cols];
2020-02-10 06:58:10 +00:00
int buflen, filerow, padding;
2020-02-08 08:40:21 +00:00
int y;
2020-02-09 06:23:32 +00:00
for (y = 0; y < editor.rows; y++) {
2020-02-10 06:58:10 +00:00
filerow = y + editor.rowoffs;
if (filerow >= editor.nrows) {
2020-02-09 06:23:32 +00:00
if ((editor.nrows == 0) && (y == editor.rows / 3)) {
buflen = snprintf(buf, sizeof(buf),
"ke k%s", KE_VERSION);
padding = (editor.rows - buflen) / 2;
if (padding) {
ab_append(ab, "|", 1);
padding--;
}
2020-02-08 10:22:31 +00:00
2020-02-09 06:23:32 +00:00
while (padding--) ab_append(ab, " ", 1);
ab_append(ab, buf, buflen);
} else {
ab_append(ab, "|", 1);
}
} else {
2020-02-10 15:43:36 +00:00
buflen = editor.row[filerow].rsize - editor.coloffs;
2020-02-10 06:58:10 +00:00
if (buflen < 0) {
buflen = 0;
}
if (buflen > editor.cols) {
buflen = editor.cols;
2020-02-09 06:23:32 +00:00
}
2020-02-10 15:43:36 +00:00
ab_append(ab, editor.row[filerow].render+editor.coloffs,
2020-02-10 06:58:10 +00:00
buflen);
2020-02-09 06:23:32 +00:00
}
ab_append(ab, ESCSEQ "K", 3);
if (y < (editor.rows - 1)) {
ab_append(ab, "\r\n", 2);
}
}
2020-02-08 08:40:21 +00:00
}
2020-02-10 15:43:36 +00:00
int
row_render_to_cursor(erow *row, int cx)
{
int rx = 0;
int j;
for (j = 0; j < cx; j++) {
if (row->line[j] == '\t') {
rx += (TAB_STOP-1) - (rx%TAB_STOP);
}
rx++;
}
return rx;
}
2020-02-10 06:58:10 +00:00
void
scroll()
{
2020-02-10 15:43:36 +00:00
editor.rx = 0;
if (editor.cury < editor.nrows) {
editor.rx = row_render_to_cursor(
&editor.row[editor.cury],
editor.curx);
}
2020-02-10 06:58:10 +00:00
if (editor.cury < editor.rowoffs) {
editor.rowoffs = editor.cury;
}
if (editor.cury >= editor.rowoffs + editor.rows) {
editor.rowoffs = editor.cury - editor.rows + 1;
}
2020-02-10 15:43:36 +00:00
if (editor.rx < editor.coloffs) {
editor.coloffs = editor.rx;
2020-02-10 06:58:10 +00:00
}
2020-02-10 15:43:36 +00:00
if (editor.rx >= editor.coloffs + editor.cols) {
editor.coloffs = editor.rx - editor.cols + 1;
2020-02-10 06:58:10 +00:00
}
}
2020-02-08 08:40:21 +00:00
void
display_refresh()
{
2020-02-08 10:22:31 +00:00
char buf[32];
2020-02-08 08:40:21 +00:00
struct abuf ab = ABUF_INIT;
2020-02-10 06:58:10 +00:00
scroll();
2020-02-08 08:40:21 +00:00
ab_append(&ab, ESCSEQ "?25l", 6);
display_clear(&ab);
2020-02-08 10:22:31 +00:00
2020-02-08 08:40:21 +00:00
draw_rows(&ab);
2020-02-08 10:22:31 +00:00
2020-02-10 06:58:10 +00:00
snprintf(buf, sizeof(buf), ESCSEQ "%d;%dH",
(editor.cury-editor.rowoffs)+1,
2020-02-10 15:43:36 +00:00
(editor.rx-editor.coloffs)+1);
2020-02-08 10:22:31 +00:00
ab_append(&ab, buf, strnlen(buf, 32));
2020-02-09 06:23:32 +00:00
/* ab_append(&ab, ESCSEQ "1;2H", 7); */
2020-02-08 08:40:21 +00:00
ab_append(&ab, ESCSEQ "?25h", 6);
write(STDOUT_FILENO, ab.b, ab.len);
ab_free(&ab);
}
2020-02-08 07:07:59 +00:00
void
loop()
{
2020-02-08 07:07:59 +00:00
while (1) {
2020-02-08 08:40:21 +00:00
display_refresh();
process_keypress();
2020-02-08 07:07:59 +00:00
}
}
2020-02-08 08:40:21 +00:00
/*
* init_editor should set up the global editor struct.
*/
void
init_editor()
{
if (get_winsz(&editor.rows, &editor.cols) == -1) {
die("can't get window size");
}
2020-02-08 10:22:31 +00:00
2020-02-10 06:58:10 +00:00
editor.curx = editor.cury = 0;
2020-02-10 15:43:36 +00:00
editor.rx = 0;
2020-02-09 06:23:32 +00:00
editor.nrows = 0;
2020-02-10 06:58:10 +00:00
editor.rowoffs = editor.coloffs = 0;
2020-02-09 06:23:32 +00:00
editor.row = NULL;
2020-02-08 08:40:21 +00:00
}
int
2020-02-09 06:23:32 +00:00
main(int argc, char *argv[])
{
2020-02-08 07:07:59 +00:00
setup_terminal();
2020-02-08 08:40:21 +00:00
init_editor();
2020-02-09 06:23:32 +00:00
if (argc > 1) {
open_file(argv[1]);
}
2020-02-08 08:40:21 +00:00
display_clear(NULL);
2020-02-08 07:07:59 +00:00
loop();
return 0;
}