From d13b6d68962409fd058d1e63fc009b7f1644805a Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Thu, 13 Feb 2020 18:47:29 +0100 Subject: [PATCH] tweaks: elide an intermediate copy of an added character In theory this would allow injecting more than one character at a time into the edit buffer. But the creation and updating of an undo item for the addition or the deletion of a character are a bit strange. For all other operations and add_undo() is called before the operation, and an update_undo() afterward. But for an ADD, the add_undo() is called after the operation, and for a DEL/BACK, the update_undo() is called before the operation. There is some logic to that, but things would be easier to understand if all operations were handled the same: an add_undo() beforehand (when needed), and an update_undo() afterward. --- src/text.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/text.c b/src/text.c index a2109be4..60eaab71 100644 --- a/src/text.c +++ b/src/text.c @@ -1290,8 +1290,8 @@ void update_multiline_undo(ssize_t lineno, char *indentation) void update_undo(undo_type action) { undostruct *u = openfile->undotop; - char *char_buf, *textposition; - size_t datalen; + size_t datalen, newlen; + char *textposition; int charlen; if (u->type != action) @@ -1301,11 +1301,10 @@ void update_undo(undo_type action) switch (u->type) { case ADD: - char_buf = charalloc(MAXCHARLEN); - charlen = collect_char(&openfile->current->data[u->mark_begin_x], - char_buf); - u->strdata = addstrings(u->strdata, u->strdata ? strlen(u->strdata) : 0, - char_buf, charlen); + newlen = openfile->current_x - u->begin; + u->strdata = charealloc(u->strdata, newlen + 1); + strncpy(u->strdata, openfile->current->data + u->begin, newlen); + u->strdata[newlen] = '\0'; u->mark_begin_lineno = openfile->current->lineno; u->mark_begin_x = openfile->current_x; break;