15 Commits

Author SHA1 Message Date
d9c5a6696e Fix ESC+BKSP.
Some checks failed
Release / Bump Homebrew formula (push) Has been cancelled
2025-11-27 14:04:55 -08:00
5c2571eba7 C-l should update window size.
Noticed this when splitting the screen in tmux.
2025-11-27 14:00:07 -08:00
9afd030b87 fix jump bug 2025-11-27 13:15:10 -08:00
2f198e611e less-compatible paging 2025-11-27 13:08:41 -08:00
e079726ced fix check for jump
Some checks failed
Release / Bump Homebrew formula (push) Has been cancelled
2025-11-27 13:02:18 -08:00
3c79e368fa Forgot to bump version in CMakeLists.
Some checks failed
Release / Bump Homebrew formula (push) Has been cancelled
2025-11-27 12:51:53 -08:00
bd68b809cd various qol improvements
Some checks failed
Release / Bump Homebrew formula (push) Has been cancelled
+ C-u works with C-f and C-b (and their delete counterparts).
+ Opening files now allows adding a line number after to jump to.
2025-11-27 12:48:10 -08:00
f200a7bfcd rename editor type. 2025-11-26 18:39:06 -08:00
58daeac6ad typedef struct abuf -> abuf 2025-11-26 18:38:32 -08:00
3ef6bab265 typedef struct erow -> erow 2025-11-26 18:35:51 -08:00
3800751bcf move var decl to top of function. 2025-11-26 17:57:59 -08:00
87272a669b remove debug log
just redirect stderr to a logfile like a normal person
2025-11-26 17:09:34 -08:00
b77748f7c0 add first post about ke to the readme 2025-11-26 16:47:58 -08:00
c70b502006 display message if KIONREAD ioctl fails. 2025-11-26 16:16:10 -08:00
e14d620887 remove comments, not needed 2025-11-26 16:14:25 -08:00
4 changed files with 183 additions and 111 deletions

View File

@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.15)
project(ke C) # Specify C language explicitly
set(CMAKE_C_STANDARD 99)
set(KE_VERSION "1.5.2")
set(KE_VERSION "1.5.6")
set(CMAKE_C_FLAGS "-Wall -Wextra -pedantic -Wshadow -Werror -std=c99 -g")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_DEFAULT_SOURCE -D_XOPEN_SOURCE")

View File

@@ -19,3 +19,6 @@ Released under an ISC license.
Started by following along with kilo:
https://viewsourcecode.org/snaptoken/kilo/
E.g., in the devlogs
https://log.wntrmute.dev/2020/02/20200207

View File

