delete region works.
This commit is contained in:
268
main.c
268
main.c
@@ -193,7 +193,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);
|
||||
void move_cursor(int16_t c);
|
||||
void move_cursor(int16_t c, int interactive);
|
||||
void newline(void);
|
||||
void process_kcommand(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
|
||||
* first slen characters of s.
|
||||
*/
|
||||
char*
|
||||
strnstr(const char* s, const char* find, size_t slen)
|
||||
char
|
||||
*strnstr(const char *s, const char *find, size_t slen)
|
||||
{
|
||||
char c, sc;
|
||||
size_t len;
|
||||
@@ -797,17 +797,17 @@ count_chars_from_cursor_to_mark(void)
|
||||
|
||||
while (editor.cury != cury) {
|
||||
while (!cursor_at_eol()) {
|
||||
move_cursor(ARROW_RIGHT);
|
||||
move_cursor(ARROW_RIGHT, 1);
|
||||
count++;
|
||||
}
|
||||
|
||||
move_cursor(ARROW_RIGHT);
|
||||
move_cursor(ARROW_RIGHT, 1);
|
||||
count++;
|
||||
}
|
||||
|
||||
while (editor.curx != curx) {
|
||||
count++;
|
||||
move_cursor(ARROW_RIGHT);
|
||||
move_cursor(ARROW_RIGHT, 1);
|
||||
}
|
||||
|
||||
return count;
|
||||
@@ -840,15 +840,15 @@ kill_region(void)
|
||||
while (editor.cury != cury) {
|
||||
while (!cursor_at_eol()) {
|
||||
killring_append_char(editor.row[editor.cury].line[editor.curx]);
|
||||
move_cursor(ARROW_RIGHT);
|
||||
move_cursor(ARROW_RIGHT, 0);
|
||||
}
|
||||
killring_append_char('\n');
|
||||
move_cursor(ARROW_RIGHT);
|
||||
move_cursor(ARROW_RIGHT, 0);
|
||||
}
|
||||
|
||||
while (editor.curx != 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.");
|
||||
@@ -925,7 +925,7 @@ delete_region(void)
|
||||
jump_to_position(markx, marky);
|
||||
|
||||
while (killed < count) {
|
||||
// move_cursor(ARROW_RIGHT);
|
||||
move_cursor(ARROW_RIGHT, 0);
|
||||
deletech(KILLRING_NO_OP);
|
||||
killed++;
|
||||
}
|
||||
@@ -1051,13 +1051,13 @@ void
|
||||
find_next_word(void)
|
||||
{
|
||||
while (cursor_at_eol()) {
|
||||
move_cursor(ARROW_RIGHT);
|
||||
move_cursor(ARROW_RIGHT, 1);
|
||||
}
|
||||
|
||||
if (isalnum(editor.row[editor.cury].line[editor.curx])) {
|
||||
while (!isspace(editor.row[editor.cury].line[editor.curx]) && !
|
||||
cursor_at_eol()) {
|
||||
move_cursor(ARROW_RIGHT);
|
||||
move_cursor(ARROW_RIGHT, 1);
|
||||
}
|
||||
|
||||
return;
|
||||
@@ -1065,7 +1065,7 @@ find_next_word(void)
|
||||
|
||||
if (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();
|
||||
@@ -1077,14 +1077,14 @@ void
|
||||
delete_next_word(void)
|
||||
{
|
||||
while (cursor_at_eol()) {
|
||||
move_cursor(ARROW_RIGHT);
|
||||
move_cursor(ARROW_RIGHT, 1);
|
||||
deletech(KILLRING_APPEND);
|
||||
}
|
||||
|
||||
if (isalnum(editor.row[editor.cury].line[editor.curx])) {
|
||||
while (!isspace(editor.row[editor.cury].line[editor.curx]) && !
|
||||
cursor_at_eol()) {
|
||||
move_cursor(ARROW_RIGHT);
|
||||
move_cursor(ARROW_RIGHT, 1);
|
||||
deletech(KILLRING_APPEND);
|
||||
}
|
||||
|
||||
@@ -1093,7 +1093,7 @@ delete_next_word(void)
|
||||
|
||||
if (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);
|
||||
}
|
||||
|
||||
@@ -1109,7 +1109,7 @@ find_prev_word(void)
|
||||
return;
|
||||
}
|
||||
|
||||
move_cursor(ARROW_LEFT);
|
||||
move_cursor(ARROW_LEFT, 1);
|
||||
|
||||
while (cursor_at_eol() || isspace(
|
||||
editor.row[editor.cury].line[editor.curx])) {
|
||||
@@ -1117,12 +1117,12 @@ find_prev_word(void)
|
||||
return;
|
||||
}
|
||||
|
||||
move_cursor(ARROW_LEFT);
|
||||
move_cursor(ARROW_LEFT, 1);
|
||||
}
|
||||
|
||||
while (editor.curx > 0 && !isspace(
|
||||
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);
|
||||
}
|
||||
|
||||
/* Any insertion breaks a delete sequence for killring chaining. */
|
||||
/* Inserting ends kill ring chaining. */
|
||||
editor.kill = 0;
|
||||
|
||||
/* Ensure we pass a non-negative byte value to avoid assert(c > 0). */
|
||||
row_insert_ch(&editor.row[editor.cury],
|
||||
editor.curx,
|
||||
@@ -1402,8 +1403,8 @@ open_file(const char *filename)
|
||||
/*
|
||||
* convert our rows to a buffer; caller must free it.
|
||||
*/
|
||||
char*
|
||||
rows_to_buffer(int* buflen)
|
||||
char
|
||||
*rows_to_buffer(int *buflen)
|
||||
{
|
||||
int len = 0;
|
||||
int j;
|
||||
@@ -1601,8 +1602,8 @@ get_keypress(void)
|
||||
}
|
||||
|
||||
|
||||
char*
|
||||
editor_prompt(char* prompt, void (*cb)(char*, int16_t))
|
||||
char
|
||||
*editor_prompt(char *prompt, void (*cb)(char*, int16_t))
|
||||
{
|
||||
size_t bufsz = 128;
|
||||
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
|
||||
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;
|
||||
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;
|
||||
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')) {
|
||||
/* reset search */
|
||||
lmatch = -1;
|
||||
dir = 1;
|
||||
last_match = -1;
|
||||
direction = 1;
|
||||
last_query[0] = '\0';
|
||||
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;
|
||||
if (c == CTRL_KEY('s') || c == ARROW_DOWN || c == ARROW_RIGHT) {
|
||||
direction = 1;
|
||||
} 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++) {
|
||||
traversed++;
|
||||
if (traversed >= editor.nrows) {
|
||||
break;
|
||||
}
|
||||
current += direction;
|
||||
|
||||
current += dir;
|
||||
if (current >= editor.nrows) {
|
||||
current = 0;
|
||||
if (wrapped++) break;
|
||||
}
|
||||
if (current < 0) {
|
||||
current = editor.nrows - 1;
|
||||
} else if (current >= editor.nrows) {
|
||||
current = 0;
|
||||
if (wrapped++) break;
|
||||
}
|
||||
|
||||
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) {
|
||||
lmatch = current;
|
||||
last_match = current;
|
||||
editor.cury = current;
|
||||
editor.curx = erow_render_to_cursor(row,
|
||||
match - row->render);
|
||||
if (editor.curx > row->size) {
|
||||
editor.curx = row->size;
|
||||
editor.curx = erow_cursor_to_render(row, match - row->render);
|
||||
if (current == start_row && direction == 1 && last_match == -1) {
|
||||
editor.curx += start_col; /* adjust if we skipped prefix */
|
||||
}
|
||||
/*
|
||||
* after this, scroll will put the match line at
|
||||
* the top of the screen.
|
||||
*/
|
||||
editor.rowoffs = editor.nrows;
|
||||
break;
|
||||
scroll();
|
||||
display_refresh();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* No match found */
|
||||
if (qlen > 0) {
|
||||
editor_set_status("Failing search: %s", query);
|
||||
}
|
||||
editor.curx = saved_cx;
|
||||
editor.cury = saved_cy;
|
||||
display_refresh();
|
||||
}
|
||||
|
||||
@@ -1779,21 +1872,25 @@ first_nonwhitespace(struct erow* row)
|
||||
}
|
||||
|
||||
memset(&state, 0, sizeof(state));
|
||||
pos = 0;
|
||||
pos = editor.curx;
|
||||
if (pos > row->size) {
|
||||
pos = row->size;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1802,7 +1899,7 @@ first_nonwhitespace(struct erow* row)
|
||||
|
||||
|
||||
void
|
||||
move_cursor(int16_t c)
|
||||
move_cursor(int16_t c, int interactive)
|
||||
{
|
||||
struct erow *row;
|
||||
int reps;
|
||||
@@ -1817,8 +1914,10 @@ move_cursor(int16_t c)
|
||||
row = (editor.cury >= editor.nrows)
|
||||
? NULL
|
||||
: &editor.row[editor.cury];
|
||||
if (interactive) {
|
||||
editor.curx = first_nonwhitespace(row);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ARROW_DOWN:
|
||||
case CTRL_KEY('n'):
|
||||
@@ -1827,12 +1926,19 @@ move_cursor(int16_t c)
|
||||
row = (editor.cury >= editor.nrows)
|
||||
? NULL
|
||||
: &editor.row[editor.cury];
|
||||
|
||||
if (interactive) {
|
||||
editor.curx = first_nonwhitespace(row);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ARROW_RIGHT:
|
||||
case CTRL_KEY('f'):
|
||||
if (row && editor.curx < row->size) {
|
||||
if (!row) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (editor.curx < row->size) {
|
||||
editor.curx++;
|
||||
/* skip over UTF-8 continuation bytes */
|
||||
while (editor.curx < row->size &&
|
||||
@@ -1840,19 +1946,15 @@ move_cursor(int16_t c)
|
||||
0xC0) == 0x80) {
|
||||
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++;
|
||||
row = (editor.cury >= editor.nrows)
|
||||
? NULL
|
||||
: &editor.row[editor.cury];
|
||||
editor.curx = first_nonwhitespace(row);
|
||||
editor.curx = 0;
|
||||
}
|
||||
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) {
|
||||
@@ -1861,7 +1963,7 @@ move_cursor(int16_t c)
|
||||
} 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] &
|
||||
@@ -1883,7 +1985,7 @@ move_cursor(int16_t c)
|
||||
|
||||
reps = editor.rows;
|
||||
while (--reps) {
|
||||
move_cursor(c == PG_UP ? ARROW_UP : ARROW_DOWN);
|
||||
move_cursor(c == PG_UP ? ARROW_UP : ARROW_DOWN, 1);
|
||||
}
|
||||
|
||||
break;
|
||||
@@ -1940,8 +2042,8 @@ newline(void)
|
||||
}
|
||||
|
||||
|
||||
char*
|
||||
get_cloc_code_lines(const char* filename)
|
||||
char
|
||||
*get_cloc_code_lines(const char *filename)
|
||||
{
|
||||
char command[512];
|
||||
char buffer[256];
|
||||
@@ -2197,7 +2299,7 @@ process_normal(int16_t c)
|
||||
if (is_arrow_key(c)) {
|
||||
/* moving the cursor breaks a delete sequence */
|
||||
editor.kill = 0;
|
||||
move_cursor(c);
|
||||
move_cursor(c, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -2213,7 +2315,7 @@ process_normal(int16_t c)
|
||||
case CTRL_KEY('d'):
|
||||
case DEL_KEY:
|
||||
if (c == DEL_KEY || c == CTRL_KEY('d')) {
|
||||
move_cursor(ARROW_RIGHT);
|
||||
move_cursor(ARROW_RIGHT, 1);
|
||||
deletech(KILLRING_APPEND);
|
||||
} else {
|
||||
deletech(KILLRING_PREPEND);
|
||||
@@ -2401,7 +2503,7 @@ setup_terminal(void)
|
||||
if (tcgetattr(STDIN_FILENO, &editor.entry_term) == -1) {
|
||||
die("can't snapshot terminal settings");
|
||||
}
|
||||
atexit(disable_termraw);
|
||||
|
||||
enable_termraw();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user