2023-08-22 21:32:16 +00:00
|
|
|
#include <errno.h>
|
2023-08-24 12:57:25 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdbool.h>
|
2023-08-22 21:32:16 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.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-23 23:27:25 +00:00
|
|
|
void enable_raw_mode();
|
|
|
|
void disable_raw_mode();
|
2023-08-24 12:57:25 +00:00
|
|
|
|
2023-08-24 11:09:03 +00:00
|
|
|
int get_window_size(int *, int *);
|
2023-08-24 12:57:25 +00:00
|
|
|
int read_cursor_position(int *, int *);
|
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();
|
|
|
|
void editor_clear_screen();
|
|
|
|
void editor_draw_rows();
|
|
|
|
void editor_process_key();
|
2023-08-24 12:57:25 +00:00
|
|
|
char editor_read_key();
|
2023-08-23 23:27:25 +00:00
|
|
|
|
2023-08-24 11:09:03 +00:00
|
|
|
void die(const char *);
|
2023-08-23 23:27:25 +00:00
|
|
|
|
2023-08-23 16:36:42 +00:00
|
|
|
#define CTRL_KEY(key) ((key) & 0x1f)
|
2023-08-22 21:32:16 +00:00
|
|
|
|
2023-08-24 11:09:03 +00:00
|
|
|
struct {
|
|
|
|
struct termios orig_termios;
|
|
|
|
int rows;
|
|
|
|
int cols;
|
2023-08-24 12:57:25 +00:00
|
|
|
bool raw;
|
|
|
|
} E = { .raw = false };
|
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-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);
|
|
|
|
}
|
|
|
|
|
|
|
|
enable_raw_mode();
|
|
|
|
|
|
|
|
if (get_window_size(&E.rows, &E.cols) == -1) die("get_window_size");
|
|
|
|
}
|
|
|
|
|
2023-08-22 21:32:16 +00:00
|
|
|
void enable_raw_mode() {
|
2023-08-24 12:57:25 +00:00
|
|
|
if (E.raw) return;
|
|
|
|
|
2023-08-24 11:09:03 +00:00
|
|
|
if (tcgetattr(STDIN_FILENO, &E.orig_termios) == -1) die("tcgetattr");
|
2023-08-22 21:32:16 +00:00
|
|
|
atexit(disable_raw_mode);
|
|
|
|
|
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-22 21:32:16 +00:00
|
|
|
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) die("tcsetattr");
|
2023-08-24 12:57:25 +00:00
|
|
|
|
|
|
|
E.raw = true;
|
2023-08-22 21:32:16 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 23:27:25 +00:00
|
|
|
void disable_raw_mode() {
|
2023-08-24 11:09:03 +00:00
|
|
|
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &E.orig_termios) == -1)
|
2023-08-23 23:27:25 +00:00
|
|
|
die("tcsetattr");
|
|
|
|
}
|
2023-08-22 21:32:16 +00:00
|
|
|
|
2023-08-24 11:09:03 +00:00
|
|
|
int get_window_size(int *rows, int *cols) {
|
|
|
|
struct winsize ws;
|
|
|
|
|
2023-08-24 12:57:25 +00:00
|
|
|
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1 && ws.ws_col != 0) {
|
2023-08-24 11:09:03 +00:00
|
|
|
*rows = ws.ws_row;
|
|
|
|
*cols = ws.ws_col;
|
|
|
|
|
|
|
|
return 0;
|
2023-08-24 12:57:25 +00:00
|
|
|
} else {
|
|
|
|
if (write(STDOUT_FILENO, "\x1b[999C\x1b[999B", 12) != 12) return -1;
|
|
|
|
return read_cursor_position(rows, cols);
|
2023-08-24 11:09:03 +00:00
|
|
|
}
|
2023-08-24 12:57:25 +00:00
|
|
|
|
2023-08-24 11:09:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-08-24 12:57:25 +00:00
|
|
|
int read_cursor_position(int *rows, int *cols) {
|
|
|
|
char buf[32];
|
|
|
|
unsigned int i = 0;
|
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;
|
|
|
|
|
|
|
|
if (read(STDIN_FILENO, buf, 1) != 1 && *buf != '\x1b') return -1;
|
|
|
|
if (read(STDIN_FILENO, buf, 1) != 1 && *buf != '[') return -1;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (i == sizeof(buf)) return -1;
|
|
|
|
if (read(STDIN_FILENO, buf+i, 1) == 0) break;
|
|
|
|
} while(buf[i++] != 'R');
|
|
|
|
buf[i-1] = '\0';
|
|
|
|
|
|
|
|
if (sscanf(buf, "%d;%d", rows, cols) != 2) return -1;
|
|
|
|
|
|
|
|
return 0;
|
2023-08-24 11:09:03 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 23:27:25 +00:00
|
|
|
void editor_redraw_screen() {
|
|
|
|
editor_clear_screen();
|
2023-08-22 21:32:16 +00:00
|
|
|
|
2023-08-23 23:27:25 +00:00
|
|
|
editor_draw_rows();
|
|
|
|
|
2023-08-24 12:57:25 +00:00
|
|
|
write(STDOUT_FILENO, "\x1b[H", 3);
|
2023-08-23 23:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void editor_clear_screen() {
|
2023-08-24 12:57:25 +00:00
|
|
|
write(STDOUT_FILENO, "\x1b[2J", 4);
|
|
|
|
write(STDOUT_FILENO, "\x1b[H", 3);
|
2023-08-23 16:36:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void editor_draw_rows() {
|
2023-08-24 11:09:03 +00:00
|
|
|
for (int y = 0; y < E.rows - 1; y++)
|
2023-08-24 12:57:25 +00:00
|
|
|
write(STDOUT_FILENO, "~\r\n", 3);
|
|
|
|
write(STDOUT_FILENO, "~", 1);
|
2023-08-23 16:36:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void editor_process_key() {
|
2023-08-24 12:57:25 +00:00
|
|
|
char c = editor_read_key();
|
2023-08-23 16:36:42 +00:00
|
|
|
switch (c) {
|
|
|
|
case CTRL_KEY('Q'):
|
|
|
|
editor_clear_screen();
|
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
default:
|
2023-08-22 21:32:16 +00:00
|
|
|
printf("%d\r\n", c);
|
2023-08-23 16:36:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-08-22 21:32:16 +00:00
|
|
|
|
2023-08-24 12:57:25 +00:00
|
|
|
char editor_read_key() {
|
|
|
|
char c;
|
|
|
|
while (read(STDIN_FILENO, &c, 1) == 0);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2023-08-23 23:27:25 +00:00
|
|
|
void die(const char *s) {
|
|
|
|
editor_clear_screen();
|
2023-08-22 21:32:16 +00:00
|
|
|
|
2023-08-23 23:27:25 +00:00
|
|
|
perror(s);
|
|
|
|
exit(1);
|
2023-08-22 21:32:16 +00:00
|
|
|
}
|