12 Commits

Author SHA1 Message Date
acde895fb7 bump version
Some checks failed
Release / Bump Homebrew formula (push) Has been cancelled
2025-11-25 15:50:35 -08:00
78d522ba98 quality of life improvements 2025-11-25 15:50:05 -08:00
feb6667a24 add missing function declarations 2025-11-25 15:32:17 -08:00
Jeremy O'Brien
66c79e0762 fix manpage install with cmake 2025-11-25 13:51:28 -08:00
a51b98c31f switch nix build to cmake 2025-11-25 09:21:03 -08:00
707362574c fixups for nixos 2025-11-25 09:18:26 -08:00
56db8bd8d2 Indent region, version bump.
Some checks failed
Release / Bump Homebrew formula (push) Has been cancelled
2025-11-25 00:47:06 -08:00
a32ef2ff53 move to first non-whitespace on next line
Some checks failed
Release / Bump Homebrew formula (push) Has been cancelled
2025-11-25 00:20:32 -08:00
0c0c3d9ce5 fix arrows in search 2025-11-25 00:12:44 -08:00
7245003769 cleanups 2025-11-24 23:05:40 -08:00
542b1d90a0 bump version
Some checks failed
Release / Bump Homebrew formula (push) Has been cancelled
2025-11-24 22:59:40 -08:00
a359f4e6c4 fix Makefile CFLAGS for nix 2025-11-24 22:59:09 -08:00
6 changed files with 267 additions and 117 deletions

View File

@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.15)
project(ke C) # Specify C language explicitly project(ke C) # Specify C language explicitly
set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD 99)
set(KE_VERSION "1.3.2") set(KE_VERSION "1.3.6")
set(CMAKE_C_FLAGS "-Wall -Wextra -pedantic -Wshadow -Werror -std=c99 -g") 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") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_DEFAULT_SOURCE -D_XOPEN_SOURCE")

View File

@@ -3,6 +3,7 @@ KE_VERSION := devel
DEST := $(HOME)/.local/bin/$(TARGET) DEST := $(HOME)/.local/bin/$(TARGET)
CFLAGS := -Wall -Wextra -pedantic -Wshadow -Werror -std=c99 -g CFLAGS := -Wall -Wextra -pedantic -Wshadow -Werror -std=c99 -g
CFLAGS += -Wno-unused-result
CFLAGS += -D_DEFAULT_SOURCE -D_XOPEN_SOURCE CFLAGS += -D_DEFAULT_SOURCE -D_XOPEN_SOURCE
CFLAGS += -fsanitize=address -fno-omit-frame-pointer CFLAGS += -fsanitize=address -fno-omit-frame-pointer

View File

@@ -16,3 +16,6 @@ To get verbose ASAN messages:
export LSAN_OPTIONS=verbosity=1:log_threads=1 export LSAN_OPTIONS=verbosity=1:log_threads=1
Released under an ISC license. Released under an ISC license.
Started by following along with kilo:
https://viewsourcecode.org/snaptoken/kilo/

View File

