Archived
1
Fork 0
This repository has been archived on 2024-10-07. You can view files and clone it, but cannot push or open issues or pull requests.
kilo/kilo.c

584 lines
14 KiB
C
Raw Normal View History

2023-08-25 22:49:55 +00:00
#define _DEFAULT_SOURCE
2023-08-26 07:54:48 +00:00
#define KILO_VERSION "0.0.1"
#define KILO_TAB_STOP 4
2023-08-25 22:49:55 +00:00
2023-08-24 12:57:25 +00:00
#include <ctype.h>
#include <errno.h>
2023-08-26 04:47:07 +00:00
#include <limits.h>
2023-08-28 03:55:03 +00:00
#include <stdarg.h>
2023-08-24 12:57:25 +00:00
#include <stdbool.h>
2023-08-25 22:49:55 +00:00
#include <stdint.h>
2023-08-22 21:32:16 +00:00
#include <stdio.h>
#include <stdlib.h>
#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>
2023-08-28 03:55:03 +00:00
#include <time.h>
2023-08-22 21:32:16 +00:00
#include <unistd.h>
#define CTRL_KEY(key) ((key) & 0x1f)
2023-08-26 18:00:18 +00:00
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
2023-08-26 07:54:48 +00:00
/*****************************************************************************/
struct string_buf;
2023-08-25 22:49:55 +00:00
2023-08-26 18:00:18 +00:00
struct EROW {
2023-08-27 17:40:12 +00:00
char *chars;
int n_chars;
char *rchars;
int n_rchars;
2023-08-26 04:47:07 +00:00
};
2023-08-25 22:49:55 +00:00
struct {
2023-08-28 03:55:03 +00:00
char *filename;
2023-08-25 22:49:55 +00:00
int cx, cy;
int rx;
2023-08-26 04:47:07 +00:00
int screenrows, screencols;
2023-08-26 07:54:48 +00:00
int row_off, col_off;
2023-08-26 18:00:18 +00:00
bool running;
2023-08-26 07:54:48 +00:00
2023-08-26 18:00:18 +00:00
struct EROW *rows;
2023-08-26 07:54:48 +00:00
int n_rows;
2023-08-28 03:55:03 +00:00
char message[256];
time_t message_time;
2023-08-26 18:00:18 +00:00
struct termios orig_termios;
} E;
2023-08-26 04:47:07 +00:00
enum KEYS {
TAB = 9,
2023-08-26 04:47:07 +00:00
HOME = 0x100,
DEL,
PG_UP,
PG_DOWN,
END,
ARROW_UP,
ARROW_DOWN,
ARROW_LEFT,
ARROW_RIGHT,
NOP
};
2023-08-26 04:47:07 +00:00
typedef uint16_t KEY;
/*****************************************************************************/
2023-08-23 23:27:25 +00:00
2023-08-26 22:08:09 +00:00
void editor_init(void);
2023-08-28 03:55:03 +00:00
void editor_open(const char *);
void editor_append_row(const char *, int);
void editor_set_message(const char *, ...);
2023-08-27 17:40:12 +00:00
2023-08-26 22:08:09 +00:00
void editor_redraw_screen(void);
2023-08-28 03:55:03 +00:00
void editor_draw_rows(struct string_buf *);
void editor_draw_statusbar(struct string_buf *);
void editor_draw_messagebar(struct string_buf *);
2023-08-27 17:40:12 +00:00
void editor_process_key(void);
void editor_move_cursor(KEY);
void editor_adjust_viewport(void);
int editor_get_rx(int);
int editor_get_cx(int);
2023-08-25 22:49:55 +00:00
struct string_buf *sb_init(void);
char *sb_get_string(struct string_buf *);
int sb_get_size(struct string_buf *);
void sb_append(struct string_buf *, const char *, int);
void sb_free(struct string_buf *);
2023-08-23 23:27:25 +00:00
2023-08-26 22:08:09 +00:00
int term_enable_raw(void);
void term_disable_raw(void);
int term_clear(void);
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);
2023-08-26 22:08:09 +00:00
KEY term_read_key(void);
2023-08-24 11:09:03 +00:00
void die(const char *);
2023-08-23 23:27:25 +00:00
/*****************************************************************************/
2023-08-22 21:32:16 +00:00
2023-08-26 07:54:48 +00:00
int main(int argc, char **argv) {
2023-08-24 11:09:03 +00:00
editor_init();
2023-08-26 07:54:48 +00:00
if (argc >= 2)
editor_open(argv[1]);
2023-08-24 11:09:03 +00:00
2023-08-25 22:49:55 +00:00
while (E.running) {
2023-08-23 23:27:25 +00:00
editor_redraw_screen();
editor_process_key();
}
2023-08-22 21:32:16 +00:00
term_clear();
2023-08-23 23:27:25 +00:00
return 0;
2023-08-22 21:32:16 +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);
}
if (term_enable_raw() == -1) die("term_enable_raw");
2023-08-26 18:00:18 +00:00
if (term_get_win_size(&E.screenrows, &E.screencols) == -1)
die("term_get_win_size");
2023-08-28 03:55:03 +00:00
E.filename = NULL;
E.cx = E.cy = 0;
E.rx = 0;
2023-08-28 03:55:03 +00:00
E.screenrows -= 2;
2023-08-26 07:54:48 +00:00
E.row_off = E.col_off = 0;
2023-08-25 22:49:55 +00:00
E.running = true;
2023-08-28 03:55:03 +00:00
E.rows = NULL;
E.n_rows = 0;
editor_set_message("Welcome to kilo. Press CTRL-Q to quit.");
2023-08-27 17:40:12 +00:00
}
2023-08-28 03:55:03 +00:00
void editor_open(const char *filename) {
free(E.filename);
E.filename = strdup(filename);
FILE *file = fopen(E.filename, "r");
2023-08-26 07:54:48 +00:00
if (!file) die("fopen");
char *line = NULL;
size_t linecap = 0;
int linelen = -1;
while ((linelen = getline(&line, &linecap, file)) != -1) {
while (linelen > 0 && (line[linelen-1] == '\n' || line[linelen-1] == '\r'))
linelen--;
editor_append_row(line, linelen);
}
free(line);
fclose(file);
}
2023-08-28 03:55:03 +00:00
void editor_append_row(const char *s, int len) {
E.rows = realloc(E.rows, sizeof(struct EROW) * (E.n_rows + 1));
struct EROW *new_row = E.rows + E.n_rows;
new_row->chars = malloc(len);
new_row->n_chars = len;
struct string_buf *sb = sb_init();
for (int i = 0; i < len; i++) {
new_row->chars[i] = s[i];
switch (s[i]) {
case TAB: {
int tabs = KILO_TAB_STOP - (sb_get_size(sb) % KILO_TAB_STOP);
while (tabs--) sb_append(sb, " ", 1);
break;
}
default:
sb_append(sb, s+i, 1);
}
}
new_row->rchars = malloc(sb_get_size(sb));
new_row->n_rchars = sb_get_size(sb);
memcpy(new_row->rchars, sb_get_string(sb), sb_get_size(sb));
sb_free(sb);
E.n_rows++;
}
void editor_set_message(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vsnprintf(E.message, sizeof(E.message), fmt, ap);
va_end(ap);
E.message_time = time(NULL);
}
2023-08-27 17:40:12 +00:00
void editor_redraw_screen() {
2023-08-28 03:55:03 +00:00
if (term_cursor_hidden(true) == -1)
die("term_cursor_hidden");
struct string_buf *draw_buf = sb_init();
editor_draw_rows(draw_buf);
editor_draw_statusbar(draw_buf);
editor_draw_messagebar(draw_buf);
2023-08-28 03:55:03 +00:00
write(STDIN_FILENO, sb_get_string(draw_buf), sb_get_size(draw_buf));
sb_free(draw_buf);
2023-08-26 18:00:18 +00:00
int row_pos = E.cy - E.row_off + 1;
int col_pos = E.rx - E.col_off + 1;
2023-08-26 18:00:18 +00:00
if (term_set_cursor_pos(row_pos, col_pos) == -1)
die("term_set_cursor_pos");
if (term_cursor_hidden(false) == -1)
die("term_cursor_hidden");
}
2023-08-28 03:55:03 +00:00
void editor_draw_rows(struct string_buf *draw_buf) {
term_set_cursor_pos(1, 1);
2023-08-25 22:49:55 +00:00
for (int y = 0; y < E.screenrows; y++) {
2023-08-26 18:00:18 +00:00
bool in_file = y < E.n_rows - E.row_off;
bool no_file = E.n_rows == 0;
if (in_file) {
struct EROW curr_row = E.rows[y + E.row_off];
int max_len = MIN(curr_row.n_rchars - E.col_off, E.screencols);
2023-08-26 18:00:18 +00:00
sb_append(draw_buf, curr_row.rchars + E.col_off, MAX(max_len, 0));
2023-08-26 18:00:18 +00:00
} else if (no_file && y == E.screenrows / 2) {
2023-08-26 07:54:48 +00:00
char welcome[64];
2023-08-26 18:00:18 +00:00
int len = snprintf(welcome, sizeof(welcome),
"Welcome to kilo! -- v%s", KILO_VERSION);
2023-08-26 07:54:48 +00:00
int padding = (E.screencols - len) / 2;
2023-08-26 18:00:18 +00:00
for (int i = 0; i < padding; i++)
sb_append(draw_buf, (i == 0 ? "~" : " "), 1);
2023-08-26 07:54:48 +00:00
sb_append(draw_buf, welcome, len);
} else sb_append(draw_buf, "~", 1);
2023-08-26 07:54:48 +00:00
2023-08-28 03:55:03 +00:00
sb_append(draw_buf, "\x1b[K\r\n", 5);
}
2023-08-24 12:57:25 +00:00
}
2023-08-28 03:55:03 +00:00
void editor_draw_statusbar(struct string_buf *draw_buf) {
char *status_buf = malloc(E.screencols), buf[256];
int len;
2023-08-28 03:55:03 +00:00
memset(status_buf, ' ', E.screencols);
2023-08-28 03:55:03 +00:00
sb_append(draw_buf, "\x1b[7m", 4);
2023-08-27 17:40:12 +00:00
2023-08-28 03:55:03 +00:00
len = sprintf(buf, "%s -- %d lines",
(E.filename ? E.filename : "[NO NAME]"), E.n_rows);
memcpy(status_buf, buf, len);
2023-08-28 03:55:03 +00:00
len = sprintf(buf, "%d:%d", E.cy + 1, E.rx + 1);
memcpy(status_buf + E.screencols - len, buf, len);
2023-08-28 03:55:03 +00:00
sb_append(draw_buf, status_buf, E.screencols);
sb_append(draw_buf, "\x1b[m\r\n", 5);
2023-08-28 03:55:03 +00:00
free(status_buf);
}
void editor_draw_messagebar(struct string_buf *draw_buf) {
sb_append(draw_buf, "\x1b[K", 3);
int len = strlen(E.message);
if (len > E.screencols) len = E.screencols;
if (len && time(NULL) - E.message_time < 5)
sb_append(draw_buf, E.message, len);
2023-08-27 17:40:12 +00:00
}
void editor_process_key() {
KEY c = term_read_key();
2023-08-27 17:40:12 +00:00
switch (c) {
case ARROW_LEFT:
case ARROW_DOWN:
case ARROW_UP:
case ARROW_RIGHT:
case HOME:
case END:
case PG_UP:
case PG_DOWN:
editor_move_cursor(c);
break;
2023-08-25 22:49:55 +00:00
2023-08-27 17:40:12 +00:00
case CTRL_KEY('Q'):
E.running = false;
break;
}
2023-08-26 22:08:09 +00:00
}
2023-08-25 22:49:55 +00:00
2023-08-27 17:40:12 +00:00
void editor_move_cursor(KEY key) {
int max_x = (E.cy < E.n_rows ? E.rows[E.cy].n_chars : 0);
2023-08-27 17:40:12 +00:00
switch (key) {
case ARROW_LEFT:
if (E.cx > 0) E.cx--;
else if (E.cy > 0) E.cx = E.rows[--E.cy].n_chars;
2023-08-25 22:49:55 +00:00
break;
2023-08-27 17:40:12 +00:00
case ARROW_DOWN:
if (E.cy < E.n_rows) E.cy++;
break;
case ARROW_UP:
if (E.cy > 0) E.cy--;
break;
case ARROW_RIGHT:
if (E.cx < max_x) E.cx++;
2023-08-27 17:40:12 +00:00
else if (E.cy < E.n_rows) {
E.cx = 0;
E.cy++;
}
break;
case HOME:
E.cx = 0;
break;
case END:
E.cx = max_x;
2023-08-27 17:40:12 +00:00
break;
case PG_UP:
if ((E.cy -= E.screenrows) < 0)
E.cy = 0;
break;
case PG_DOWN:
if ((E.cy += E.screenrows) > E.n_rows)
E.cy = E.n_rows;
break;
}
2023-08-27 17:40:12 +00:00
static int saved_rx = 0;
bool horizontal = (key == ARROW_LEFT || key == ARROW_RIGHT ||
key == HOME || key == END);
if (horizontal) saved_rx = editor_get_rx(E.cx);
else E.cx = editor_get_cx(saved_rx);
max_x = (E.cy < E.n_rows ? E.rows[E.cy].n_chars : 0);
if (E.cx > max_x)
E.cx = max_x;
2023-08-27 17:40:12 +00:00
E.rx = editor_get_rx(E.cx);
editor_adjust_viewport();
2023-08-25 22:49:55 +00:00
}
void editor_adjust_viewport() {
int max_row_off = E.cy;
if (E.row_off > max_row_off)
E.row_off = max_row_off;
int max_col_off = E.rx;
if (E.col_off > max_col_off)
E.col_off = max_col_off;
int min_row_off = E.cy - (E.screenrows - 1);
if (E.row_off < min_row_off)
E.row_off = min_row_off;
int min_col_off = E.rx - (E.screencols - 1);
if (E.col_off < min_col_off)
E.col_off = min_col_off;
2023-08-25 22:49:55 +00:00
}
int editor_get_rx(int cx) {
if (E.cy >= E.n_rows) return 0;
int rx = 0;
struct EROW *row = E.rows+E.cy;
for (int i = 0; i < MIN(cx, row->n_chars); i++) {
if (row->chars[i] == TAB)
rx += KILO_TAB_STOP - (rx % KILO_TAB_STOP);
else rx++;
2023-08-26 18:00:18 +00:00
}
2023-08-25 22:49:55 +00:00
return rx;
2023-08-25 22:49:55 +00:00
}
int editor_get_cx(int rx_target) {
if (E.cy >= E.n_rows) return 0;
int cx = 0, rx = 0;
2023-08-26 18:00:18 +00:00
struct EROW *row = E.rows+E.cy;
while (rx < rx_target && cx < row->n_chars) {
if (row->chars[cx] == TAB) {
rx += KILO_TAB_STOP - (rx % KILO_TAB_STOP);
} else rx++;
2023-08-26 18:00:18 +00:00
cx++;
}
2023-08-25 22:49:55 +00:00
return (rx >= rx_target ? cx : row->n_chars);
2023-08-25 22:49:55 +00:00
}
/*****************************************************************************/
2023-08-25 22:49:55 +00:00
struct string_buf {
2023-08-28 03:55:03 +00:00
char *chars;
int n_chars;
2023-08-25 22:49:55 +00:00
};
struct string_buf *sb_init() {
size_t buf_size = sizeof(struct string_buf);
struct string_buf *sb = (struct string_buf *) malloc(buf_size);
2023-08-28 03:55:03 +00:00
sb->chars = NULL;
sb->n_chars = 0;
2023-08-25 22:49:55 +00:00
return sb;
2023-08-25 22:49:55 +00:00
}
char *sb_get_string(struct string_buf *sb) {
2023-08-28 03:55:03 +00:00
return sb->chars;
2023-08-25 22:49:55 +00:00
}
int sb_get_size(struct string_buf *sb) {
2023-08-28 03:55:03 +00:00
return sb->n_chars;
}
2023-08-25 22:49:55 +00:00
2023-08-28 03:55:03 +00:00
void sb_append(struct string_buf *sb, const char *chars, int n_chars) {
sb->chars = realloc(sb->chars, sb->n_chars + n_chars);
2023-08-25 22:49:55 +00:00
2023-08-28 03:55:03 +00:00
memcpy(sb->chars + sb->n_chars, chars, n_chars);
sb->n_chars += n_chars;
2023-08-25 22:49:55 +00:00
}
void sb_free(struct string_buf *sb) {
2023-08-28 03:55:03 +00:00
free(sb->chars);
free(sb);
}
/*****************************************************************************/
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;
2023-08-25 22:49:55 +00:00
raw.c_cc[VTIME] = 10;
2023-08-24 12:57:25 +00:00
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) return -1;
return 0;
2023-08-22 21:32:16 +00:00
}
void term_disable_raw() {
2023-08-24 11:09:03 +00:00
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &E.orig_termios) == -1)
die ("term_disable_raw");
2023-08-23 23:27:25 +00:00
}
2023-08-22 21:32:16 +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) {
*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;
return term_get_cursor_pos(row, col);
2023-08-24 11:09:03 +00:00
}
}
int term_get_cursor_pos(int *row, int *col) {
2023-08-28 03:55:03 +00:00
char buf[16];
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-26 04:47:07 +00:00
for (int i = 0; i < (int) 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
if (sscanf(buf, "\x1b[%d;%d", row, col) == EOF)
return -1;
2023-08-24 12:57:25 +00:00
return 0;
2023-08-24 11:09:03 +00:00
}
int term_set_cursor_pos(int row, int col) {
char buf[16];
2023-08-22 21:32:16 +00:00
2023-08-26 04:47:07 +00:00
int len = snprintf(buf, sizeof(buf), "\x1b[%d;%dH", row, col);
len = (len >= (int) sizeof(buf) ? (int) sizeof(buf) - 1 : len);
2023-08-23 23:27:25 +00:00
2023-08-26 04:47:07 +00:00
if ((int) write(STDOUT_FILENO, buf, len) != len) return -1;
return 0;
}
2023-08-25 22:49:55 +00:00
KEY term_read_key() {
char c;
while (read(STDIN_FILENO, &c, 1) == 0);
2023-08-25 22:49:55 +00:00
if (c == '\x1b') {
char buf[8] = { '\0' };
2023-08-26 04:47:07 +00:00
for (int i = 0; i < (int) sizeof(buf); i++) {
2023-08-25 22:49:55 +00:00
if (read(STDIN_FILENO, buf+i, 1) == 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;
}
}
2023-08-25 22:49:55 +00:00
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;
}
}
}
2023-08-25 22:49:55 +00:00
return NOP;
}
2023-08-25 22:49:55 +00:00
return (KEY) c;
}
/*****************************************************************************/
void die(const char *s) {
term_clear();
perror(s);
exit(1);
}