tweaks: extend the undo data for deleting and backspacing more directly

To get rid of an intermediate copy of the deleted character.
master
Benno Schulenberg 2020-02-13 17:25:27 +01:00
parent a65982bffb
commit 819066c5d5
1 changed files with 11 additions and 8 deletions

View File

@ -1290,7 +1290,8 @@ void update_multiline_undo(ssize_t lineno, char *indentation)
void update_undo(undo_type action) void update_undo(undo_type action)
{ {
undostruct *u = openfile->undotop; undostruct *u = openfile->undotop;
char *char_buf; char *char_buf, *textposition;
size_t datalen;
int charlen; int charlen;
if (u->type != action) if (u->type != action)
@ -1314,22 +1315,24 @@ void update_undo(undo_type action)
break; break;
case BACK: case BACK:
case DEL: case DEL:
char_buf = charalloc(MAXCHARLEN); textposition = openfile->current->data + openfile->current_x;
charlen = collect_char(&openfile->current->data[openfile->current_x], charlen = char_length(textposition);
char_buf); datalen = strlen(u->strdata);
if (openfile->current_x == u->begin) { if (openfile->current_x == u->begin) {
/* They deleted more: add removed character after earlier stuff. */ /* They deleted more: add removed character after earlier stuff. */
u->strdata = addstrings(u->strdata, strlen(u->strdata), char_buf, charlen); u->strdata = charealloc(u->strdata, datalen + charlen + 1);
strncpy(u->strdata + datalen, textposition, charlen);
u->strdata[datalen + charlen] = '\0';
u->mark_begin_x = openfile->current_x; u->mark_begin_x = openfile->current_x;
} else if (openfile->current_x == u->begin - charlen) { } else if (openfile->current_x == u->begin - charlen) {
/* They backspaced further: add removed character before earlier. */ /* They backspaced further: add removed character before earlier. */
u->strdata = addstrings(char_buf, charlen, u->strdata, strlen(u->strdata)); u->strdata = charealloc(u->strdata, datalen + charlen + 1);
memmove(u->strdata + charlen, u->strdata, datalen + 1);
strncpy(u->strdata, textposition, charlen);
u->begin = openfile->current_x; u->begin = openfile->current_x;
} else { } else {
/* They deleted *elsewhere* on the line: start a new undo item. */ /* They deleted *elsewhere* on the line: start a new undo item. */
free(char_buf);
add_undo(u->type, NULL); add_undo(u->type, NULL);
return;
} }
break; break;
case JOIN: case JOIN: