From bcfc71f4fe195f8ef98ea29a44f9c7a25a6aa75f Mon Sep 17 00:00:00 2001 From: Lars Henriksen Date: Tue, 20 Nov 2018 21:02:38 +0100 Subject: Safety exit and read-only mode The key_generic_command() function provides a "safety exit" in case of online changes that should be dropped at exit when auto_save is on, or (NEW) should be saved at exit when in read-only mode. A check for unsaved changes has been added. The command prompt has been extended with minimal help information. After commit 05e0fd0 "Quit, autosave and interactive save" you could not leave calcurse in read-only mode with the quit command because the key_generic_quit() function interpreted IO_SAVE_CANCEL as a decision (by the user) to cancel a save-conflict and cancelled the quit as well. Now the function will quit unconditionally in read-only mode (as stated in the man page). In normal mode a check for unsaved changes has been added in case auto-save is off. Signed-off-by: Lars Henriksen Signed-off-by: Lukas Fleischer --- src/calcurse.c | 53 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/src/calcurse.c b/src/calcurse.c index 7bdcf1e..a6d54fd 100644 --- a/src/calcurse.c +++ b/src/calcurse.c @@ -508,7 +508,11 @@ static inline void key_generic_scroll_down(void) static inline void key_generic_quit(void) { - if (conf.auto_save) + /* In read-only mode, quit unconditionally without saving. */ + if (!read_only && + (conf.auto_save || (io_get_modified() && + status_ask_bool(_("There are unsaved changes. " + "Should they be saved?")) == 1))) if (io_save_cal(interactive) == IO_SAVE_CANCEL) { /* Cancel quit as well. */ wins_update(FLAG_STA); @@ -518,28 +522,30 @@ static inline void key_generic_quit(void) note_gc(); if (conf.confirm_quit) { - if (status_ask_bool(_("Do you really want to quit?")) == 1) { + if (status_ask_bool(_("Do you really want to quit?")) == 1) exit_calcurse(EXIT_SUCCESS); - } else { - wins_erase_status_bar(); + else wins_update(FLAG_STA); - } } else { exit_calcurse(EXIT_SUCCESS); } } +/* + * Safety exit. + * Auto_save is ignored, but modifications are checked for. + * Use of force(=!) will override configuration settings and --read-only option. + */ static inline void key_generic_cmd(void) { char cmd[BUFSIZ] = ""; char *cmd_name; - int valid = 0, force = 0; + int valid = 0, force = 0, ret; char *error_msg; - status_mesg(_("Command:"), ""); + status_mesg(_("Command: [ h(elp) | w(rite)(!) | q(uit)(!) | wq(!) ]"), ""); if (getstring(win[STA].p, cmd, BUFSIZ, 0, 1) != GETSTRING_VALID) goto cleanup; - cmd_name = strtok(cmd, " "); if (cmd_name[strlen(cmd_name) - 1] == '!') { cmd_name[strlen(cmd_name) - 1] = '\0'; @@ -548,25 +554,35 @@ static inline void key_generic_cmd(void) if (!strcmp(cmd_name, "write") || !strcmp(cmd_name, "w") || !strcmp(cmd_name, "wq")) { - if (io_save_cal(interactive) == IO_SAVE_CANCEL && - strcmp(cmd_name, "wq") == 0) { - /* Cancel quit as well. */ - wins_update(FLAG_STA); - return; + if (force) + read_only = 0; + ret = io_save_cal(interactive); + /* Either the save was cancelled or read_only mode is on */ + if (ret == IO_SAVE_CANCEL) { + if (read_only) { + status_mesg(_("Read-only mode - use w!"), ""); + return; + } else if (!strcmp(cmd_name, "wq")) { + /* Cancel quit as well. */ + goto cleanup; + } } valid = 1; } if (!strcmp(cmd_name, "quit") || !strcmp(cmd_name, "q") || !strcmp(cmd_name, "wq")) { + if (!force && io_get_modified()) { + status_mesg( + _("There are unsaved changes - use w or q!"), ""); + return; + } if (force || !conf.confirm_quit || status_ask_bool( - _("Do you really want to quit?")) == 1) + _("Do you really want to quit?")) == 1) exit_calcurse(EXIT_SUCCESS); - else - wins_erase_status_bar(); valid = 1; } - if (!strcmp(cmd_name, "help")) { + if (!strcmp(cmd_name, "help") || !strcmp(cmd_name, "h")) { char *topic = strtok(NULL, " "); if (!display_help(topic)) { @@ -581,8 +597,9 @@ static inline void key_generic_cmd(void) if (!valid) { asprintf(&error_msg, _("No such command: %s"), cmd); - warnbox(error_msg); + status_mesg(error_msg, ""); mem_free(error_msg); + keys_wgetch(win[KEY].p); } cleanup: -- cgit v1.2.3-70-g09d2