@@ -1,16 +1,25 @@
{ {
lib, lib,
installShellFiles,
stdenv, stdenv,
cmake,
installShellFiles,
... ...
}: }:
stdenv.mkDerivation { stdenv.mkDerivation {
pname = "ke"; pname = "ke";
version = "1.3.2"; version = "1.3.6";
src = lib.cleanSource ./.; src = lib.cleanSource ./.;
nativeBuildInputs = [ installShellFiles ]; nativeBuildInputs = [
cmake
installShellFiles
];
cmakeFlags = [
"-DENABLE_ASAN=on"
"-DCMAKE_BUILD_TYPE=Debug"
];
installPhase = '' installPhase = ''
runHook preInstall runHook preInstall
@@ -18,10 +27,8 @@ stdenv.mkDerivation {
mkdir -p $out/bin mkdir -p $out/bin
cp ke $out/bin/ cp ke $out/bin/
installManPage ../ke.1
runHook postInstall runHook postInstall
''; '';
postInstall = ''
installManPage ke.1
'';
} }

5
ke.1
View File

@@ -22,6 +22,7 @@ grandeur. Many commands work with and without control; for example,
saving a file can be done with either C-k s or C-k C-s. Other commands work saving a file can be done with either C-k s or C-k C-s. Other commands work
with ESC or CTRL. with ESC or CTRL.
.Sh K-COMMANDS .Sh K-COMMANDS
k-command mode can be exited with ESC or C-g.
.Bl -tag -width xxxxxxxxxxxx -offset indent .Bl -tag -width xxxxxxxxxxxx -offset indent
.It C-k BACKSPACE .It C-k BACKSPACE
Delete from the cursor to the beginning of the line. Delete from the cursor to the beginning of the line.
@@ -36,7 +37,7 @@ Edit a new file. Also C-k C-e.
.It C-k f .It C-k f
Incremental find. Incremental find.
.It C-k g .It C-k g
Go to a specific line. Also C-k C-g. Go to a specific line.
.It C-k l .It C-k l
List the number of lines of code in a saved file. List the number of lines of code in a saved file.
.It C-k m .It C-k m
@@ -54,6 +55,8 @@ Dump core.
.El .El
.Sh OTHER KEYBINDINGS .Sh OTHER KEYBINDINGS
.Bl -tag -width xxxxxxxxxxxx -offset indent .Bl -tag -width xxxxxxxxxxxx -offset indent
.It C-g
In general, C-g cancels an operation.
.It C-l .It C-l
Refresh the display. Refresh the display.
.It C-s .It C-s

350
main.c
View File

@@ -2,11 +2,7 @@
* kyle's editor * kyle's editor
* *
* first version is a run-through of the kilo editor walkthrough as a * first version is a run-through of the kilo editor walkthrough as a
* set of guiderails. I've made a lot of changes and did some things * set of guiderails. I've made a lot of changes.
* differently. keep an eye for for kte, kyle's text editor - the
* rewrite that will be coming out... when it comes out.
*
* https://viewsourcecode.org/snaptoken/kilo/
*/ */
#include <sys/ioctl.h> #include <sys/ioctl.h>
@@ -21,6 +17,7 @@
#include <string.h> #include <string.h>
#include <locale.h> #include <locale.h>
#include <wchar.h> #include <wchar.h>
#include <wctype.h>
#include <termios.h> #include <termios.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
@@ -155,10 +152,16 @@ void killring_append_char(unsigned char ch);
void killring_prepend_char(unsigned char ch); void killring_prepend_char(unsigned char ch);
void toggle_markset(void); void toggle_markset(void);
int cursor_after_mark(void); int cursor_after_mark(void);
int count_chars_from_cursor_to_mark(void);
void kill_region(void);
void indent_region(void);
void delete_region(void);
/* miscellaneous */ /* miscellaneous */
void kwrite(int fd, const char *buf, int len);
void die(const char *s); void die(const char *s);
int get_winsz(int *rows, int *cols); int get_winsz(int *rows, int *cols);
void jump_to_position(int col, int row);
void goto_line(void); void goto_line(void);
int cursor_at_eol(void); int cursor_at_eol(void);
void delete_row(int at); void delete_row(int at);
@@ -790,6 +793,50 @@ kill_region(void)
} }
void
indent_region(void)
{
int start_row, end_row;
int i;
if (!editor.mark_set) {
return;
}
if (editor.mark_cury < editor.cury) {
start_row = editor.mark_cury;
end_row = editor.cury;
} else if (editor.mark_cury > editor.cury) {
start_row = editor.cury;
end_row = editor.mark_cury;
} else {
start_row = end_row = editor.cury;
}
/* Ensure bounds are valid */
if (start_row < 0) {
start_row = 0;
}
if (end_row >= editor.nrows) {
end_row = editor.nrows - 1;
}
if (start_row >= editor.nrows || end_row < 0) {
return;
}
/* Prepend a tab character to every row in the region */
for (i = start_row; i <= end_row; i++) {
row_insert_ch(&editor.row[i], 0, '\t');
}
/* Move cursor to beginning of the line it was on */
editor.curx = 0;
editor.dirty++;
}
/* call after kill_region */ /* call after kill_region */
void void
delete_region(void) delete_region(void)
@@ -811,8 +858,7 @@ delete_region(void)
swap_int(&cury, &marky); swap_int(&cury, &marky);
} }
editor.curx = markx; jump_to_position(markx, marky);
editor.cury = marky;
while (killed < count) { while (killed < count) {
move_cursor(ARROW_RIGHT); move_cursor(ARROW_RIGHT);
@@ -820,16 +866,34 @@ delete_region(void)
killed++; killed++;
} }
while (editor.curx != markx && editor.cury != marky) {
deletech(KILLRING_NO_OP);
}
editor.kill = 1; editor.kill = 1;
editor_set_status("Region killed."); editor_set_status("Region killed.");
} }
void
kwrite(const int fd, const char *buf, const int len)
{
int wlen = 0;
wlen = write(fd, buf, len);
assert(wlen != -1);
assert(wlen == len);
if (wlen == -1) {
abort();
}
}
void void
die(const char *s) die(const char *s)
{ {
write(STDOUT_FILENO, "\x1b[2J", 4); kwrite(STDOUT_FILENO, "\x1b[2J", 4);
write(STDOUT_FILENO, "\x1b[H", 3); kwrite(STDOUT_FILENO, "\x1b[H", 3);
perror(s); perror(s);
exit(1); exit(1);
@@ -860,6 +924,15 @@ get_winsz(int *rows, int *cols)
} }
void
jump_to_position(const int x, const int y)
{
editor.curx = x;
editor.cury = y;
editor.rowoffs = editor.cury - (editor.rows / 2);
}
void void
goto_line(void) goto_line(void)
{ {
@@ -874,11 +947,12 @@ goto_line(void)
if (lineno < 1 || lineno >= editor.nrows) { if (lineno < 1 || lineno >= editor.nrows) {
editor_set_status("Line number must be between 1 and %d.", editor_set_status("Line number must be between 1 and %d.",
editor.nrows); editor.nrows);
free(query);
return; return;
} }
editor.cury = lineno - 1; jump_to_position(0, lineno - 1);
editor.rowoffs = editor.cury - (editor.rows / 2); free(query);
} }
@@ -1439,17 +1513,17 @@ get_keypress(void)
char * char *
editor_prompt(char *prompt, void (*cb)(char *, int16_t)) editor_prompt(char *prompt, void (*cb)(char *, int16_t))
{ {
size_t bufsz = 128; size_t bufsz = 128;
char *buf = malloc(bufsz); char *buf = malloc(bufsz);
size_t buflen = 0; size_t buflen = 0;
int16_t c; int16_t c;
buf[0] = '\0'; buf[0] = '\0';
while (1) { while (1) {
editor_set_status(prompt, buf); editor_set_status(prompt, buf);
display_refresh(); display_refresh();
while ((c = get_keypress()) <= 0) ; while ((c = get_keypress()) <= 0);
if (c == DEL_KEY || c == CTRL_KEY('h') || c == BACKSPACE) { if (c == DEL_KEY || c == CTRL_KEY('h') || c == BACKSPACE) {
if (buflen != 0) { if (buflen != 0) {
buf[--buflen] = '\0'; buf[--buflen] = '\0';
@@ -1469,7 +1543,7 @@ editor_prompt(char *prompt, void (*cb)(char *, int16_t))
} }
return buf; return buf;
} }
} else if ((c == TAB_KEY) || (c >= 0x20 && c != 0x7f)) { } else if ((c == TAB_KEY) || (c >= 0x20 && c < 0x7f)) {
if (buflen == bufsz - 1) { if (buflen == bufsz - 1) {
bufsz *= 2; bufsz *= 2;
buf = realloc(buf, bufsz); buf = realloc(buf, bufsz);
@@ -1498,7 +1572,7 @@ editor_find_callback(char *query, int16_t c)
char *match; char *match;
struct erow *row; struct erow *row;
if (c == '\r' || c == ESC_KEY) { if (c == '\r' || c == ESC_KEY || c == CTRL_KEY('g')) {
/* reset search */ /* reset search */
lmatch = -1; lmatch = -1;
dir = 1; dir = 1;
@@ -1587,6 +1661,41 @@ editor_openfile(void)
} }
int
first_nonwhitespace(struct erow *row)
{
int pos;
wchar_t wc;
mbstate_t state;
size_t len;
if (row == NULL) {
return 0;
}
memset(&state, 0, sizeof(state));
pos = 0;
while (pos < row->size) {
len = mbrtowc(&wc, &row->line[pos], row->size - pos, &state);
if (len == (size_t) -1 || len == (size_t) -2) {
/* Invalid or incomplete sequence, stop here */
break;
}
if (len == 0) {
/* Null character, stop here */
break;
}
if (!iswspace(wc)) {
/* Found non-whitespace character */
break;
}
pos += len;
}
return pos;
}
void void
move_cursor(int16_t c) move_cursor(int16_t c)
{ {
@@ -1596,86 +1705,97 @@ move_cursor(int16_t c)
row = (editor.cury >= editor.nrows) ? NULL : &editor.row[editor.cury]; row = (editor.cury >= editor.nrows) ? NULL : &editor.row[editor.cury];
switch (c) { switch (c) {
case ARROW_UP: case ARROW_UP:
case CTRL_KEY('p'): case CTRL_KEY('p'):
if (editor.cury > 0) { if (editor.cury > 0) {
editor.cury--; editor.cury--;
} row = (editor.cury >= editor.nrows)
break; ? NULL
case ARROW_DOWN: : &editor.row[editor.cury];
case CTRL_KEY('n'): editor.curx = first_nonwhitespace(row);
if (editor.cury < editor.nrows) { }
editor.cury++; break;
} case ARROW_DOWN:
break; case CTRL_KEY('n'):
case ARROW_RIGHT: if (editor.cury < editor.nrows) {
case CTRL_KEY('f'): editor.cury++;
if (row && editor.curx < row->size) { row = (editor.cury >= editor.nrows)
? NULL
: &editor.row[editor.cury];
editor.curx = first_nonwhitespace(row);
}
break;
case ARROW_RIGHT:
case CTRL_KEY('f'):
if (row && editor.curx < row->size) {
editor.curx++;
/* skip over UTF-8 continuation bytes */
while (row && editor.curx < row->size &&
((unsigned char) row->line[editor.curx] &
0xC0) == 0x80) {
editor.curx++; editor.curx++;
/* skip over UTF-8 continuation bytes */
while (row && editor.curx < row->size &&
((unsigned char) row->line[editor.curx] &
0xC0) == 0x80) {
editor.curx++;
}
} else if (row && editor.curx == row->size) {
editor.cury++;
editor.curx = 0;
} }
break; } else if (row && editor.curx == row->size) {
case ARROW_LEFT: editor.cury++;
case CTRL_KEY('b'): row = (editor.cury >= editor.nrows)
if (editor.curx > 0) { ? NULL
: &editor.row[editor.cury];
editor.curx = first_nonwhitespace(row);
}
break;
case ARROW_LEFT:
case CTRL_KEY('b'):
if (editor.curx > 0) {
editor.curx--;
/* move to the start byte if we landed on a continuation */
while (editor.curx > 0 &&
((unsigned char) row->line[editor.curx] &
0xC0) == 0x80) {
editor.curx--; editor.curx--;
/* move to the start byte if we landed on a continuation */
while (editor.curx > 0 &&
((unsigned char) row->line[editor.curx] &
0xC0) == 0x80) {
editor.curx--;
}
} else if (editor.cury > 0) {
editor.cury--;
editor.curx = editor.row[editor.cury].size;
/* ensure at a codepoint boundary at end of previous line */
row = &editor.row[editor.cury];
while (editor.curx > 0 &&
((unsigned char) row->line[editor.curx] &
0xC0) == 0x80) {
editor.curx--;
}
}
break;
case PG_UP:
case PG_DN:
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;
while (--reps) {
move_cursor(c == PG_UP ? ARROW_UP : ARROW_DOWN);
}
break;
case HOME_KEY:
case CTRL_KEY('a'):
editor.curx = 0;
break;
case END_KEY:
case CTRL_KEY('e'):
if (editor.nrows == 0) {
break;
} }
} else if (editor.cury > 0) {
editor.cury--;
editor.curx = editor.row[editor.cury].size; editor.curx = editor.row[editor.cury].size;
/* ensure at a codepoint boundary at end of previous line */
row = &editor.row[editor.cury];
while (editor.curx > 0 &&
((unsigned char) row->line[editor.curx] &
0xC0) == 0x80) {
editor.curx--;
}
}
break;
case PG_UP:
case PG_DN:
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;
while (--reps) {
move_cursor(c == PG_UP ? ARROW_UP : ARROW_DOWN);
}
break;
case HOME_KEY:
case CTRL_KEY('a'):
editor.curx = 0;
break;
case END_KEY:
case CTRL_KEY('e'):
if (editor.nrows == 0) {
break; break;
default: }
break; editor.curx = editor.row[editor.cury].size;
break;
default:
break;
} }
@@ -1692,7 +1812,10 @@ newline(void)
{ {
struct erow *row = NULL; struct erow *row = NULL;
if (editor.curx == 0) { if (editor.cury >= editor.nrows) {
/* At or past end of file, insert empty line */
erow_insert(editor.cury, "", 0);
} else if (editor.curx == 0) {
erow_insert(editor.cury, "", 0); erow_insert(editor.cury, "", 0);
} else { } else {
row = &editor.row[editor.cury]; row = &editor.row[editor.cury];
@@ -1752,13 +1875,11 @@ get_cloc_code_lines(const char* filename)
} }
if (fgets(buffer, sizeof(buffer), pipe) != NULL) { if (fgets(buffer, sizeof(buffer), pipe) != NULL) {
// Remove trailing newline
len = strlen(buffer); len = strlen(buffer);
if (len > 0 && buffer[len - 1] == '\n') { if (len > 0 && buffer[len - 1] == '\n') {
buffer[len - 1] = '\0'; buffer[len - 1] = '\0';
} }
// Allocate and copy the string
result = malloc(strlen(buffer) + 1); result = malloc(strlen(buffer) + 1);
assert(result != NULL); assert(result != NULL);
if (result) { if (result) {
@@ -1820,7 +1941,6 @@ process_kcommand(int16_t c)
break; break;
case 'g': case 'g':
case CTRL_KEY('g'):
goto_line(); goto_line();
break; break;
case BACKSPACE: case BACKSPACE:
@@ -1866,13 +1986,20 @@ process_kcommand(int16_t c)
case 'y': case 'y':
killring_yank(); killring_yank();
break; break;
case ESC_KEY:
case CTRL_KEY('g'):
break;
default: default:
if (isprint(c)) {
editor_set_status("unknown kcommand '%c'", c);
break;
}
editor_set_status("unknown kcommand: %04x", c); editor_set_status("unknown kcommand: %04x", c);
return; return;
} }
editor.dirtyex = 1; editor.dirtyex = 1;
return;
} }
@@ -1904,6 +2031,8 @@ process_normal(int16_t c)
deletech(KILLRING_PREPEND); deletech(KILLRING_PREPEND);
} }
break; break;
case CTRL_KEY('g'):
break;
case CTRL_KEY('l'): case CTRL_KEY('l'):
display_refresh(); display_refresh();
break; break;
@@ -1921,12 +2050,17 @@ process_normal(int16_t c)
case ESC_KEY: case ESC_KEY:
editor.mode = MODE_ESCAPE; editor.mode = MODE_ESCAPE;
break; break;
default: default:
/* Insert any printable byte: ASCII 0x200x7E and all bytes >=0x80. */ if (c == TAB_KEY) {
if ((c == TAB_KEY) || (c >= 0x20 && c != 0x7f)) { if (editor.mark_set) {
insertch(c); indent_region();
} } else {
break; insertch(c);
}
} else if (c >= 0x20 && c != 0x7f) {
insertch(c);
}
break;
} }
editor.dirtyex = 1; editor.dirtyex = 1;
@@ -1970,6 +2104,9 @@ process_escape(int16_t c)
case BACKSPACE: case BACKSPACE:
delete_prev_word(); delete_prev_word();
break; break;
case ESC_KEY:
case CTRL_KEY('g'):
break; /* escape out of escape-mode */
default: default:
editor_set_status("unknown ESC key: %04x", c); editor_set_status("unknown ESC key: %04x", c);
} }
@@ -2054,8 +2191,8 @@ void
display_clear(struct abuf *ab) display_clear(struct abuf *ab)
{ {
if (ab == NULL) { if (ab == NULL) {
write(STDOUT_FILENO, ESCSEQ "2J", 4); kwrite(STDOUT_FILENO, ESCSEQ "2J", 4);
write(STDOUT_FILENO, ESCSEQ "H", 3); kwrite(STDOUT_FILENO, ESCSEQ "H", 3);
} else { } else {
ab_append(ab, ESCSEQ "2J", 4); ab_append(ab, ESCSEQ "2J", 4);
ab_append(ab, ESCSEQ "H", 3); ab_append(ab, ESCSEQ "H", 3);
@@ -2269,7 +2406,7 @@ display_refresh(void)
/* ab_append(&ab, ESCSEQ "1;2H", 7); */ /* ab_append(&ab, ESCSEQ "1;2H", 7); */
ab_append(&ab, ESCSEQ "?25h", 6); ab_append(&ab, ESCSEQ "?25h", 6);
write(STDOUT_FILENO, ab.b, ab.len); kwrite(STDOUT_FILENO, ab.b, ab.len);
ab_free(&ab); ab_free(&ab);
} }
@@ -2312,7 +2449,6 @@ loop(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
// Set locale for proper UTF-8 handling
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
setup_terminal(); setup_terminal();