Commit c17a8a98 authored by Benno Schulenberg's avatar Benno Schulenberg
Browse files

undo: discarding the stack does not always lose information

When some or all edits have been undone, and the user starts to make
new edits, the old part of the undo stack is discarded, but this does
not mean that the undo stack doesn't go back to the very beginning.

This really fixes https://savannah.gnu.org/bugs/?52504.
Showing with 12 additions and 10 deletions
+12 -10
...@@ -523,7 +523,7 @@ void delete_opennode(openfilestruct *fileptr) ...@@ -523,7 +523,7 @@ void delete_opennode(openfilestruct *fileptr)
free(fileptr->current_stat); free(fileptr->current_stat);
free(fileptr->lock_filename); free(fileptr->lock_filename);
/* Free the undo stack. */ /* Free the undo stack. */
discard_until(NULL, fileptr); discard_until(NULL, fileptr, TRUE);
#endif #endif
free(fileptr); free(fileptr);
} }
......
...@@ -535,7 +535,7 @@ void do_enter(void); ...@@ -535,7 +535,7 @@ void do_enter(void);
#ifndef NANO_TINY #ifndef NANO_TINY
RETSIGTYPE cancel_command(int signal); RETSIGTYPE cancel_command(int signal);
bool execute_command(const char *command); bool execute_command(const char *command);
void discard_until(const undo *thisitem, openfilestruct *thefile); void discard_until(const undo *thisitem, openfilestruct *thefile, bool keep);
void add_undo(undo_type action); void add_undo(undo_type action);
void update_multiline_undo(ssize_t lineno, char *indentation); void update_multiline_undo(ssize_t lineno, char *indentation);
void update_undo(undo_type action); void update_undo(undo_type action);
......
...@@ -1186,8 +1186,9 @@ bool execute_command(const char *command) ...@@ -1186,8 +1186,9 @@ bool execute_command(const char *command)
return TRUE; return TRUE;
} }
/* Discard undo items that are newer than the given one, or all if NULL. */ /* Discard undo items that are newer than the given one, or all if NULL.
void discard_until(const undo *thisitem, openfilestruct *thefile) * When keep is TRUE, do not touch the pristine flag. */
void discard_until(const undo *thisitem, openfilestruct *thefile, bool keep)
{ {
undo *dropit = thefile->undotop; undo *dropit = thefile->undotop;
undo_group *group; undo_group *group;
...@@ -1214,8 +1215,9 @@ void discard_until(const undo *thisitem, openfilestruct *thefile) ...@@ -1214,8 +1215,9 @@ void discard_until(const undo *thisitem, openfilestruct *thefile)
/* Prevent a chain of editing actions from continuing. */ /* Prevent a chain of editing actions from continuing. */
thefile->last_action = OTHER; thefile->last_action = OTHER;
/* Record that the undo stack no longer goes back to the beginning. */ /* When requested, record that the undo stack was chopped. */
thefile->pristine = FALSE; if (!keep)
thefile->pristine = FALSE;
} }
/* Add a new undo struct to the top of the current pile. */ /* Add a new undo struct to the top of the current pile. */
...@@ -1232,7 +1234,7 @@ void add_undo(undo_type action) ...@@ -1232,7 +1234,7 @@ void add_undo(undo_type action)
return; return;
/* Blow away newer undo items if we add somewhere in the middle. */ /* Blow away newer undo items if we add somewhere in the middle. */
discard_until(u, openfile); discard_until(u, openfile, TRUE);
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, " >> Adding an undo...\n"); fprintf(stderr, " >> Adding an undo...\n");
...@@ -2519,7 +2521,7 @@ void do_justify(bool full_justify) ...@@ -2519,7 +2521,7 @@ void do_justify(bool full_justify)
#ifndef NANO_TINY #ifndef NANO_TINY
/* Throw away the entire undo stack, to prevent a crash when /* Throw away the entire undo stack, to prevent a crash when
* the user tries to undo something in the justified text. */ * the user tries to undo something in the justified text. */
discard_until(NULL, openfile); discard_until(NULL, openfile, FALSE);
#endif #endif
/* Blow away the unjustified text. */ /* Blow away the unjustified text. */
free_filestruct(jusbuffer); free_filestruct(jusbuffer);
...@@ -2968,7 +2970,7 @@ const char *do_alt_speller(char *tempfile_name) ...@@ -2968,7 +2970,7 @@ const char *do_alt_speller(char *tempfile_name)
#ifndef NANO_TINY #ifndef NANO_TINY
/* Flush the undo stack, to avoid making a mess when the user /* Flush the undo stack, to avoid making a mess when the user
* tries to undo things in spell-corrected lines. */ * tries to undo things in spell-corrected lines. */
discard_until(NULL, openfile); discard_until(NULL, openfile, FALSE);
#endif #endif
} }
...@@ -3465,7 +3467,7 @@ void do_formatter(void) ...@@ -3465,7 +3467,7 @@ void do_formatter(void)
#ifndef NANO_TINY #ifndef NANO_TINY
/* Flush the undo stack, to avoid a mess or crash when /* Flush the undo stack, to avoid a mess or crash when
* the user tries to undo things in reformatted lines. */ * the user tries to undo things in reformatted lines. */
discard_until(NULL, openfile); discard_until(NULL, openfile, FALSE);
#endif #endif
finalstatus = _("Finished formatting"); finalstatus = _("Finished formatting");
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment