esc
Externally Scriptable Editor
git clone git://mccd.space/esc
| Log | Files | Refs | README |
commit fd4536628f126a57db6a451a12feb265047a1371 parent 510afa756f3e66e75d3a4423a4b20addf1071a5a Author: Marc Coquand <marc@coquand.email> Date: Thu, 19 Feb 2026 15:06:32 +0100 * Diffstat:
| M | editor.c | | | 7 | +++++-- |
| M | editor.h | | | 2 | +- |
| M | main.c | | | 2 | +- |
3 files changed, 7 insertions(+), 4 deletions(-)
diff --git a/editor.c b/editor.c
@@ -20,7 +20,10 @@ void editor_destroy(Editor *ed) {
free(ed);
}
-void editor_insert_text(Editor *ed, const char *text) {
+void editor_insert_text(Editor *ed, const char *text,bool replace) {
+ if (replace && ed->cursor_idx != ed->selection_anchor) {
+ editor_delete_range(ed, ed->cursor_idx, ed->selection_anchor) ;
+ }
size_t input_len = strlen(text);
if (ed->length + input_len + 1 > ed->capacity) {
ed->capacity *= 2;
@@ -34,7 +37,7 @@ void editor_insert_text(Editor *ed, const char *text) {
}
void editor_newline(Editor *ed) {
- editor_insert_text(ed, "\n");
+ editor_insert_text(ed, "\n", true);
}
void editor_delete_range(Editor *ed, int start, int end) {
diff --git a/editor.h b/editor.h
@@ -21,7 +21,7 @@ Editor* editor_create(int char_width, float line_height);
void editor_destroy(Editor *ed);
// Core Actions
-void editor_insert_text(Editor *ed, const char *text);
+void editor_insert_text(Editor *ed, const char *text, bool replace);
void editor_newline(Editor *ed);
void editor_delete_back(Editor *ed);
void editor_delete_forward(Editor *ed);
diff --git a/main.c b/main.c
@@ -159,7 +159,7 @@ int main(int argc, char *argv[]) {
break;
case SDL_EVENT_TEXT_INPUT:
- editor_insert_text(ed, event.text.text);
+ editor_insert_text(ed, event.text.text, true);
break;
}
} while (SDL_PollEvent(&event));