Index: src/global.c =================================================================== --- src/global.c (revision 5489) +++ src/global.c (working copy) @@ -270,6 +270,9 @@ void backup_file_void(void) { } +void discard_buffer_void(void) +{ +} void new_buffer_void(void) { } @@ -632,6 +635,7 @@ 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 @@ N_("Last File"), IFSCHELP(nano_lastfile_msg), BLANKAFTER, VIEW); #endif + add_to_funcs(discard_buffer_void, MWRITEFILE, + N_("Discard buffer"), IFSCHELP(nano_discard_buffer_msg), TOGETHER, 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,7 @@ add_to_sclist(MBROWSER, "M-G", goto_dir_void, 0); add_to_sclist(MBROWSER, "F13", goto_dir_void, 0); #endif + add_to_sclist(MWRITEFILE, "^Q", discard_buffer_void, 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 +1341,8 @@ s->scfunc = do_cancel; else if (!strcasecmp(input, "exit")) s->scfunc = do_exit; + else if (!strcasecmp(input, "discardbuffer")) + s->scfunc = discard_buffer_void; else if (!strcasecmp(input, "writeout")) s->scfunc = do_writeout_void; #ifndef NANO_TINY Index: src/files.c =================================================================== --- src/files.c (revision 5489) +++ src/files.c (working copy) @@ -2221,11 +2221,11 @@ #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; @@ -2234,15 +2234,13 @@ #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; } ans = mallocstrcpy(NULL, @@ -2299,11 +2297,16 @@ * 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 (func == discard_buffer_void) { + free(ans); + return 2; + } + ans = mallocstrcpy(ans, answer); #ifndef DISABLE_BROWSER @@ -2357,7 +2360,6 @@ strcasecmp(answer, "zzy") == 0) { do_credits(); did_credits = TRUE; - retval = FALSE; break; } #endif @@ -2441,7 +2443,7 @@ * 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) : @@ -2454,14 +2456,15 @@ 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 (do_writeout(FALSE) == 2) + close_and_go(); + display_main_list(); } Index: src/proto.h =================================================================== --- src/proto.h (revision 5489) +++ src/proto.h (working copy) @@ -323,7 +323,7 @@ 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); @@ -481,6 +481,7 @@ 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); @@ -837,6 +838,7 @@ void append_void(void); void prepend_void(void); void backup_file_void(void); +void discard_buffer_void(void); void new_buffer_void(void); void backwards_void(void); void goto_dir_void(void); Index: src/nano.c =================================================================== --- src/nano.c (revision 5489) +++ src/nano.c (working copy) @@ -1139,23 +1139,27 @@ /* 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. */