working on delete-region

This commit is contained in:
2025-11-24 21:14:56 -08:00
parent 9434a34116
commit af8dcb847e

81
main.c
View File

@@ -351,6 +351,15 @@ nibble_to_hex(char c)
} }
void
swap_int(int *a, int *b)
{
*a ^= *b;
*b ^= *a;
*a ^= *b;
}
int int
erow_render_to_cursor(struct erow *row, int cx) erow_render_to_cursor(struct erow *row, int cx)
{ {
@@ -700,11 +709,48 @@ cursor_after_mark(void)
} }
int
count_chars_from_cursor_to_mark(void)
{
int count = 0;
int curx = editor.curx;
int cury = editor.cury;
int markx = editor.mark_curx;
int marky = editor.mark_cury;
if (!cursor_after_mark()) {
swap_int(&curx, &markx);
swap_int(&curx, &marky);
}
editor.curx = markx;
editor.cury = marky;
while (editor.cury != cury) {
while (!cursor_at_eol()) {
move_cursor(ARROW_RIGHT);
count++;
}
count++;
}
while (editor.curx != curx) {
count++;
move_cursor(ARROW_RIGHT);
}
return count;
}
void void
kill_region(void) kill_region(void)
{ {
int curx = editor.curx; int curx = editor.curx;
int cury = editor.cury; int cury = editor.cury;
int markx = editor.mark_curx;
int marky = editor.mark_cury;
if (!editor.mark_set) { if (!editor.mark_set) {
return; return;
@@ -713,15 +759,14 @@ kill_region(void)
/* kill the current killring */ /* kill the current killring */
killring_flush(); killring_flush();
if (!cursor_after_mark()) { if (!cursor_after_mark()) {
/* gotta flex sometimes */ swap_int(&curx, &markx);
curx ^= cury; swap_int(&curx, &marky);
cury ^= curx;
cury ^= curx;
} }
editor.curx = editor.mark_curx; editor.curx = markx;
editor.cury = editor.mark_cury; editor.cury = marky;
while (editor.cury != cury) { while (editor.cury != cury) {
while (!cursor_at_eol()) { while (!cursor_at_eol()) {
@@ -742,6 +787,26 @@ kill_region(void)
} }
/* call after kill_region */
void
delete_region(void)
{
int count = count_chars_from_cursor_to_mark();
int killed = 0;
if (!editor.mark_set) {
return;
}
while (killed < count) {
move_cursor(ARROW_LEFT);
deletech(KILLRING_NO_OP);
killed++;
}
}
void void
die(const char *s) die(const char *s)
{ {
@@ -1753,6 +1818,10 @@ process_normal(int16_t c)
case CTRL_KEY('s'): case CTRL_KEY('s'):
editor_find(); editor_find();
break; break;
case CTRL_KEY('w'):
kill_region();
delete_region();
break;
case CTRL_KEY('y'): case CTRL_KEY('y'):
killring_yank(); killring_yank();
break; break;