11 Commits

Author SHA1 Message Date
f147c866ab v1.2.0 with basic killring.
Some checks failed
Release / Bump Homebrew formula (push) Has been cancelled
2025-11-24 14:37:33 -08:00
1227d0abf4 Support basic killing/yanking. 2025-11-24 14:35:44 -08:00
d3591331a5 Update man page with yank. 2025-11-24 14:03:12 -08:00
5eafc1a34b local install in Makefile 2025-11-24 13:57:10 -08:00
a0103dd5aa Start killring work. 2025-11-24 13:46:00 -08:00
3782880062 UTF-8 support. 2025-11-24 13:11:24 -08:00
e345b55595 Fix version number
Some checks failed
Release / Bump Homebrew formula (push) Has been cancelled
2025-11-24 11:05:51 -08:00
d1978a3b98 Code cleanups and editor fixups.
Some checks failed
Release / Bump Homebrew formula (push) Has been cancelled
2025-11-24 10:54:48 -08:00
5ee8234ab7 Update the manpage.
Print the core dump k-command correctly.
2025-11-24 10:23:39 -08:00
f82d1f8831 move nix to nixos config 2025-11-24 10:22:48 -08:00
2019ec10ce local testing updates 2025-11-23 19:15:19 -08:00
6 changed files with 480 additions and 112 deletions

2
.gitignore vendored
View File

@@ -1,2 +1,4 @@
*.log *.log
build build
ke
*.txt

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.1.0") set(KE_VERSION "1.2.0")
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

@@ -1,6 +1,6 @@
TARGET := ke TARGET := ke
KE_VERSION := devel KE_VERSION := devel
DEST := $(HOME)/.local/bin/$(TARGET)
CFLAGS := -Wall -Wextra -pedantic -Wshadow -Werror -std=c99 -g CFLAGS := -Wall -Wextra -pedantic -Wshadow -Werror -std=c99 -g
CFLAGS += -D_DEFAULT_SOURCE -D_XOPEN_SOURCE CFLAGS += -D_DEFAULT_SOURCE -D_XOPEN_SOURCE
@@ -8,10 +8,19 @@ CFLAGS += -fsanitize=address -fno-omit-frame-pointer
LDFLAGS := -fsanitize=address LDFLAGS := -fsanitize=address
all: $(TARGET) all: $(TARGET) test.txt
$(TARGET): main.c $(TARGET): main.c
$(CC) $(CFLAGS) -o $(TARGET) $(LDFLAGS) main.c $(CC) $(CFLAGS) -o $(TARGET) $(LDFLAGS) main.c
.PHONY: install
#install: $(TARGET)
install:
cp $(TARGET) $(DEST)
clean: clean:
rm -f $(TARGET) rm -f $(TARGET)
.PHONY: test.txt
test.txt:
cp test.txt.bak $@

4
ke.1
View File

@@ -42,7 +42,9 @@ exit the editor. Also C-k C-q.
save the file, prompting for a filename if needed. Also C-k C-s. save the file, prompting for a filename if needed. Also C-k C-s.
.It C-k x .It C-k x
save the file and exit. Also C-k C-x. save the file and exit. Also C-k C-x.
.It C-k \ .It C-k y
Yank the killring.
.It C-k \[char92]
Dump core. Dump core.
.El .El
.Sh FIND .Sh FIND

484
main.c
View File

