delete region works.

This commit is contained in:
2025-11-25 22:31:03 -08:00
parent 75b19042b9
commit 12553f301b

268
main.c
View File

@@ -193,7 +193,7 @@ void editor_find_callback(char* query, int16_t c);
void editor_find(void); void editor_find(void);
char *editor_prompt(char*, void (*cb)(char*, int16_t)); char *editor_prompt(char*, void (*cb)(char*, int16_t));
void editor_openfile(void); void editor_openfile(void);
void move_cursor(int16_t c); void move_cursor(int16_t c, int interactive);
void newline(void); void newline(void);
void process_kcommand(int16_t c); void process_kcommand(int16_t c);
void process_normal(int16_t c); void process_normal(int16_t c);
@@ -222,8 +222,8 @@ static void install_signal_handlers(void);
* Find the first occurrence of find in s, where the search is limited to the * Find the first occurrence of find in s, where the search is limited to the
* first slen characters of s. * first slen characters of s.
*/ */
char* char
strnstr(const char* s, const char* find, size_t slen) *strnstr(const char *s, const char *find, size_t slen)
{ {
char c, sc; char c, sc;
size_t len; size_t len;
@@ -797,17 +797,17 @@ count_chars_from_cursor_to_mark(void)
while (editor.cury != cury) { while (editor.cury != cury) {
while (!cursor_at_eol()) { while (!cursor_at_eol()) {
move_cursor(ARROW_RIGHT); move_cursor(ARROW_RIGHT, 1);
count++; count++;
} }
move_cursor(ARROW_RIGHT); move_cursor(ARROW_RIGHT, 1);
count++; count++;
} }
while (editor.curx != curx) { while (editor.curx != curx) {
count++; count++;
move_cursor(ARROW_RIGHT); move_cursor(ARROW_RIGHT, 1);
} }
return count; return count;
@@ -840,15 +840,15 @@ kill_region(void)
while (editor.cury != cury) { while (editor.cury != cury) {
while (!cursor_at_eol()) { while (!cursor_at_eol()) {
killring_append_char(editor.row[editor.cury].line[editor.curx]); killring_append_char(editor.row[editor.cury].line[editor.curx]);
move_cursor(ARROW_RIGHT); move_cursor(ARROW_RIGHT, 0);
} }
killring_append_char('\n'); killring_append_char('\n');
move_cursor(ARROW_RIGHT); move_cursor(ARROW_RIGHT, 0);
} }
while (editor.curx != curx) { while (editor.curx != curx) {
killring_append_char(editor.row[editor.cury].line[editor.curx]); killring_append_char(editor.row[editor.cury].line[editor.curx]);
move_cursor(ARROW_RIGHT); move_cursor(ARROW_RIGHT, 0);
} }
editor_set_status("Region killed."); editor_set_status("Region killed.");
@@ -925,7 +925,7 @@ delete_region(void)
jump_to_position(markx, marky); jump_to_position(markx, marky);
while (killed < count) { while (killed < count) {
// move_cursor(ARROW_RIGHT); move_cursor(ARROW_RIGHT, 0);
deletech(KILLRING_NO_OP); deletech(KILLRING_NO_OP);
killed++; killed++;
} }
@@ -1051,13 +1051,13 @@ void
find_next_word(void) find_next_word(void)
{ {
while (cursor_at_eol()) { while (cursor_at_eol()) {
move_cursor(ARROW_RIGHT); move_cursor(ARROW_RIGHT, 1);
} }
if (isalnum(editor.row[editor.cury].line[editor.curx])) { if (isalnum(editor.row[editor.cury].line[editor.curx])) {
while (!isspace(editor.row[editor.cury].line[editor.curx]) && ! while (!isspace(editor.row[editor.cury].line[editor.curx]) && !
cursor_at_eol()) { cursor_at_eol()) {
move_cursor(ARROW_RIGHT); move_cursor(ARROW_RIGHT, 1);
} }
return; return;
@@ -1065,7 +1065,7 @@ find_next_word(void)
if (isspace(editor.row[editor.cury].line[editor.curx])) { if (isspace(editor.row[editor.cury].line[editor.curx])) {
while (isspace(editor.row[editor.cury].line[editor.curx])) { while (isspace(editor.row[editor.cury].line[editor.curx])) {
move_cursor(ARROW_RIGHT); move_cursor(ARROW_RIGHT, 1);
} }
find_next_word(); find_next_word();
@@ -1077,14 +1077,14 @@ void
delete_next_word(void) delete_next_word(void)
{ {
while (cursor_at_eol()) { while (cursor_at_eol()) {
move_cursor(ARROW_RIGHT); move_cursor(ARROW_RIGHT, 1);
deletech(KILLRING_APPEND); deletech(KILLRING_APPEND);
} }
if (isalnum(editor.row[editor.cury].line[editor.curx])) { if (isalnum(editor.row[editor.cury].line[editor.curx])) {
while (!isspace(editor.row[editor.cury].line[editor.curx]) && ! while (!isspace(editor.row[editor.cury].line[editor.curx]) && !
cursor_at_eol()) { cursor_at_eol()) {
move_cursor(ARROW_RIGHT); move_cursor(ARROW_RIGHT, 1);
deletech(KILLRING_APPEND); deletech(KILLRING_APPEND);
} }
@@ -1093,7 +1093,7 @@ delete_next_word(void)
if (isspace(editor.row[editor.cury].line[editor.curx])) { if (isspace(editor.row[editor.cury].line[editor.curx])) {
while (isspace(editor.row[editor.cury].line[editor.curx])) { while (isspace(editor.row[editor.cury].line[editor.curx])) {
move_cursor(ARROW_RIGHT); move_cursor(ARROW_RIGHT, 1);
deletech(KILLRING_APPEND); deletech(KILLRING_APPEND);
} }
@@ -1109,7 +1109,7 @@ find_prev_word(void)
return; return;
} }
move_cursor(ARROW_LEFT); move_cursor(ARROW_LEFT, 1);
while (cursor_at_eol() || isspace( while (cursor_at_eol() || isspace(
editor.row[editor.cury].line[editor.curx])) { editor.row[editor.cury].line[editor.curx])) {
@@ -1117,12 +1117,12 @@ find_prev_word(void)
return; return;
} }
move_cursor(ARROW_LEFT); move_cursor(ARROW_LEFT, 1);
} }
while (editor.curx > 0 && !isspace( while (editor.curx > 0 && !isspace(
editor.row[editor.cury].line[editor.curx - 1])) { editor.row[editor.cury].line[editor.curx - 1])) {
move_cursor(ARROW_LEFT); move_cursor(ARROW_LEFT, 1);
} }
} }
@@ -1270,8 +1270,9 @@ insertch(int16_t c)
erow_insert(editor.nrows, "", 0); erow_insert(editor.nrows, "", 0);
} }
/* Any insertion breaks a delete sequence for killring chaining. */ /* Inserting ends kill ring chaining. */
editor.kill = 0; editor.kill = 0;
/* Ensure we pass a non-negative byte value to avoid assert(c > 0). */ /* Ensure we pass a non-negative byte value to avoid assert(c > 0). */
row_insert_ch(&editor.row[editor.cury], row_insert_ch(&editor.row[editor.cury],
editor.curx, editor.curx,
@@ -1402,8 +1403,8 @@ open_file(const char *filename)
/* /*
* convert our rows to a buffer; caller must free it. * convert our rows to a buffer; caller must free it.
*/ */
char* char
rows_to_buffer(int* buflen) *rows_to_buffer(int *buflen)
{ {
int len = 0; int len = 0;
int j; int j;
@@ -1601,8 +1602,8 @@ 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);
@@ -1655,72 +1656,164 @@ editor_prompt(char* prompt, void (*cb)(char*, int16_t))
} }
// void
// editor_find_callback(char *query, int16_t c)
// {
// static int lmatch = -1;
// static int dir = -1;
// int i, current, traversed = 0;
// int qlen = kstrnlen(query, 128);
// char *match;
// struct erow *row;
//
// if (c == '\r' || c == ESC_KEY || c == CTRL_KEY('g')) {
// /* reset search */
// lmatch = -1;
// dir = 1;
// return;
// } else if (c == ARROW_RIGHT || c == ARROW_DOWN || c == CTRL_KEY('s')) {
// dir = 1;
// } else if (c == ARROW_LEFT || c == ARROW_UP) {
// dir = -1;
// } else {
// lmatch = -1;
// dir = 1;
// }
//
// if (lmatch == -1) {
// dir = 1;
// current = editor.cury;
// }
// current = lmatch;
//
// for (i = 0; i < editor.nrows; i++) {
// traversed++;
// if (traversed >= editor.nrows) {
// break;
// }
//
// current += dir;
// if (current < 0) {
// current = editor.nrows - 1;
// } else if (current >= editor.nrows) {
// current = 0;
// }
//
// row = &editor.row[current];
// if (row == NULL || row->size < qlen) {
// continue;
// }
//
// match = strnstr(row->render, query, row->size);
// if (match) {
// lmatch = current;
// editor.cury = current;
// editor.curx = erow_render_to_cursor(row,
// match - row->render);
// if (editor.curx > row->size) {
// editor.curx = row->size;
// }
// /*
// * after this, scroll will put the match line at
// * the top of the screen.
// */
// editor.rowoffs = editor.nrows;
// break;
// }
// }
//
// display_refresh();
// }
void void
editor_find_callback(char* query, int16_t c) editor_find_callback(char* query, int16_t c)
{ {
static int lmatch = -1; static int last_match = -1; /* row index of last match */
static int dir = -1; static int direction = 1; /* 1 = forward, -1 = backward */
int i, current, traversed = 0; static char last_query[128] = {0}; /* remember last successful query */
int qlen = kstrnlen(query, 128);
char* match;
struct erow *row; struct erow *row;
int saved_cx = editor.curx;
int saved_cy = editor.cury;
size_t qlen = strlen(query);
char *match;
int i;
if (c == '\r' || c == ESC_KEY || c == CTRL_KEY('g')) { if (c == '\r' || c == ESC_KEY || c == CTRL_KEY('g')) {
/* reset search */ last_match = -1;
lmatch = -1; direction = 1;
dir = 1; last_query[0] = '\0';
return; return;
} else if (c == ARROW_RIGHT || c == ARROW_DOWN || c == CTRL_KEY('s')) {
dir = 1;
} else if (c == ARROW_LEFT || c == ARROW_UP) {
dir = -1;
} else {
lmatch = -1;
dir = 1;
} }
if (lmatch == -1) { if (c == CTRL_KEY('s') || c == ARROW_DOWN || c == ARROW_RIGHT) {
dir = 1; direction = 1;
current = editor.cury; } else if (c == CTRL_KEY('r') || c == ARROW_UP || c == ARROW_LEFT) {
direction = -1;
} }
current = lmatch;
if (qlen > 0 && (qlen != strlen(last_query) || strcmp(query, last_query) != 0)) {
last_match = -1;
strcpy(last_query, query);
}
int start_row = editor.cury;
int start_col = editor.curx;
if (last_match == -1) {
if (direction == 1) {
start_col += 1;
}
last_match = editor.cury;
}
int current = last_match;
int wrapped = 0;
for (i = 0; i < editor.nrows; i++) { for (i = 0; i < editor.nrows; i++) {
traversed++; current += direction;
if (traversed >= editor.nrows) {
break;
}
current += dir; if (current >= editor.nrows) {
current = 0;
if (wrapped++) break;
}
if (current < 0) { if (current < 0) {
current = editor.nrows - 1; current = editor.nrows - 1;
} else if (current >= editor.nrows) { if (wrapped++) break;
current = 0;
} }
row = &editor.row[current]; row = &editor.row[current];
if (row == NULL || row->size < qlen) {
continue; /* Skip rendering search on raw bytes — use line[] but respect render offsets */
erow_update(row);
char* search_start = row->render;
if (current == start_row && direction == 1 && last_match == -1) {
/* On first search forward: skip text before cursor */
int skip = erow_render_to_cursor(row, start_col);
search_start += skip;
} }
match = strnstr(row->render, query, row->size); match = strnstr(search_start, query, row->rsize - (search_start - row->render));
if (match) { if (match) {
lmatch = current; last_match = current;
editor.cury = current; editor.cury = current;
editor.curx = erow_render_to_cursor(row, editor.curx = erow_cursor_to_render(row, match - row->render);
match - row->render); if (current == start_row && direction == 1 && last_match == -1) {
if (editor.curx > row->size) { editor.curx += start_col; /* adjust if we skipped prefix */
editor.curx = row->size;
} }
/* scroll();
* after this, scroll will put the match line at display_refresh();
* the top of the screen. return;
*/
editor.rowoffs = editor.nrows;
break;
} }
} }
/* No match found */
if (qlen > 0) {
editor_set_status("Failing search: %s", query);
}
editor.curx = saved_cx;
editor.cury = saved_cy;
display_refresh(); display_refresh();
} }
@@ -1779,21 +1872,25 @@ first_nonwhitespace(struct erow* row)
} }
memset(&state, 0, sizeof(state)); memset(&state, 0, sizeof(state));
pos = 0; pos = editor.curx;
if (pos > row->size) {
pos = row->size;
}
while (pos < row->size) { while (pos < row->size) {
len = mbrtowc(&wc, &row->line[pos], row->size - pos, &state); len = mbrtowc(&wc, &row->line[pos], row->size - pos, &state);
if (len == (size_t)-1 || len == (size_t)-2) { if (len == (size_t)-1 || len == (size_t)-2) {
/* Invalid or incomplete sequence, stop here */
break; break;
} }
if (len == 0) { if (len == 0) {
/* Null character, stop here */
break; break;
} }
if (!iswspace(wc)) { if (!iswspace(wc)) {
/* Found non-whitespace character */
break; break;
} }
pos += len; pos += len;
} }
@@ -1802,7 +1899,7 @@ first_nonwhitespace(struct erow* row)
void void
move_cursor(int16_t c) move_cursor(int16_t c, int interactive)
{ {
struct erow *row; struct erow *row;
int reps; int reps;
@@ -1817,8 +1914,10 @@ move_cursor(int16_t c)
row = (editor.cury >= editor.nrows) row = (editor.cury >= editor.nrows)
? NULL ? NULL
: &editor.row[editor.cury]; : &editor.row[editor.cury];
if (interactive) {
editor.curx = first_nonwhitespace(row); editor.curx = first_nonwhitespace(row);
} }
}
break; break;
case ARROW_DOWN: case ARROW_DOWN:
case CTRL_KEY('n'): case CTRL_KEY('n'):
@@ -1827,12 +1926,19 @@ move_cursor(int16_t c)
row = (editor.cury >= editor.nrows) row = (editor.cury >= editor.nrows)
? NULL ? NULL
: &editor.row[editor.cury]; : &editor.row[editor.cury];
if (interactive) {
editor.curx = first_nonwhitespace(row); editor.curx = first_nonwhitespace(row);
} }
}
break; break;
case ARROW_RIGHT: case ARROW_RIGHT:
case CTRL_KEY('f'): case CTRL_KEY('f'):
if (row && editor.curx < row->size) { if (!row) {
break;
}
if (editor.curx < row->size) {
editor.curx++; editor.curx++;
/* skip over UTF-8 continuation bytes */ /* skip over UTF-8 continuation bytes */
while (editor.curx < row->size && while (editor.curx < row->size &&
@@ -1840,19 +1946,15 @@ move_cursor(int16_t c)
0xC0) == 0x80) { 0xC0) == 0x80) {
editor.curx++; editor.curx++;
} }
} else if (row && editor.curx == row->size && editor.cury < editor.nrows - 1) { } else if (editor.curx == row->size && editor.cury < editor.nrows - 1) {
editor.cury++; editor.cury++;
row = (editor.cury >= editor.nrows) editor.curx = 0;
? NULL
: &editor.row[editor.cury];
editor.curx = first_nonwhitespace(row);
} }
break; break;
case ARROW_LEFT: case ARROW_LEFT:
case CTRL_KEY('b'): case CTRL_KEY('b'):
if (editor.curx > 0) { if (editor.curx > 0) {
editor.curx--; editor.curx--;
/* move to the start byte if we landed on a continuation */
while (editor.curx > 0 && while (editor.curx > 0 &&
((unsigned char)row->line[editor.curx] & ((unsigned char)row->line[editor.curx] &
0xC0) == 0x80) { 0xC0) == 0x80) {
@@ -1861,7 +1963,7 @@ move_cursor(int16_t c)
} else if (editor.cury > 0) { } 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]; row = &editor.row[editor.cury];
while (editor.curx > 0 && while (editor.curx > 0 &&
((unsigned char)row->line[editor.curx] & ((unsigned char)row->line[editor.curx] &
@@ -1883,7 +1985,7 @@ move_cursor(int16_t c)
reps = editor.rows; reps = editor.rows;
while (--reps) { while (--reps) {
move_cursor(c == PG_UP ? ARROW_UP : ARROW_DOWN); move_cursor(c == PG_UP ? ARROW_UP : ARROW_DOWN, 1);
} }
break; break;
@@ -1940,8 +2042,8 @@ newline(void)
} }
char* char
get_cloc_code_lines(const char* filename) *get_cloc_code_lines(const char *filename)
{ {
char command[512]; char command[512];
char buffer[256]; char buffer[256];
@@ -2197,7 +2299,7 @@ process_normal(int16_t c)
if (is_arrow_key(c)) { if (is_arrow_key(c)) {
/* moving the cursor breaks a delete sequence */ /* moving the cursor breaks a delete sequence */
editor.kill = 0; editor.kill = 0;
move_cursor(c); move_cursor(c, 1);
return; return;
} }
@@ -2213,7 +2315,7 @@ process_normal(int16_t c)
case CTRL_KEY('d'): case CTRL_KEY('d'):
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, 1);
deletech(KILLRING_APPEND); deletech(KILLRING_APPEND);
} else { } else {
deletech(KILLRING_PREPEND); deletech(KILLRING_PREPEND);
@@ -2401,7 +2503,7 @@ setup_terminal(void)
if (tcgetattr(STDIN_FILENO, &editor.entry_term) == -1) { if (tcgetattr(STDIN_FILENO, &editor.entry_term) == -1) {
die("can't snapshot terminal settings"); die("can't snapshot terminal settings");
} }
atexit(disable_termraw);
enable_termraw(); enable_termraw();
} }