>From 38a9d64bea7679c7a39b910eaf0415acb86927e4 Mon Sep 17 00:00:00 2001 From: Brand Huntsman Date: Wed, 10 Oct 2018 16:40:59 -0600 Subject: [PATCH] add nuke function Signed-off-by: Brand Huntsman --- src/cut.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/global.c | 5 +++++ src/nano.h | 3 +++ src/proto.h | 1 + src/text.c | 18 ++++++++++++++++++ 5 files changed, 76 insertions(+) diff --git a/src/cut.c b/src/cut.c index 69c50062..72765ae3 100644 --- a/src/cut.c +++ b/src/cut.c @@ -239,6 +239,55 @@ void do_cut_till_eof(void) do_cut_text(FALSE, FALSE, TRUE); update_undo(CUT_TO_EOF); } + +/* Move text from the current buffer into oblivion. */ +void do_nuke_text(void) +{ + bool right_side_up = TRUE; + /* There *is* no region, *or* it is marked forward. */ + size_t was_totsize = openfile->totsize; + filestruct *old_cutbuffer = cutbuffer; + filestruct *old_cutbottom = cutbottom; + + /* Only add a new undo item when the current item is not a NUKE or when + * the current nuke is not contiguous with the previous nuking. */ + if (openfile->last_action != NUKE || openfile->current_undo == NULL || + openfile->current_undo->mark_begin_lineno != openfile->current->lineno) + add_undo(NUKE); + + /* Load cutbuffer from NUKE undo. */ + cutbuffer = openfile->current_undo->cutbuffer; + cutbottom = openfile->current_undo->cutbottom; + + /* TODO: update_undo() could move cutbuffer instead of copying it, + * and the following two lines wouldn't be needed. */ + /* Clear cutbuffer in undo. */ + openfile->current_undo->cutbuffer = NULL; + openfile->current_undo->cutbottom = NULL; + + if (openfile->mark) { + /* Move the marked text to the cutbuffer, and turn the mark off. */ + cut_marked(&right_side_up); + openfile->mark = NULL; + } else if (ISSET(CUT_FROM_CURSOR)) + /* Move all text up to the end of the line into the cutbuffer. */ + cut_to_eol(); + else + /* Move the entire line into the cutbuffer. */ + cut_line(); + + /* Only set the modification flag if actually something was cut. */ + if (openfile->totsize != was_totsize) + set_modified(); + + refresh_needed = TRUE; + + update_undo(NUKE); + + /* Restore cutbuffer. */ + cutbuffer = old_cutbuffer; + cutbottom = old_cutbottom; +} #endif /* !NANO_TINY */ /* Copy text from the cutbuffer into the current buffer. */ diff --git a/src/global.c b/src/global.c index a19b0db2..c6b1de49 100644 --- a/src/global.c +++ b/src/global.c @@ -562,6 +562,7 @@ void shortcut_init(void) const char *mark_gist = N_("Mark text starting from the cursor position"); const char *copy_gist = N_("Copy current line (or marked region) and store it in cutbuffer"); + const char *nuke_gist = N_("Cut current line (or marked region)"); const char *indent_gist = N_("Indent the current line (or marked lines)"); const char *unindent_gist = N_("Unindent the current line (or marked lines)"); const char *undo_gist = N_("Undo the last operation"); @@ -794,6 +795,8 @@ void shortcut_init(void) N_("Mark Text"), WITHORSANS(mark_gist), TOGETHER, VIEW); add_to_funcs(do_copy_text, MMAIN, N_("Copy Text"), WITHORSANS(copy_gist), BLANKAFTER, NOVIEW); + add_to_funcs(do_nuke_text, MMAIN, + N_("Nuke Text"), WITHORSANS(nuke_gist), TOGETHER, NOVIEW); #endif add_to_funcs(case_sens_void, MWHEREIS|MREPLACE, @@ -1468,6 +1471,8 @@ sc *strtosc(const char *input) else if (!strcasecmp(input, "copy") || !strcasecmp(input, "copytext")) /* Deprecated. Remove end of 2018. */ s->func = do_copy_text; + else if (!strcasecmp(input, "nuke")) + s->func = do_nuke_text; else if (!strcasecmp(input, "mark")) s->func = do_mark; #endif diff --git a/src/nano.h b/src/nano.h index 480e4c3c..cd0e8387 100644 --- a/src/nano.h +++ b/src/nano.h @@ -177,6 +177,9 @@ typedef enum { INDENT, UNINDENT, #ifdef ENABLE_COMMENT COMMENT, UNCOMMENT, PREFLIGHT, +#endif +#ifndef NANO_TINY + NUKE, #endif CUT, CUT_TO_EOF, PASTE, INSERT, COUPLE_BEGIN, COUPLE_END, OTHER } undo_type; diff --git a/src/proto.h b/src/proto.h index b0227253..078b9b7c 100644 --- a/src/proto.h +++ b/src/proto.h @@ -251,6 +251,7 @@ void do_cut_text_void(void); #ifndef NANO_TINY void do_copy_text(void); void do_cut_till_eof(void); +void do_nuke_text(void); #endif void do_uncut_text(void); diff --git a/src/text.c b/src/text.c index 67710f0b..f8ac12ee 100644 --- a/src/text.c +++ b/src/text.c @@ -791,6 +791,12 @@ void do_undo(void) case SPLIT_BEGIN: undidmsg = _("text add"); break; +#endif +#ifndef NANO_TINY + case NUKE: + undidmsg = _("text nuked"); + undo_cut(u); + break; #endif case CUT_TO_EOF: case CUT: @@ -967,6 +973,12 @@ void do_redo(void) case SPLIT_END: redidmsg = _("text add"); break; +#endif +#ifndef NANO_TINY + case NUKE: + redidmsg = _("text nuked"); + redo_cut(u); + break; #endif case CUT_TO_EOF: case CUT: @@ -1403,6 +1415,9 @@ void add_undo(undo_type action) #endif case CUT_TO_EOF: break; +#ifndef NANO_TINY + case NUKE: +#endif case CUT: if (openfile->mark) { u->mark_begin_lineno = openfile->mark->lineno; @@ -1528,6 +1543,9 @@ void update_undo(undo_type action) case SPLIT_BEGIN: case SPLIT_END: break; +#endif +#ifndef NANO_TINY + case NUKE: #endif case CUT_TO_EOF: case CUT: -- 2.18.0