From 8f761120849447cd5045b0e09aa5830996826fdc Mon Sep 17 00:00:00 2001
From: Chris Allegretta <chrisa@asty.org>
Date: Fri, 1 Aug 2008 06:52:15 +0000
Subject: [PATCH] - When undoing a cut, set cutbuffer to NULL after freeing the
 current one - When updating a normal consecutive ^K cut, bail from add_undo
 since it's not an add, and in update_undo free the old cutbuffer before
 copying it again.

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@4287 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 src/text.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/text.c b/src/text.c
index c0629a8f..d70060b9 100644
--- a/src/text.c
+++ b/src/text.c
@@ -451,6 +451,7 @@ void do_undo(void)
 	    do_gotolinecolumn(u->lineno, u->begin+1, FALSE, FALSE, FALSE, FALSE);
 	do_uncut_text();
 	free_filestruct(cutbuffer);
+	cutbuffer = NULL;
 	break;
     default:
 	undidmsg = _("wtf?");
@@ -731,9 +732,15 @@ bool execute_command(const char *command)
 /* Add a new undo struct to the top of the current pile */
 void add_undo(undo_type current_action, openfilestruct *fs)
 {
-    undo *u = nmalloc(sizeof(undo));
+    undo *u;
     char *data;
 
+    /* Ugh, if we were called while cutting not-to-end, non-marked and on the same lineno,
+       we need to  abort here */
+    u = fs->current_undo;
+    if (u && u->type == CUT && !u->mark_set && u->lineno == fs->current->lineno)
+	return;
+
     /* Blow away the old undo stack if we are starting from the middle */
     while (fs->undotop != NULL && fs->undotop != fs->current_undo) {
 	undo *u2 = fs->undotop;
@@ -748,6 +755,8 @@ void add_undo(undo_type current_action, openfilestruct *fs)
 	free(u2);
     }
 
+    /* Allocate and initialize a new undo type */
+    u = nmalloc(sizeof(undo));
     u->type = current_action;
     u->lineno = fs->current->lineno;
     u->begin = fs->current_x;
@@ -895,6 +904,8 @@ void update_undo(undo_type action, openfilestruct *fs)
     case CUT:
     case CUTTOEND:
     case UNCUT:
+	if (u->cutbuffer)
+	    free(u->cutbuffer);
 	u->cutbuffer = copy_filestruct(cutbuffer);
 	u->cutbottom = cutbottom;
 	break;
-- 
GitLab