tweaks: simplify the undoing and redoing of an <Enter>

I don't know what I was thinking three years ago with this convoluted
'from_x' and 'to_x'.  When undoing an <Enter>, 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 <Enter>, 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 <Enter>?  So, conciseness of the code is preferable.)
master
Benno Schulenberg 2020-02-27 11:50:45 +01:00
parent 843dbd56ef
commit 1961c052c8
1 changed files with 4 additions and 9 deletions

View File

@ -516,7 +516,6 @@ void do_undo(void)
linestruct *f = NULL, *t = NULL; linestruct *f = NULL, *t = NULL;
linestruct *oldcutbuffer; linestruct *oldcutbuffer;
char *data, *undidmsg = NULL; char *data, *undidmsg = NULL;
size_t from_x, to_x;
if (u == NULL) { if (u == NULL) {
statusbar(_("Nothing to undo")); statusbar(_("Nothing to undo"));
@ -544,14 +543,12 @@ void do_undo(void)
break; break;
case ENTER: case ENTER:
undidmsg = _("line break"); 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) + f->data = charealloc(f->data, strlen(f->data) +
strlen(&u->strdata[from_x]) + 1); strlen(&u->strdata[u->tail_x]) + 1);
strcat(f->data, &u->strdata[from_x]); strcat(f->data, &u->strdata[u->tail_x]);
unlink_node(f->next); unlink_node(f->next);
renumber_from(f); renumber_from(f);
goto_line_posx(u->head_lineno, to_x); goto_line_posx(u->head_lineno, u->head_x);
break; break;
case BACK: case BACK:
case DEL: case DEL:
@ -718,11 +715,9 @@ void do_redo(void)
break; break;
case ENTER: case ENTER:
redidmsg = _("line break"); redidmsg = _("line break");
f->data[u->head_x] = '\0';
shoveline = make_new_node(f); shoveline = make_new_node(f);
shoveline->data = copy_of(u->strdata); shoveline->data = copy_of(u->strdata);
data = measured_copy(f->data, u->head_x);
free(f->data);
f->data = data;
splice_node(f, shoveline); splice_node(f, shoveline);
renumber_from(shoveline); renumber_from(shoveline);
goto_line_posx(u->head_lineno + 1, u->tail_x); goto_line_posx(u->head_lineno + 1, u->tail_x);