From 8cc6308f9c3723a76979e4df3048724a1bbb4b7e Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Wed, 23 Dec 2015 16:34:44 +0000 Subject: [PATCH] Offering ^Q in the writeout menu to close and discard the current buffer without saving it. git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@5509 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- ChangeLog | 7 ++++++ doc/man/nanorc.5 | 7 +++++- doc/texinfo/nano.texi | 9 ++++++-- src/files.c | 50 +++++++++++++++++++++++++++---------------- src/global.c | 11 ++++++++++ src/nano.c | 28 +++++++++++++----------- src/proto.h | 4 +++- 7 files changed, 82 insertions(+), 34 deletions(-) diff --git a/ChangeLog b/ChangeLog index d006b8c5..67c3ec7c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2015-12-23 Benno Schulenberg + * src/files.c (do_writeout, do_writeout_void), src/global.c + (shortcut_init, strtosc), src/nano.c (do_exit, close_and_go), + doc/man/nanorc.5, doc/texinfo/nano.texi: In the writeout menu, + offer ^Q to close and discard the buffer without saving it. By + default, the key is bound only when --tempfile is in effect. + 2015-12-23 Mike Frysinger * doc/syntax/autoconf.nanorc: Handle .m4 files too, add the "elif" keyword, handle dnl comments better, and mark trailing whitespace. diff --git a/doc/man/nanorc.5 b/doc/man/nanorc.5 index 2d029141..cb7ff3ab 100644 --- a/doc/man/nanorc.5 +++ b/doc/man/nanorc.5 @@ -20,7 +20,7 @@ .\" Documentation License along with this program. If not, see .\" . .\" -.TH NANORC 5 "version 2.5.0" "December 2015" +.TH NANORC 5 "version 2.5.1" "December 2015" .\" Please adjust this date whenever revising the manpage. .\" .SH NAME @@ -596,6 +596,11 @@ When writing a file, 'prepends' (writes at the beginning) instead of overwriting .B backup When writing a file, creates a backup of the current file. .TP +.B discardbuffer +When about to write a file, discard the current buffer without saving. +(This function is bound by default only when option \fB\-\-tempfile\fR +is in effect.) +.TP .B firstfile Goes to the first file when using the file browser (reading or writing files). .TP diff --git a/doc/texinfo/nano.texi b/doc/texinfo/nano.texi index 646575b9..6fb84155 100644 --- a/doc/texinfo/nano.texi +++ b/doc/texinfo/nano.texi @@ -6,7 +6,7 @@ @smallbook @set EDITION 0.3 -@set VERSION 2.5.0 +@set VERSION 2.5.1 @set UPDATED December 2015 @dircategory Editors @@ -21,7 +21,7 @@ @titlepage @title GNU @code{nano} @subtitle a small and friendly text editor. -@subtitle version 2.5.0 +@subtitle version 2.5.1 @author Chris Allegretta @page @@ -1159,6 +1159,11 @@ When writing a file, 'prepends' (writes at the beginning) instead of overwriting @item backup When writing a file, creates a backup of the current file. +@item discardbuffer +When about to write a file, discard the current buffer without saving. +(This function is bound by default only when option @option{--tempfile} +is in effect.) + @item tofiles Starts the file browser, allowing to select a file from a list. diff --git a/src/files.c b/src/files.c index d5a4ca43..490efa25 100644 --- a/src/files.c +++ b/src/files.c @@ -2211,11 +2211,11 @@ bool write_marked_file(const char *name, FILE *f_open, bool tmp, #endif /* !NANO_TINY */ /* Write the current file to disk. If the mark is on, write the current - * marked selection to disk. If exiting is TRUE, write the file to disk - * regardless of whether the mark is on, and without prompting if the - * TEMP_FILE flag is set and the current file has a name. Return TRUE - * on success or FALSE on error. */ -bool do_writeout(bool exiting) + * marked selection to disk. If exiting is TRUE, write the entire file + * to disk regardless of whether the mark is on, and without prompting if + * the TEMP_FILE flag is set and the current file has a name. Return 0 + * on error, 1 on success, and 2 when the buffer is to be discarded. */ +int do_writeout(bool exiting) { int i; append_type append = OVERWRITE; @@ -2224,15 +2224,13 @@ bool do_writeout(bool exiting) #ifndef DISABLE_EXTRA static bool did_credits = FALSE; #endif - bool retval = FALSE; + bool result = FALSE; if (exiting && openfile->filename[0] != '\0' && ISSET(TEMP_FILE)) { - retval = write_file(openfile->filename, NULL, FALSE, OVERWRITE, - FALSE); + result = write_file(openfile->filename, NULL, FALSE, OVERWRITE, FALSE); - /* Write succeeded. */ - if (retval) - return retval; + if (result) + return 1; /* The write succeeded. */ } ans = mallocstrcpy(NULL, @@ -2289,11 +2287,26 @@ bool do_writeout(bool exiting) * encoded null), treat it as though it's blank. */ if (i < 0 || *answer == '\n') { statusbar(_("Cancelled")); - retval = FALSE; break; } else { functionptrtype func = func_from_key(&i); + /* Upon request, abandon the buffer, if user is sure. */ + if (func == discard_buffer) { + if (openfile->modified) + i = do_yesno_prompt(FALSE, + _("Save modified buffer anyway ? ")); + else + i = 0; + + if (i == 0) { + free(ans); + return 2; /* Yes, discard the buffer. */ + } + if (i < 0) + continue; /* The discard was cancelled. */ + } + ans = mallocstrcpy(ans, answer); #ifndef DISABLE_BROWSER @@ -2347,7 +2360,6 @@ bool do_writeout(bool exiting) strcasecmp(answer, "zzy") == 0) { do_credits(); did_credits = TRUE; - retval = FALSE; break; } #endif @@ -2431,7 +2443,7 @@ bool do_writeout(bool exiting) * a separate file. If we're using restricted mode, this * function is disabled, since it allows reading from or * writing to files not specified on the command line. */ - retval = + result = #ifndef NANO_TINY (!ISSET(RESTRICTED) && !exiting && openfile->mark_set) ? write_marked_file(answer, NULL, FALSE, append) : @@ -2444,14 +2456,16 @@ bool do_writeout(bool exiting) free(ans); - return retval; + return result ? 1 : 0; } -/* Write the current file to disk. If the mark is on, write the current - * marked selection to disk. */ +/* Write the current buffer to disk, or discard it. */ void do_writeout_void(void) { - do_writeout(FALSE); + /* If the user chose to discard the buffer, close it. */ + if (do_writeout(FALSE) == 2) + close_and_go(); + display_main_list(); } diff --git a/src/global.c b/src/global.c index cdc6e994..c40733a4 100644 --- a/src/global.c +++ b/src/global.c @@ -270,6 +270,9 @@ void prepend_void(void) void backup_file_void(void) { } +void discard_buffer(void) +{ +} void new_buffer_void(void) { } @@ -632,6 +635,7 @@ void shortcut_init(void) const char *nano_backup_msg = N_("Toggle backing up of the original file"); const char *nano_execute_msg = N_("Execute external command"); #endif + const char *nano_discard_buffer_msg = N_("Close buffer without saving it"); #ifndef DISABLE_MULTIBUFFER const char *nano_multibuffer_msg = N_("Toggle the use of a new buffer"); #endif @@ -1006,6 +1010,9 @@ void shortcut_init(void) N_("Last File"), IFSCHELP(nano_lastfile_msg), BLANKAFTER, VIEW); #endif + add_to_funcs(discard_buffer, MWRITEFILE, + N_("Discard buffer"), IFSCHELP(nano_discard_buffer_msg), BLANKAFTER, NOVIEW); + #if !defined(NANO_TINY) && !defined(DISABLE_BROWSER) add_to_funcs(do_research, MBROWSER, whereis_next_tag, IFSCHELP(nano_whereis_next_msg), TOGETHER, VIEW); @@ -1197,6 +1204,8 @@ void shortcut_init(void) add_to_sclist(MBROWSER, "M-G", goto_dir_void, 0); add_to_sclist(MBROWSER, "F13", goto_dir_void, 0); #endif + if (ISSET(TEMP_FILE)) + add_to_sclist(MWRITEFILE, "^Q", discard_buffer, 0); add_to_sclist(MWRITEFILE, "M-D", dos_format_void, 0); add_to_sclist(MWRITEFILE, "M-M", mac_format_void, 0); if (!ISSET(RESTRICTED)) { @@ -1333,6 +1342,8 @@ sc *strtosc(char *input) s->scfunc = do_cancel; else if (!strcasecmp(input, "exit")) s->scfunc = do_exit; + else if (!strcasecmp(input, "discardbuffer")) + s->scfunc = discard_buffer; else if (!strcasecmp(input, "writeout")) s->scfunc = do_writeout_void; #ifndef NANO_TINY diff --git a/src/nano.c b/src/nano.c index 0780ebcd..dc998106 100644 --- a/src/nano.c +++ b/src/nano.c @@ -1146,23 +1146,27 @@ void do_exit(void) /* If the user chose not to save, or if the user chose to save and * the save succeeded, we're ready to exit. */ - if (i == 0 || (i == 1 && do_writeout(TRUE))) { + if (i == 0 || (i == 1 && do_writeout(TRUE))) + close_and_go(); + else if (i != 1) + statusbar(_("Cancelled")); + display_main_list(); +} + +/* Close the current buffer, and terminate nano if it was the last. */ +void close_and_go(void) +{ #ifndef NANO_TINY - if (ISSET(LOCKING) && openfile->lock_filename) - delete_lockfile(openfile->lock_filename); + /* If there is a lockfile, remove it. */ + if (ISSET(LOCKING) && openfile->lock_filename) + delete_lockfile(openfile->lock_filename); #endif - #ifndef DISABLE_MULTIBUFFER - /* Exit only if there are no more open file buffers. */ - if (!close_buffer(FALSE)) + /* If there are no more open file buffers, jump off a cliff. */ + if (!close_buffer(FALSE)) #endif - finish(); - /* If the user canceled, we go on. */ - } else if (i != 1) - statusbar(_("Cancelled")); - - display_main_list(); + finish(); } /* Another placeholder for function mapping. */ diff --git a/src/proto.h b/src/proto.h index 91dcff12..0d2683c2 100644 --- a/src/proto.h +++ b/src/proto.h @@ -325,7 +325,7 @@ bool write_file(const char *name, FILE *f_open, bool tmp, append_type bool write_marked_file(const char *name, FILE *f_open, bool tmp, append_type append); #endif -bool do_writeout(bool exiting); +int do_writeout(bool exiting); void do_writeout_void(void); #ifndef NANO_TINY void do_savefile(void); @@ -483,6 +483,7 @@ int more_space(void); int no_help(void); void no_current_file_name_warning(void); void do_exit(void); +void close_and_go(void); void signal_init(void); RETSIGTYPE handle_hupterm(int signal); RETSIGTYPE do_suspend(int signal); @@ -836,6 +837,7 @@ void mac_format_void(void); void append_void(void); void prepend_void(void); void backup_file_void(void); +void discard_buffer(void); void new_buffer_void(void); void backwards_void(void); void goto_dir_void(void); -- 2.39.5