From 5d02ee64b6559e310a27bb5a84e99b37c6a99aef Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Thu, 17 May 2018 12:10:06 +0200 Subject: [PATCH] filtering: pair the cut and the insert, so they can be undone together MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When executing a command in the current buffer and piping this buffer (or marked region) to that command, then the cutting of the existing text and the insertion of the new text should be undone and redone together, as to the user they appear as a single operation. With-help-from: Marco Diego Aurélio Mesquita --- src/nano.h | 2 +- src/text.c | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/nano.h b/src/nano.h index 15eadd5d..4fd186a1 100644 --- a/src/nano.h +++ b/src/nano.h @@ -178,7 +178,7 @@ typedef enum { #ifdef ENABLE_COMMENT COMMENT, UNCOMMENT, PREFLIGHT, #endif - CUT, CUT_TO_EOF, PASTE, INSERT, OTHER + CUT, CUT_TO_EOF, PASTE, INSERT, COUPLE_BEGIN, COUPLE_END, OTHER } undo_type; /* Structure types. */ diff --git a/src/text.c b/src/text.c index deb613f9..0b5217aa 100644 --- a/src/text.c +++ b/src/text.c @@ -800,6 +800,15 @@ void do_undo(void) cutbuffer = oldcutbuffer; cutbottom = oldcutbottom; break; + case COUPLE_BEGIN: + undidmsg = _("filtering"); + break; + case COUPLE_END: + openfile->current_undo = openfile->current_undo->next; + do_undo(); + do_undo(); + do_undo(); + return; case INDENT: handle_indent_action(u, TRUE, TRUE); undidmsg = _("indent"); @@ -962,6 +971,15 @@ void do_redo(void) free_filestruct(u->cutbuffer); u->cutbuffer = NULL; break; + case COUPLE_BEGIN: + openfile->current_undo = u; + do_redo(); + do_redo(); + do_redo(); + return; + case COUPLE_END: + redidmsg = _("filtering"); + break; case INDENT: handle_indent_action(u, FALSE, TRUE); redidmsg = _("indent"); @@ -1159,16 +1177,18 @@ bool execute_command(const char *command) filestruct *was_cutbuffer = cutbuffer; cutbuffer = NULL; - if (ISSET(MULTIBUFFER)) + if (ISSET(MULTIBUFFER)) { switch_to_prev_buffer(); - - if (has_selection || !ISSET(MULTIBUFFER)) { + if (has_selection) + do_cut_text(TRUE, FALSE); + } else { + add_undo(COUPLE_BEGIN); if (!has_selection) { openfile->current = openfile->fileage; openfile->current_x = 0; } add_undo(CUT); - do_cut_text(ISSET(MULTIBUFFER), !has_selection); + do_cut_text(FALSE, !has_selection); update_undo(CUT); } @@ -1211,6 +1231,9 @@ bool execute_command(const char *command) else read_file(stream, 0, "pipe", TRUE); + if (should_pipe && !ISSET(MULTIBUFFER)) + add_undo(COUPLE_END); + if (wait(NULL) == -1) nperror("wait"); @@ -1377,6 +1400,8 @@ void add_undo(undo_type action) u->lineno += cutbottom->lineno - cutbuffer->lineno; break; case INSERT: + case COUPLE_BEGIN: + case COUPLE_END: break; case INDENT: case UNINDENT: @@ -1537,6 +1562,8 @@ fprintf(stderr, " >> Updating an undo... action = %d\n", action); case INSERT: u->mark_begin_lineno = openfile->current->lineno; u->mark_begin_x = openfile->current_x; + case COUPLE_BEGIN: + case COUPLE_END: break; default: statusline(ALERT, "Wrong undo update type -- please report a bug");