From 1961c052c85394d1dfb5d2d98d31f154757c8d08 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Thu, 27 Feb 2020 11:50:45 +0100 Subject: [PATCH] tweaks: simplify the undoing and redoing of an I don't know what I was thinking three years ago with this convoluted 'from_x' and 'to_x'. When undoing an , the cursor always needs to be placed back at the 'head' point. And the data always needs to be copied starting from 'tail_x' (skipping any whitespace that autoindent might have added). When redoing an , there is no need to reallocate and copy the data of the line, it is enough to just clip it at the original cursor position. (This wastes some memory, but... how often does one redo an ? So, conciseness of the code is preferable.) --- src/text.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/text.c b/src/text.c index 55993b20..edaaaa2a 100644 --- a/src/text.c +++ b/src/text.c @@ -516,7 +516,6 @@ void do_undo(void) linestruct *f = NULL, *t = NULL; linestruct *oldcutbuffer; char *data, *undidmsg = NULL; - size_t from_x, to_x; if (u == NULL) { statusbar(_("Nothing to undo")); @@ -544,14 +543,12 @@ void do_undo(void) break; case ENTER: undidmsg = _("line break"); - from_x = (u->head_x == 0) ? 0 : u->tail_x; - to_x = (u->head_x == 0) ? u->tail_x : u->head_x; f->data = charealloc(f->data, strlen(f->data) + - strlen(&u->strdata[from_x]) + 1); - strcat(f->data, &u->strdata[from_x]); + strlen(&u->strdata[u->tail_x]) + 1); + strcat(f->data, &u->strdata[u->tail_x]); unlink_node(f->next); renumber_from(f); - goto_line_posx(u->head_lineno, to_x); + goto_line_posx(u->head_lineno, u->head_x); break; case BACK: case DEL: @@ -718,11 +715,9 @@ void do_redo(void) break; case ENTER: redidmsg = _("line break"); + f->data[u->head_x] = '\0'; shoveline = make_new_node(f); shoveline->data = copy_of(u->strdata); - data = measured_copy(f->data, u->head_x); - free(f->data); - f->data = data; splice_node(f, shoveline); renumber_from(shoveline); goto_line_posx(u->head_lineno + 1, u->tail_x);