@@ -19,6 +19,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <locale.h>
#include <wchar.h>
#include <termios.h> #include <termios.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
@@ -49,6 +51,13 @@
#define INITIAL_CAPACITY 64 #define INITIAL_CAPACITY 64
#define KILLRING_NO_OP 0 /* don't touch the killring */
#define KILLRING_APPEND 1 /* append deleted chars */
#define KILLRING_PREPEND 2 /* prepend deleted chars */
#define KILLING_SET 3 /* set killring to deleted char */
#define KILLRING_FLUSH 4 /* clear the killring */
int int
next_power_of_2(int n) next_power_of_2(int n)
{ {
@@ -120,7 +129,6 @@ void editor_set_status(const char *fmt, ...);
/* miscellaneous */ /* miscellaneous */
void die(const char *s); void die(const char *s);
int iswspace(const char c);
int get_winsz(int *rows, int *cols); int get_winsz(int *rows, int *cols);
void goto_line(void); void goto_line(void);
int cursor_at_eol(void); int cursor_at_eol(void);
@@ -129,11 +137,16 @@ void delete_next_word(void);
void find_prev_word(void); void find_prev_word(void);
void delete_prev_word(void); void delete_prev_word(void);
void delete_row(int at); void delete_row(int at);
void killring_start_with_char(unsigned char ch);
void killring_append_char(unsigned char ch);
void killring_prepend_char(unsigned char ch);
void killring_flush(void);
void killring_yank(void);
void row_append_row(struct erow *row, char *s, int len); void row_append_row(struct erow *row, char *s, int len);
void row_insert_ch(struct erow *row, int at, int16_t c); void row_insert_ch(struct erow *row, int at, int16_t c);
void row_delete_ch(struct erow *row, int at); void row_delete_ch(struct erow *row, int at);
void insertch(int16_t c); void insertch(int16_t c);
void deletech(void); void deletech(uint8_t op);
void open_file(const char *filename); void open_file(const char *filename);
char *rows_to_buffer(int *buflen); char *rows_to_buffer(int *buflen);
int save_file(void); int save_file(void);
@@ -195,6 +208,9 @@ struct editor_t {
int nrows; int nrows;
int rowoffs, coloffs; int rowoffs, coloffs;
struct erow *row; struct erow *row;
struct erow *killring;
int killing; /* are we in a contiguous delete sequence? */
int suppress_killrow; /* internal flag: don't add to killring inside delete_row */
char *filename; char *filename;
int dirty; int dirty;
int dirtyex; int dirtyex;
@@ -210,6 +226,9 @@ struct editor_t {
.rowoffs = 0, .rowoffs = 0,
.coloffs = 0, .coloffs = 0,
.row = NULL, .row = NULL,
.killring = NULL,
.killing = 0,
.suppress_killrow = 0,
.filename = NULL, .filename = NULL,
.dirty = 0, .dirty = 0,
.dirtyex = 0, .dirtyex = 0,
@@ -237,6 +256,9 @@ init_editor(void)
editor.nrows = 0; editor.nrows = 0;
editor.rowoffs = editor.coloffs = 0; editor.rowoffs = editor.coloffs = 0;
editor.row = NULL; editor.row = NULL;
editor.killring = NULL;
editor.killing = 0;
editor.suppress_killrow = 0;
editor.msg[0] = '\0'; editor.msg[0] = '\0';
editor.msgtm = 0; editor.msgtm = 0;
@@ -262,6 +284,12 @@ reset_editor(void)
editor.filename = NULL; editor.filename = NULL;
} }
if (editor.killring != NULL) {
erow_free(editor.killring);
free(editor.killring);
editor.killring = NULL;
}
init_editor(); init_editor();
} }
@@ -304,9 +332,9 @@ nibble_to_hex(char c)
{ {
c &= 0xf; c &= 0xf;
if (c < 10) { if (c < 10) {
return c + 0x30; return (char)('0' + c);
} }
return c + 0x41; return (char)('A' + (c - 10));
} }
@@ -314,15 +342,54 @@ int
erow_render_to_cursor(struct erow *row, int cx) erow_render_to_cursor(struct erow *row, int cx)
{ {
int rx = 0; int rx = 0;
int j; size_t j = 0;
for (j = 0; j < cx; j++) { wchar_t wc;
if (row->line[j] == '\t') { mbstate_t st;
rx += (TAB_STOP-1) - (rx%TAB_STOP);
} else if (row->line[j] < 0x20) { memset(&st, 0, sizeof(st));
rx += 2;
} while (j < (size_t)cx && j < (size_t)row->size) {
unsigned char b = (unsigned char)row->line[j];
if (b == '\t') {
rx += (TAB_STOP - 1) - (rx % TAB_STOP);
rx++; rx++;
j++;
continue;
}
if (b < 0x20) {
/* render as \xx -> width 3 */
rx += 3;
j++;
continue;
}
size_t rem = (size_t)row->size - j;
size_t n = mbrtowc(&wc, &row->line[j], rem, &st);
if (n == (size_t)-2) {
/* incomplete sequence at end; treat one byte */
rx += 1;
j += 1;
memset(&st, 0, sizeof(st));
}
else if (n == (size_t)-1) {
/* invalid byte; consume one and reset state */
rx += 1;
j += 1;
memset(&st, 0, sizeof(st));
}
else if (n == 0) {
/* null character */
rx += 0;
j += 1;
}
else {
int w = wcwidth(wc);
if (w < 0) w = 1; /* non-printable -> treat as width 1 */
rx += w;
j += n;
}
} }
return rx; return rx;
@@ -333,22 +400,59 @@ int
erow_cursor_to_render(struct erow *row, int rx) erow_cursor_to_render(struct erow *row, int rx)
{ {
int cur_rx = 0; int cur_rx = 0;
int curx = 0; size_t j = 0;
for (curx = 0; curx < row->size; curx++) { wchar_t wc;
if (row->line[curx] == '\t') { mbstate_t st;
cur_rx += (TAB_STOP - 1) - (cur_rx % TAB_STOP);
} else if (row->line[curx] < 0x20) { memset(&st, 0, sizeof(st));
cur_rx += 2;
while (j < (size_t)row->size) {
int w = 0;
size_t adv = 1;
unsigned char b = (unsigned char)row->line[j];
if (b == '\t') {
int add = (TAB_STOP - 1) - (cur_rx % TAB_STOP);
w = add + 1;
adv = 1;
/* tabs are single byte */
} }
cur_rx++; else if (b < 0x20) {
w = 3; /* "\\xx" */
adv = 1;
}
else {
size_t rem = (size_t)row->size - j;
size_t n = mbrtowc(&wc, &row->line[j], rem, &st);
if (cur_rx > rx) { if (n == (size_t)-2 || n == (size_t)-1) {
/* invalid/incomplete */
w = 1;
adv = 1;
memset(&st, 0, sizeof(st));
}
else if (n == 0) {
w = 0;
adv = 1;
}
else {
int ww = wcwidth(wc);
if (ww < 0) ww = 1;
w = ww;
adv = n;
}
}
if (cur_rx + w > rx) {
break; break;
} }
cur_rx += w;
j += adv;
} }
return curx; return (int)j;
} }
@@ -365,7 +469,9 @@ erow_update(struct erow *row)
for (j = 0; j < row->size; j++) { for (j = 0; j < row->size; j++) {
if (row->line[j] == '\t') { if (row->line[j] == '\t') {
tabs++; tabs++;
} else if (!isprint(row->line[j])) { }
else if ((unsigned char)row->line[j] < 0x20) {
/* treat only ASCII control characters as non-printable */
ctrl++; ctrl++;
} }
} }
@@ -375,19 +481,23 @@ erow_update(struct erow *row)
row->rsize = 0; row->rsize = 0;
} }
row->render = NULL; row->render = NULL;
row->render = malloc(row->size + (tabs * (TAB_STOP-1)) + (ctrl * 3) + 1); row->render = malloc(row->size + (tabs * (TAB_STOP - 1)) + (ctrl * 3) + 1);
assert(row->render != NULL); assert(row->render != NULL);
for (j = 0; j < row->size; j++) { for (j = 0; j < row->size; j++) {
if (row->line[j] == '\t') { if (row->line[j] == '\t') {
do { do {
row->render[i++] = ' '; row->render[i++] = ' ';
} while ((i % TAB_STOP) != 0); }
} else if (!isprint(row->line[j])) { while ((i % TAB_STOP) != 0);
}
else if ((unsigned char)row->line[j] < 0x20) {
row->render[i++] = '\\'; row->render[i++] = '\\';
row->render[i++] = nibble_to_hex(row->line[j] >> 4); row->render[i++] = nibble_to_hex(row->line[j] >> 4);
row->render[i++] = nibble_to_hex(row->line[j] & 0x0f); row->render[i++] = nibble_to_hex(row->line[j] & 0x0f);
} else { }
else {
/* leave UTF-8 multibyte bytes untouched so terminal can render */
row->render[i++] = row->line[j]; row->render[i++] = row->line[j];
} }
} }
@@ -444,6 +554,105 @@ erow_insert(int at, char *s, int len)
} }
void
killring_flush(void)
{
if (editor.killring != NULL) {
erow_free(editor.killring);
free(editor.killring);
editor.killring = NULL;
}
}
void
killring_yank(void)
{
if (editor.killring == NULL) {
return;
}
/*
* Insert killring contents at the cursor without clearing the ring.
* Interpret '\n' as an actual newline() rather than inserting a raw 0x0A
* byte, so yanked content preserves lines correctly.
*/
for (int i = 0; i < editor.killring->size; i++) {
unsigned char ch = (unsigned char)editor.killring->line[i];
if (ch == '\n') {
newline();
}
else {
insertch(ch);
}
}
}
void
killring_start_with_char(unsigned char ch)
{
struct erow *row = NULL;
if (editor.killring != NULL) {
erow_free(editor.killring);
free(editor.killring);
editor.killring = NULL;
}
editor.killring = malloc(sizeof(struct erow));
assert(editor.killring != NULL);
assert(erow_init(editor.killring, 0) == 0);
/* append one char to empty killring without affecting editor.dirty */
row = editor.killring;
row->line = realloc(row->line, row->size + 2);
assert(row->line != NULL);
row->line[row->size] = ch;
row->size++;
row->line[row->size] = '\0';
erow_update(row);
}
void
killring_append_char(unsigned char ch)
{
struct erow *row = NULL;
if (editor.killring == NULL) {
killring_start_with_char(ch);
return;
}
row = editor.killring;
row->line = realloc(row->line, row->size + 2);
assert(row->line != NULL);
row->line[row->size] = ch;
row->size++;
row->line[row->size] = '\0';
erow_update(row);
}
void
killring_prepend_char(unsigned char ch)
{
if (editor.killring == NULL) {
killring_start_with_char(ch);
return;
}
struct 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);
row->line[0] = ch;
row->size++;
erow_update(row);
}
void void
erow_free(struct erow *row) erow_free(struct erow *row)
{ {
@@ -469,11 +678,6 @@ die(const char *s)
} }
int
iswspace(const char c)
{
return isspace(c) || c == '\t';
}
/* /*
* get_winsz uses the TIOCGWINSZ to get the window size. * get_winsz uses the TIOCGWINSZ to get the window size.
@@ -540,15 +744,15 @@ find_next_word(void)
} }
if (isalnum(editor.row[editor.cury].line[editor.curx])) { if (isalnum(editor.row[editor.cury].line[editor.curx])) {
while (!iswspace(editor.row[editor.cury].line[editor.curx]) && !cursor_at_eol()) { while (!isspace(editor.row[editor.cury].line[editor.curx]) && !cursor_at_eol()) {
move_cursor(ARROW_RIGHT); move_cursor(ARROW_RIGHT);
} }
return; return;
} }
if (iswspace(editor.row[editor.cury].line[editor.curx])) { if (isspace(editor.row[editor.cury].line[editor.curx])) {
while (iswspace(editor.row[editor.cury].line[editor.curx])) { while (isspace(editor.row[editor.cury].line[editor.curx])) {
move_cursor(ARROW_RIGHT); move_cursor(ARROW_RIGHT);
} }
@@ -562,22 +766,22 @@ delete_next_word(void)
{ {
while (cursor_at_eol()) { while (cursor_at_eol()) {
move_cursor(ARROW_RIGHT); move_cursor(ARROW_RIGHT);
deletech(); deletech(KILLRING_APPEND);
} }
if (isalnum(editor.row[editor.cury].line[editor.curx])) { if (isalnum(editor.row[editor.cury].line[editor.curx])) {
while (!iswspace(editor.row[editor.cury].line[editor.curx]) && !cursor_at_eol()) { while (!isspace(editor.row[editor.cury].line[editor.curx]) && !cursor_at_eol()) {
move_cursor(ARROW_RIGHT); move_cursor(ARROW_RIGHT);
deletech(); deletech(KILLRING_APPEND);
} }
return; return;
} }
if (iswspace(editor.row[editor.cury].line[editor.curx])) { if (isspace(editor.row[editor.cury].line[editor.curx])) {
while (iswspace(editor.row[editor.cury].line[editor.curx])) { while (isspace(editor.row[editor.cury].line[editor.curx])) {
move_cursor(ARROW_RIGHT); move_cursor(ARROW_RIGHT);
deletech(); deletech(KILLRING_APPEND);
} }
delete_next_word(); delete_next_word();
@@ -594,7 +798,7 @@ find_prev_word(void)
move_cursor(ARROW_LEFT); move_cursor(ARROW_LEFT);
while (cursor_at_eol() || iswspace(editor.row[editor.cury].line[editor.curx])) { while (cursor_at_eol() || isspace(editor.row[editor.cury].line[editor.curx])) {
if (editor.cury == 0 && editor.curx == 0) { if (editor.cury == 0 && editor.curx == 0) {
return; return;
} }
@@ -602,7 +806,7 @@ find_prev_word(void)
move_cursor(ARROW_LEFT); move_cursor(ARROW_LEFT);
} }
while (editor.curx > 0 && !iswspace(editor.row[editor.cury].line[editor.curx - 1])) { while (editor.curx > 0 && !isspace(editor.row[editor.cury].line[editor.curx - 1])) {
move_cursor(ARROW_LEFT); move_cursor(ARROW_LEFT);
} }
} }
@@ -614,24 +818,26 @@ delete_prev_word(void)
return; return;
} }
deletech(); deletech(KILLRING_PREPEND);
while (editor.cury > 0 || editor.curx > 0) { while (editor.cury > 0 || editor.curx > 0) {
if (editor.curx == 0) { if (editor.curx == 0) {
deletech(); deletech(KILLRING_PREPEND);
} else { continue
if (!iswspace(editor.row[editor.cury].line[editor.curx - 1])) { }
if (!isspace(editor.row[editor.cury].line[editor.curx - 1])) {
break; break;
} }
deletech();
} deletech(KILLRING_PREPEND);
} }
while (editor.curx > 0) { while (editor.curx > 0) {
if (iswspace(editor.row[editor.cury].line[editor.curx - 1])) { if (isspace(editor.row[editor.cury].line[editor.curx - 1])) {
break; break;
} }
deletech(); deletech(KILLRING_PREPEND);
} }
} }
@@ -642,8 +848,42 @@ delete_row(int at)
return; return;
} }
/*
* Update killring with the deleted row's contents followed by a newline
* unless this deletion is an internal merge triggered by deletech at
* start-of-line. In that case, deletech will account for the single
* newline itself and we must NOT also push the entire row here.
*/
if (!editor.suppress_killrow) {
struct erow* r = &editor.row[at];
/* Start or continue the kill sequence based on editor.killing */
if (r->size > 0) {
/* push raw bytes of the line */
if (!editor.killing) {
killring_start_with_char((unsigned char)r->line[0]);
for (int i = 1; i < r->size; i++) {
killring_append_char((unsigned char)r->line[i]);
}
} else {
for (int i = 0; i < r->size; i++) {
killring_append_char((unsigned char)r->line[i]);
}
}
killring_append_char('\n');
editor.killing = 1;
} else {
if (!editor.killing) {
killring_start_with_char('\n');
}
else {
killring_append_char('\n');
}
editor.killing = 1;
}
}
erow_free(&editor.row[at]); erow_free(&editor.row[at]);
memmove(&editor.row[at], &editor.row[at+1], memmove(&editor.row[at], &editor.row[at + 1],
sizeof(struct erow) * (editor.nrows - at - 1)); sizeof(struct erow) * (editor.nrows - at - 1));
editor.nrows--; editor.nrows--;
editor.dirty++; editor.dirty++;
@@ -690,6 +930,7 @@ row_delete_ch(struct erow *row, int at)
if (at < 0 || at >= row->size) { if (at < 0 || at >= row->size) {
return; return;
} }
memmove(&row->line[at], &row->line[at+1], row->size-at); memmove(&row->line[at], &row->line[at+1], row->size-at);
row->size--; row->size--;
erow_update(row); erow_update(row);
@@ -709,7 +950,11 @@ insertch(int16_t c)
erow_insert(editor.nrows, "", 0); erow_insert(editor.nrows, "", 0);
} }
row_insert_ch(&editor.row[editor.cury], editor.curx, (char)(c & 0xff)); /* Any insertion breaks a delete sequence for killring chaining. */
editor.killing = 0;
/* Ensure we pass a non-negative byte value to avoid assert(c > 0). */
row_insert_ch(&editor.row[editor.cury], editor.curx,
(int16_t)(c & 0xff));
editor.curx++; editor.curx++;
editor.dirty++; editor.dirty++;
} }
@@ -719,9 +964,10 @@ insertch(int16_t c)
* deletech * deletech
*/ */
void void
deletech(void) deletech(uint8_t op)
{ {
struct erow *row = NULL; struct erow* row = NULL;
unsigned char dch = 0;
if (editor.cury >= editor.nrows) { if (editor.cury >= editor.nrows) {
return; return;
@@ -732,16 +978,60 @@ deletech(void)
} }
row = &editor.row[editor.cury]; row = &editor.row[editor.cury];
/* determine which character is being deleted for killring purposes */
if (editor.curx > 0) {
dch = (unsigned char)row->line[editor.curx - 1];
}
else {
/* deleting at start of line merges with previous line: treat as newline */
dch = '\n';
}
/* update buffer */
if (editor.curx > 0) { if (editor.curx > 0) {
row_delete_ch(row, editor.curx - 1); row_delete_ch(row, editor.curx - 1);
editor.curx--; editor.curx--;
} else { }
else {
editor.curx = editor.row[editor.cury - 1].size; editor.curx = editor.row[editor.cury - 1].size;
row_append_row(&editor.row[editor.cury - 1], row->line, row_append_row(&editor.row[editor.cury - 1], row->line,
row->size); row->size);
int prev = editor.suppress_killrow;
editor.suppress_killrow = 1; /* prevent killring update on internal row delete */
delete_row(editor.cury); delete_row(editor.cury);
editor.suppress_killrow = prev;
editor.cury--; editor.cury--;
} }
/* update killring if requested */
if (op == KILLRING_FLUSH) {
killring_flush();
editor.killing = 0;
return;
}
if (op == KILLRING_NO_OP) {
/* Do not modify killring or chaining state. */
return;
}
if (!editor.killing) {
killring_start_with_char(dch);
editor.killing = 1;
return;
}
if (op == KILLING_SET) {
killring_start_with_char(dch);
editor.killing = 1;
}
else if (op == KILLRING_APPEND) {
killring_append_char(dch);
}
else if (op == KILLRING_PREPEND) {
killring_prepend_char(dch);
}
} }
@@ -910,12 +1200,16 @@ int16_t
get_keypress(void) get_keypress(void)
{ {
char seq[3]; char seq[3];
char c = -1; /* read raw byte so UTF-8 bytes (>=0x80) are not sign-extended */
unsigned char uc = 0;
int16_t c;
if (read(STDIN_FILENO, &c, 1) == -1) { if (read(STDIN_FILENO, &uc, 1) == -1) {
die("get_keypress:read"); die("get_keypress:read");
} }
c = (int16_t)uc;
if (c == 0x1b) { if (c == 0x1b) {
if (read(STDIN_FILENO, &seq[0], 1) != 1) return c; if (read(STDIN_FILENO, &seq[0], 1) != 1) return c;
if (read(STDIN_FILENO, &seq[1], 1) != 1) return c; if (read(STDIN_FILENO, &seq[1], 1) != 1) return c;
@@ -935,7 +1229,8 @@ get_keypress(void)
case '8': return END_KEY; case '8': return END_KEY;
} }
} }
} else { }
else {
switch (seq[1]) { switch (seq[1]) {
case 'A': return ARROW_UP; case 'A': return ARROW_UP;
case 'B': return ARROW_DOWN; case 'B': return ARROW_DOWN;
@@ -948,7 +1243,8 @@ get_keypress(void)
/* nada */ ; /* nada */ ;
} }
} }
} else if (seq[0] == 'O') { }
else if (seq[0] == 'O') {
switch (seq[1]) { switch (seq[1]) {
case 'F': return END_KEY; case 'F': return END_KEY;
case 'H': return HOME_KEY; case 'H': return HOME_KEY;
@@ -995,14 +1291,14 @@ editor_prompt(char *prompt, void (*cb)(char *, int16_t))
} }
return buf; return buf;
} }
} else if (!iscntrl(c) && c < 128) { } 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);
assert(buf != NULL); assert(buf != NULL);
} }
buf[buflen++] = c; buf[buflen++] = (char)(c & 0xff);
buf[buflen] = '\0'; buf[buflen] = '\0';
} }
@@ -1114,7 +1410,7 @@ editor_openfile(void)
void void
move_cursor(int16_t c) move_cursor(int16_t c)
{ {
struct erow *row; struct erow* row;
int reps; int reps;
row = (editor.cury >= editor.nrows) ? NULL : &editor.row[editor.cury]; row = (editor.cury >= editor.nrows) ? NULL : &editor.row[editor.cury];
@@ -1136,7 +1432,13 @@ move_cursor(int16_t c)
case CTRL_KEY('f'): case CTRL_KEY('f'):
if (row && editor.curx < row->size) { if (row && editor.curx < row->size) {
editor.curx++; editor.curx++;
} else if (row && editor.curx == row->size) { /* 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.cury++;
editor.curx = 0; editor.curx = 0;
} }
@@ -1145,17 +1447,30 @@ move_cursor(int16_t c)
case CTRL_KEY('b'): case CTRL_KEY('b'):
if (editor.curx > 0) { if (editor.curx > 0) {
editor.curx--; editor.curx--;
} else if (editor.cury > 0) { /* 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.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; break;
case PG_UP: case PG_UP:
case PG_DN: { case PG_DN:
if (c == PG_UP) { if (c == PG_UP) {
editor.cury = editor.rowoffs; editor.cury = editor.rowoffs;
} else if (c == PG_DN) { }
editor.cury = editor.rowoffs + editor.rows-1; else if (c == PG_DN) {
editor.cury = editor.rowoffs + editor.rows - 1;
if (editor.cury > editor.nrows) { if (editor.cury > editor.nrows) {
editor.cury = editor.nrows; editor.cury = editor.nrows;
} }
@@ -1167,7 +1482,6 @@ move_cursor(int16_t c)
} }
break; break;
}
case HOME_KEY: case HOME_KEY:
case CTRL_KEY('a'): case CTRL_KEY('a'):
@@ -1185,8 +1499,7 @@ move_cursor(int16_t c)
} }
row = (editor.cury >= editor.nrows) ? row = (editor.cury >= editor.nrows) ? NULL : &editor.row[editor.cury];
NULL : &editor.row[editor.cury];
reps = row ? row->size : 0; reps = row ? row->size : 0;
if (editor.curx > reps) { if (editor.curx > reps) {
editor.curx = reps; editor.curx = reps;
@@ -1213,6 +1526,8 @@ newline(void)
editor.cury++; editor.cury++;
editor.curx = 0; editor.curx = 0;
/* Any insertion breaks a delete sequence for killring chaining. */
editor.killing = 0;
} }
@@ -1239,9 +1554,15 @@ process_kcommand(int16_t c)
delete_row(editor.cury); delete_row(editor.cury);
break; break;
case 'd': case 'd':
if (editor.curx == 0 && cursor_at_eol()) {
delete_row(editor.cury);
return;
}
while ((editor.row[editor.cury].size - editor.curx) > 0) { while ((editor.row[editor.cury].size - editor.curx) > 0) {
process_normal(DEL_KEY); process_normal(DEL_KEY);
} }
break; break;
case 'g': case 'g':
case CTRL_KEY('g'): case CTRL_KEY('g'):
@@ -1258,6 +1579,11 @@ process_kcommand(int16_t c)
abort(); abort();
case 'e': case 'e':
case CTRL_KEY('e'): case CTRL_KEY('e'):
if (editor.dirty && editor.dirtyex) {
editor_set_status("File not saved - C-k e again to open a new file anyways.");
editor.dirtyex = 0;
return;
}
editor_openfile(); editor_openfile();
break; break;
case 'f': case 'f':
@@ -1270,6 +1596,12 @@ process_kcommand(int16_t c)
editor_set_status("make: ok"); editor_set_status("make: ok");
} }
break; break;
case 'y':
killring_yank();
break;
default:
editor_set_status("unknown kcommand: %04x", c);
return;
} }
editor.dirtyex = 1; editor.dirtyex = 1;
@@ -1281,6 +1613,8 @@ void
process_normal(int16_t c) process_normal(int16_t c)
{ {
if (is_arrow_key(c)) { if (is_arrow_key(c)) {
/* moving the cursor breaks a delete sequence */
editor.killing = 0;
move_cursor(c); move_cursor(c);
return; return;
} }
@@ -1298,9 +1632,11 @@ process_normal(int16_t c)
case DEL_KEY: case DEL_KEY:
if (c == DEL_KEY || c == CTRL_KEY('d')) { if (c == DEL_KEY || c == CTRL_KEY('d')) {
move_cursor(ARROW_RIGHT); move_cursor(ARROW_RIGHT);
deletech(KILLRING_APPEND);
}
else {
deletech(KILLRING_PREPEND);
} }
deletech();
break; break;
case CTRL_KEY('l'): case CTRL_KEY('l'):
display_refresh(); display_refresh();
@@ -1312,7 +1648,8 @@ process_normal(int16_t c)
editor.mode = MODE_ESCAPE; editor.mode = MODE_ESCAPE;
break; break;
default: default:
if (isprint(c) || c == TAB_KEY) { /* Insert any printable byte: ASCII 0x200x7E and all bytes >=0x80. */
if ((c == TAB_KEY) || (c >= 0x20 && c != 0x7f)) {
insertch(c); insertch(c);
} }
break; break;
@@ -1662,6 +1999,9 @@ loop(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
// Set locale for proper UTF-8 handling
setlocale(LC_ALL, "");
setup_terminal(); setup_terminal();
init_editor(); init_editor();

15
test.txt.bak Normal file
View File

@@ -0,0 +1,15 @@
hello
world it is me
but I am not me