>From ab6ec162b77450d160dca3187fc1692007ca38f4 Mon Sep 17 00:00:00 2001 From: Brand Huntsman Date: Wed, 21 Feb 2018 02:54:38 -0700 Subject: [PATCH] toggle to write buffer or selection to file Signed-off-by: Brand Huntsman --- src/files.c | 35 +++++++++++++++++++++++------------ src/global.c | 11 +++++++++++ src/proto.h | 1 + 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/files.c b/src/files.c index e6fe1fb6..980829d3 100644 --- a/src/files.c +++ b/src/files.c @@ -2027,6 +2027,7 @@ int do_writeout(bool exiting, bool withprompt) { bool result = FALSE; kind_of_writing_type method = OVERWRITE; + bool write_selection = FALSE; char *given; /* The filename we offer, or what the user typed so far. */ bool maychange = (openfile->filename[0] == '\0'); @@ -2038,11 +2039,7 @@ int do_writeout(bool exiting, bool withprompt) /* Display newlines in filenames as ^J. */ as_an_at = FALSE; - given = mallocstrcpy(NULL, -#ifndef NANO_TINY - (openfile->mark && !exiting) ? "" : -#endif - openfile->filename); + given = mallocstrcpy(NULL, openfile->filename); while (TRUE) { const char *msg; @@ -2058,17 +2055,21 @@ int do_writeout(bool exiting, bool withprompt) /* When the mark is on, offer to write the selection to disk, but * not when in restricted mode, because it would allow writing to * a file not specified on the command line. */ - if (openfile->mark && !exiting && !ISSET(RESTRICTED)) - /* TRANSLATORS: The next six strings are prompts. */ + if (write_selection) + /* TRANSLATORS: The next nine strings are prompts. */ msg = (method == PREPEND) ? _("Prepend Selection to File") : (method == APPEND) ? _("Append Selection to File") : _("Write Selection to File"); + else if (openfile->mark) + msg = (method == PREPEND) ? _("Prepend Buffer to File") : + (method == APPEND) ? _("Append Buffer to File") : + _("Write Buffer to File"); else if (method != OVERWRITE) - msg = (method == PREPEND) ? _("File Name to Prepend to") : - _("File Name to Append to"); + msg = (method == PREPEND) ? _("Prepend to File") : + _("Append to File"); else #endif /* !NANO_TINY */ - msg = _("File Name to Write"); + msg = _("Write to File"); present_path = mallocstrcpy(present_path, "./"); @@ -2132,6 +2133,16 @@ int do_writeout(bool exiting, bool withprompt) } else if (func == append_void) { method = (method == APPEND) ? OVERWRITE : APPEND; continue; + } else if (func == flip_write_selection) { + if (write_selection) { + given = mallocstrcpy(given, openfile->filename); + write_selection = FALSE; + } else if (openfile->mark && !exiting && !ISSET(RESTRICTED)) { + given = mallocstrcpy(given, ""); + write_selection = TRUE; + } else + beep(); + continue; } #endif /* !NANO_TINY */ if (func == do_help_void) { @@ -2184,7 +2195,7 @@ int do_writeout(bool exiting, bool withprompt) if (!maychange) { #ifndef NANO_TINY - if (exiting || !openfile->mark) + if (exiting || !write_selection) #endif { if (do_yesno_prompt(FALSE, _("Save file under " @@ -2252,7 +2263,7 @@ int do_writeout(bool exiting, bool withprompt) * function is disabled, since it allows reading from or * writing to files not specified on the command line. */ #ifndef NANO_TINY - if (openfile->mark && !exiting && withprompt && !ISSET(RESTRICTED)) + if (write_selection && !exiting && withprompt && !ISSET(RESTRICTED)) result = write_marked_file(answer, NULL, FALSE, method); else #endif diff --git a/src/global.c b/src/global.c index 041b09c2..4e0ebdca 100644 --- a/src/global.c +++ b/src/global.c @@ -303,6 +303,9 @@ void append_void(void) void prepend_void(void) { } +void flip_write_selection(void) +{ +} void backup_file_void(void) { } @@ -652,6 +655,7 @@ void shortcut_init(void) const char *mac_gist = N_("Toggle the use of Mac format"); const char *append_gist = N_("Toggle appending"); const char *prepend_gist = N_("Toggle prepending"); + const char *flip_write_selection_gist = N_("Toggle selection writing"); const char *backup_gist = N_("Toggle backing up of the original file"); const char *execute_gist = N_("Execute external command"); #endif @@ -1019,6 +1023,10 @@ void shortcut_init(void) add_to_funcs(prepend_void, MWRITEFILE, N_("Prepend"), WITHORSANS(prepend_gist), TOGETHER, NOVIEW); + add_to_funcs(flip_write_selection, MWRITEFILE, + N_("Write Selection"), WITHORSANS(flip_write_selection_gist), + TOGETHER, NOVIEW); + add_to_funcs(backup_file_void, MWRITEFILE, N_("Backup File"), WITHORSANS(backup_gist), BLANKAFTER, NOVIEW); } @@ -1326,6 +1334,7 @@ void shortcut_init(void) if (!ISSET(RESTRICTED)) { add_to_sclist(MWRITEFILE, "M-A", 0, append_void, 0); add_to_sclist(MWRITEFILE, "M-P", 0, prepend_void, 0); + add_to_sclist(MWRITEFILE, "M-S", 0, flip_write_selection, 0); add_to_sclist(MWRITEFILE, "M-B", 0, backup_file_void, 0); add_to_sclist(MINSERTFILE|MEXTCMD, "^X", 0, flip_execute, 0); } @@ -1648,6 +1657,8 @@ sc *strtosc(const char *input) s->scfunc = append_void; else if (!strcasecmp(input, "prepend")) s->scfunc = prepend_void; + else if (!strcasecmp(input, "flipwriteselection")) + s->scfunc = flip_write_selection; else if (!strcasecmp(input, "backup")) s->scfunc = backup_file_void; else if (!strcasecmp(input, "flipexecute")) diff --git a/src/proto.h b/src/proto.h index 26f7d6d8..a3646695 100644 --- a/src/proto.h +++ b/src/proto.h @@ -706,6 +706,7 @@ void dos_format_void(void); void mac_format_void(void); void append_void(void); void prepend_void(void); +void flip_write_selection(void); void backup_file_void(void); void flip_execute(void); #endif -- 2.16.1