@@ -8,9 +8,7 @@
let
cmakeContent = builtins.readFile ./CMakeLists.txt;
cmakeLines = lib.splitString "\n" cmakeContent;
# Find the line containing set(KE_VERSION "...")
versionLine = lib.findFirst (l: builtins.match ".*set\\(KE_VERSION \".+\"\\).*" l != null) (throw "KE_VERSION not found in CMakeLists.txt") cmakeLines;
# Extract the version number
version = builtins.head (builtins.match ".*set\\(KE_VERSION \"(.+)\"\\).*" versionLine);
in
stdenv.mkDerivation {

259
main.c
View File

@@ -73,21 +73,19 @@
#define calloc1(sz) calloc(1, sz)
static FILE* debug_log = NULL;
/* append buffer */
struct abuf {
typedef struct abuf {
char *b;
size_t len;
size_t cap;
};
} abuf;
#define ABUF_INIT {NULL, 0, 0}
/* editor row */
struct erow {
typedef struct erow {
char *line;
char *render;
@@ -96,14 +94,38 @@ struct erow {
int cap;
int dirty;
};
} erow;
typedef enum undo_kind {
UNDO_INSERT = 1 << 0,
UNDO_UNKNOWN = 1 << 1,
} undo_kind;
typedef struct undo_node {
undo_kind op;
size_t row, col;
abuf text;
struct undo_node *next;
struct undo_node *parent;
} undo_node;
typedef struct undo_tree {
undo_node *root; /* the start of the undo sequence */
undo_node *current; /* where we are currently at */
undo_node *pending; /* the current undo operations being built */
} undo_tree;
/*
* editor is the global editor state; it should be broken out
* to buffers and screen state, probably.
*/
struct editor_t {
struct editor {
struct termios entry_term;
int rows, cols;
int curx, cury;
@@ -111,8 +133,8 @@ struct editor_t {
int mode;
int nrows;
int rowoffs, coloffs;
struct erow *row;
struct erow *killring;
erow *row;
erow *killring;
int kill; /* KILL CHAIN (sounds metal) */
int no_kill; /* don't kill in delete_row */
char *filename;
@@ -154,22 +176,22 @@ void reset_editor(void);
int next_power_of_2(int n);
int cap_growth(int cap, int sz);
size_t kstrnlen(const char *buf, const size_t max);
void ab_init(struct abuf *buf);
void ab_appendch(struct abuf *buf, char c);
void ab_append(struct abuf *buf, const char *s, size_t len);
void ab_prependch(struct abuf *buf, char c);
void ab_prepend(struct abuf *buf, const char *s, size_t len);
void ab_free(struct abuf *buf);
void ab_init(abuf *buf);
void ab_appendch(abuf *buf, char c);
void ab_append(abuf *buf, const char *s, size_t len);
void ab_prependch(abuf *buf, char c);
void ab_prepend(abuf *buf, const char *s, size_t len);
void ab_free(abuf *buf);
char nibble_to_hex(char c);
void swap_int(int *a, int *b);
/* editor rows */
int erow_render_to_cursor(struct erow *row, int cx);
int erow_cursor_to_render(struct erow *row, int rx);
int erow_init(struct erow *row, int len);
void erow_update(struct erow *row);
int erow_render_to_cursor(erow *row, int cx);
int erow_cursor_to_render(erow *row, int rx);
int erow_init(erow *row, int len);
void erow_update(erow *row);
void erow_insert(int at, char *s, int len);
void erow_free(struct erow *row);
void erow_free(erow *row);
/* kill ring, marking, etc... */
@@ -198,8 +220,8 @@ void delete_next_word(void);
void find_prev_word(void);
void delete_prev_word(void);
void delete_row(int at);
void row_insert_ch(struct erow *row, int at, int16_t c);
void row_delete_ch(struct erow *row, int at);
void row_insert_ch(erow *row, int at, int16_t c);
void row_delete_ch(erow *row, int at);
void insertch(int16_t c);
void deletech(uint8_t op);
void open_file(const char *filename);
@@ -212,7 +234,7 @@ void editor_find_callback(char *query, int16_t c);
void editor_find(void);
char *editor_prompt(char*, void (*cb)(char*, int16_t));
void editor_openfile(void);
int first_nonwhitespace(struct erow *row);
int first_nonwhitespace(erow *row);
void move_cursor_once(int16_t c, int interactive);
void move_cursor(int16_t c, int interactive);
void uarg_start(void);
@@ -227,18 +249,18 @@ int process_keypress(void);
char *get_cloc_code_lines(const char *filename);
int dump_pidfile(void);
void enable_termraw(void);
void display_clear(struct abuf *ab);
void display_clear(abuf *ab);
void disable_termraw(void);
void setup_terminal(void);
void draw_rows(struct abuf *ab);
void draw_rows(abuf *ab);
char status_mode_char(void);
void draw_status_bar(struct abuf *ab);
void draw_message_line(struct abuf *ab);
void draw_status_bar(abuf *ab);
void draw_message_line(abuf *ab);
void scroll(void);
void display_refresh(void);
void editor_set_status(const char *fmt, ...);
void loop(void);
void enable_debugging(const char *logfile);
void enable_debugging(void);
void deathknell(void);
static void signal_handler(int sig);
static void install_signal_handlers(void);
@@ -393,7 +415,7 @@ reset_editor(void)
void
ab_init(struct abuf *buf)
ab_init(abuf *buf)
{
buf->b = NULL;
buf->len = 0;
@@ -402,14 +424,14 @@ ab_init(struct abuf *buf)
void
ab_appendch(struct abuf *buf, char c)
ab_appendch(abuf *buf, char c)
{
ab_append(buf, &c, 1);
}
void
ab_append(struct abuf *buf, const char *s, size_t len)
ab_append(abuf *buf, const char *s, size_t len)
{
char *nc = buf->b;
size_t sz = buf->len + len;
@@ -433,14 +455,14 @@ ab_append(struct abuf *buf, const char *s, size_t len)
void
ab_prependch(struct abuf *buf, const char c)
ab_prependch(abuf *buf, const char c)
{
ab_prepend(buf, &c, 1);
}
void
ab_prepend(struct abuf *buf, const char *s, const size_t len)
ab_prepend(abuf *buf, const char *s, const size_t len)
{
char *nc = realloc(buf->b, buf->len + len);
assert(nc != NULL);
@@ -454,7 +476,7 @@ ab_prepend(struct abuf *buf, const char *s, const size_t len)
void
ab_free(struct abuf *buf)
ab_free(abuf *buf)
{
free(buf->b);
buf->b = NULL;
@@ -484,7 +506,7 @@ swap_int(int *a, int *b)
int
erow_render_to_cursor(struct erow *row, int cx)
erow_render_to_cursor(erow *row, int cx)
{
int rx = 0;
size_t j = 0;
@@ -545,7 +567,7 @@ erow_render_to_cursor(struct erow *row, int cx)
int
erow_cursor_to_render(struct erow *row, int rx)
erow_cursor_to_render(erow *row, int rx)
{
int cur_rx = 0;
size_t j = 0;
@@ -604,7 +626,7 @@ erow_cursor_to_render(struct erow *row, int rx)
int
erow_init(struct erow *row, int len)
erow_init(erow *row, int len)
{
row->size = len;
row->rsize = 0;
@@ -625,7 +647,7 @@ erow_init(struct erow *row, int len)
void
erow_update(struct erow *row)
erow_update(erow *row)
{
int i = 0, j;
int tabs = 0;
@@ -675,7 +697,7 @@ erow_update(struct erow *row)
void
erow_insert(int at, char *s, int len)
{
struct erow row;
erow row;
if (at < 0 || at > editor.nrows) {
return;
@@ -686,13 +708,13 @@ erow_insert(int at, char *s, int len)
row.line[len] = 0;
editor.row = realloc(editor.row,
sizeof(struct erow) * (editor.nrows + 1));
sizeof(erow) * (editor.nrows + 1));
assert(editor.row != NULL);
if (at < editor.nrows) {
memmove(&editor.row[at + 1],
&editor.row[at],
sizeof(struct erow) * (editor.nrows - at));
sizeof(erow) * (editor.nrows - at));
}
editor.row[at] = row;
@@ -702,7 +724,7 @@ erow_insert(int at, char *s, int len)
void
erow_free(struct erow *row)
erow_free(erow *row)
{
free(row->render);
free(row->line);
@@ -747,7 +769,7 @@ killring_yank(void)
void
killring_start_with_char(unsigned char ch)
{
struct erow *row = NULL;
erow *row = NULL;
if (editor.killring != NULL) {
erow_free(editor.killring);
@@ -755,7 +777,7 @@ killring_start_with_char(unsigned char ch)
editor.killring = NULL;
}
editor.killring = malloc(sizeof(struct erow));
editor.killring = malloc(sizeof(erow));
assert(editor.killring != NULL);
assert(erow_init(editor.killring, 0) == 0);
@@ -774,7 +796,7 @@ killring_start_with_char(unsigned char ch)
void
killring_append_char(unsigned char ch)
{
struct erow *row = NULL;
erow *row = NULL;
if (editor.killring == NULL) {
killring_start_with_char(ch);
@@ -799,7 +821,7 @@ killring_prepend_char(unsigned char ch)
return;
}
struct erow *row = editor.killring;
erow *row = editor.killring;
row->line = realloc(row->line, row->size + 2);
assert(row->line != NULL);
memmove(&row->line[1], &row->line[0], row->size + 1);
@@ -968,7 +990,7 @@ void
unindent_region(void)
{
int start_row, end_row, i, del;
struct erow *row;
erow *row;
if (!editor.mark_set) {
editor_set_status("Mark not set.");
@@ -1132,7 +1154,6 @@ jump_to_position(int col, int row)
editor.curx = col;
editor.cury = row;
scroll();
display_refresh();
}
@@ -1293,6 +1314,8 @@ delete_prev_word(void)
void
delete_row(int at)
{
erow *row = NULL;
if (at < 0 || at >= editor.nrows) {
return;
}
@@ -1304,21 +1327,21 @@ delete_row(int at)
* newline itself and we must NOT also push the entire row here.
*/
if (!editor.no_kill) {
struct erow *r = &editor.row[at];
row = &editor.row[at];
/* Start or continue the kill sequence based on editor.killing */
if (r->size > 0) {
if (row->size > 0) {
/* push raw bytes of the line */
if (!editor.kill) {
killring_start_with_char(
(unsigned char)r->line[0]);
for (int i = 1; i < r->size; i++) {
(unsigned char)row->line[0]);
for (int i = 1; i < row->size; i++) {
killring_append_char(
(unsigned char)r->line[i]);
(unsigned char)row->line[i]);
}
} else {
for (int i = 0; i < r->size; i++) {
for (int i = 0; i < row->size; i++) {
killring_append_char(
(unsigned char)r->line[i]);
(unsigned char)row->line[i]);
}
}
killring_append_char('\n');
@@ -1336,14 +1359,14 @@ delete_row(int at)
erow_free(&editor.row[at]);
memmove(&editor.row[at],
&editor.row[at + 1],
sizeof(struct erow) * (editor.nrows - at - 1));
sizeof(erow) * (editor.nrows - at - 1));
editor.nrows--;
editor.dirty++;
}
void
row_append_row(struct erow *row, char *s, int len)
row_append_row(erow *row, char *s, int len)
{
row->line = realloc(row->line, row->size + len + 1);
assert(row->line != NULL);
@@ -1356,7 +1379,7 @@ row_append_row(struct erow *row, char *s, int len)
void
row_insert_ch(struct erow *row, int at, int16_t c)
row_insert_ch(erow *row, int at, int16_t c)
{
/*
* row_insert_ch just concerns itself with how to update a row.
@@ -1377,7 +1400,7 @@ row_insert_ch(struct erow *row, int at, int16_t c)
void
row_delete_ch(struct erow *row, int at)
row_delete_ch(erow *row, int at)
{
if (at < 0 || at >= row->size) {
return;
@@ -1417,8 +1440,9 @@ insertch(int16_t c)
void
deletech(uint8_t op)
{
struct erow *row = NULL;
erow *row = NULL;
unsigned char dch = 0;
int prev = 0;
if (editor.cury >= editor.nrows) {
return;
@@ -1443,7 +1467,8 @@ deletech(uint8_t op)
row_append_row(&editor.row[editor.cury - 1],
row->line,
row->size);
int prev = editor.no_kill;
prev = editor.no_kill;
editor.no_kill = 1;
delete_row(editor.cury);
@@ -1791,7 +1816,7 @@ editor_find_callback(char* query, int16_t c)
static int last_match = -1; /* row index of last match */
static int direction = 1; /* 1 = forward, -1 = backward */
static char last_query[128] = {0}; /* remember last successful query */
struct erow *row;
erow *row;
int saved_cx = editor.curx;
int saved_cy = editor.cury;
size_t qlen = strlen(query);
@@ -1920,7 +1945,7 @@ editor_openfile(void)
int
first_nonwhitespace(struct erow *row)
first_nonwhitespace(erow *row)
{
int pos;
wchar_t wc;
@@ -1969,7 +1994,7 @@ first_nonwhitespace(struct erow *row)
void
move_cursor_once(int16_t c, int interactive)
{
struct erow *row;
erow *row;
int reps = 0;
row = (editor.cury >= editor.nrows) ? NULL : &editor.row[editor.cury];
@@ -2089,7 +2114,7 @@ move_cursor(int16_t c, int interactive)
void
newline(void)
{
struct erow *row = NULL;
erow *row = NULL;
if (editor.cury >= editor.nrows) {
erow_insert(editor.cury, "", 0);
@@ -2321,8 +2346,10 @@ process_kcommand(int16_t c)
reset_editor();
open_file(buf);
display_refresh();
free(buf);
jump_to_position(jumpx, jumpy);
editor_set_status("file reloaded");
break;
case CTRL_KEY('s'):
case 's':
@@ -2366,6 +2393,8 @@ process_kcommand(int16_t c)
void
process_normal(int16_t c)
{
int cols = 0;
int rows = 0;
int reps = 0;
/* C-u handling must be the very first thing */
@@ -2422,6 +2451,12 @@ process_normal(int16_t c)
case CTRL_KEY('g'):
break;
case CTRL_KEY('l'):
if (get_winsz(&rows, &cols) == 0) {
editor.rows = rows;
editor.cols = cols;
} else {
editor_set_status("Couldn't update window size.");
}
display_refresh();
break;
case CTRL_KEY('s'):
@@ -2467,6 +2502,8 @@ process_normal(int16_t c)
void
process_escape(int16_t c)
{
int reps = 0;
editor_set_status("hi");
switch (c) {
@@ -2479,13 +2516,25 @@ process_escape(int16_t c)
editor.curx = 0;
break;
case 'b':
reps = uarg_get();
while (reps--) {
find_prev_word();
}
break;
case 'd':
reps = uarg_get();
while (reps--) {
delete_next_word();
}
break;
case 'f':
reps = uarg_get();
while (reps--) {
find_next_word();
}
break;
case 'm':
toggle_markset();
@@ -2499,7 +2548,11 @@ process_escape(int16_t c)
toggle_markset();
break;
case BACKSPACE:
reps = uarg_get();
while (reps--) {
delete_prev_word();
}
break;
case ESC_KEY:
case CTRL_KEY('g'):
@@ -2507,6 +2560,8 @@ process_escape(int16_t c)
default:
editor_set_status("unknown ESC key: %04x", c);
}
uarg_clear();
}
@@ -2666,7 +2721,7 @@ enable_termraw(void)
void
display_clear(struct abuf *ab)
display_clear(abuf *ab)
{
if (ab == NULL) {
kwrite(STDOUT_FILENO, ESCSEQ "2J", 4);
@@ -2701,11 +2756,11 @@ setup_terminal(void)
void
draw_rows(struct abuf *ab)
draw_rows(abuf *ab)
{
assert(editor.cols >= 0);
struct erow *row;
erow *row;
char buf[editor.cols];
int len, filerow, padding;
int y;
@@ -2773,7 +2828,7 @@ status_mode_char(void)
void
draw_status_bar(struct abuf *ab)
draw_status_bar(abuf *ab)
{
char status[editor.cols];
char rstatus[editor.cols];
@@ -2823,7 +2878,7 @@ draw_status_bar(struct abuf *ab)
void
draw_message_line(struct abuf *ab)
draw_message_line(abuf *ab)
{
int len = strlen(editor.msg);
@@ -2841,7 +2896,8 @@ draw_message_line(struct abuf *ab)
void
scroll(void)
{
struct erow *row = NULL;
erow *row = NULL;
editor.rx = 0;
if (editor.cury < editor.nrows) {
row = &editor.row[editor.cury];
@@ -2874,7 +2930,7 @@ void
display_refresh(void)
{
char buf[32];
struct abuf ab = ABUF_INIT;
abuf ab = ABUF_INIT;
scroll();
@@ -2918,6 +2974,15 @@ kbhit(void)
{
int bytes_waiting;
ioctl(STDIN_FILENO, FIONREAD, &bytes_waiting);
if (bytes_waiting < 0) {
editor_set_status("kbhit: FIONREAD failed: %s", strerror(errno));
/* if FIONREAD fails, we need to assume we should read. this
* will default to a much slower input sequence, but it'll work.
*/
return 1;
}
return bytes_waiting > 0;
}
@@ -2947,21 +3012,11 @@ loop(void)
void
enable_debugging(const char *logfile)
enable_debugging(void)
{
time_t now;
if (debug_log != NULL) {
fclose(debug_log);
}
if ((debug_log = fopen(logfile, "w")) == NULL) {
fprintf(stderr, "Failed to open error log!\n");
fprintf(stderr, "\t%s\n", strerror(errno));
}
dump_pidfile();
stderr = debug_log;
now = time(&now);
printf("time: %s\n", ctime(&now));
@@ -2973,13 +3028,8 @@ enable_debugging(const char *logfile)
void
deathknell(void)
{
if (debug_log != NULL) {
fflush(stderr);
fclose(debug_log);
debug_log = NULL;
}
if (editor.killring != NULL) {
erow_free(editor.killring);
free(editor.killring);
@@ -3035,9 +3085,12 @@ install_signal_handlers(void)
int
main(int argc, char *argv[])
{
char *logfile = "debug-ke.log";
char *fname = NULL;
char *lnarg = NULL;
int lineno = 0;
int opt;
int debug = 0;
int jump = 0;
install_signal_handlers();
@@ -3046,9 +3099,6 @@ main(int argc, char *argv[])
case 'd':
debug = 1;
break;
case 'f':
logfile = optarg;
break;
default:
fprintf(stderr, "Usage: ke [-d] [-f logfile] [path]\n");
exit(EXIT_FAILURE);
@@ -3060,17 +3110,38 @@ main(int argc, char *argv[])
setlocale(LC_ALL, "");
if (debug) {
enable_debugging(logfile);
enable_debugging();
}
setup_terminal();
init_editor();
if (argc > 0) {
open_file(argv[0]);
fname = argv[0];
}
if (argc > 1) {
lnarg = argv[0];
fname = argv[1];
if (lnarg[0] == '+') {
lnarg++;
}
lineno = atoi(lnarg);
jump = 1;
}
if (fname != NULL) {
open_file(fname);
}
editor_set_status("C-k q to exit / C-k d to dump core");
if (jump) {
if (lineno < 1) {
editor_set_status("Invalid line number %s", lnarg);
} else {
jump_to_position(0, lineno - 1);
}
}
display_clear(NULL);
loop();