From 4ee8f9ec38401b00661b5d2b865935de2153b9ec Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Wed, 24 Feb 2016 23:29:23 +0100 Subject: [PATCH] Save modified file on editor(1) open and quit. --- editor/command.c++ | 5 +++- editor/display.c++ | 8 ++++-- editor/editor.h++ | 3 ++ editor/modal.c++ | 71 +++++++++++++++++++++++++++++++++++++++++++++- editor/modal.h++ | 3 ++ 5 files changed, 86 insertions(+), 4 deletions(-) diff --git a/editor/command.c++ b/editor/command.c++ index 560a2736..3b376bcd 100644 --- a/editor/command.c++ +++ b/editor/command.c++ @@ -662,7 +662,7 @@ void editor_type_save_as(struct editor* editor) void editor_type_open(struct editor* editor) { - editor->mode = MODE_LOAD; + editor->mode = editor->dirty ? MODE_ASK_LOAD : MODE_LOAD; editor->modal_used = 0; editor->modal_cursor = 0; editor->modal_error = false; @@ -670,6 +670,9 @@ void editor_type_open(struct editor* editor) void editor_type_open_as(struct editor* editor) { + if ( editor->dirty ) + return editor_type_open(editor); + editor->mode = MODE_LOAD; free(editor->modal); diff --git a/editor/display.c++ b/editor/display.c++ index da0a07d6..4cdcc3c9 100644 --- a/editor/display.c++ +++ b/editor/display.c++ @@ -258,12 +258,16 @@ void render_editor(struct editor* editor, struct terminal_state* state) return; const char* msg = ""; - if ( editor->mode == MODE_SAVE ) + if ( editor->mode == MODE_SAVE || + editor->mode == MODE_SAVE_LOAD || + editor->mode == MODE_SAVE_QUIT ) msg = "File Name to Write: "; if ( editor->mode == MODE_LOAD ) msg = "File Name to Read: "; + if ( editor->mode == MODE_ASK_LOAD ) + msg = "Save modified file? (Y/N): "; if ( editor->mode == MODE_ASK_QUIT ) - msg = "Exit without saving changes? (Y/N): "; + msg = "Save modified file? (Y/N): "; if ( editor->mode == MODE_GOTO_LINE ) msg = "Go to line: "; if ( editor->mode == MODE_COMMAND ) diff --git a/editor/editor.h++ b/editor/editor.h++ index a80b3073..7bef0c0a 100644 --- a/editor/editor.h++ +++ b/editor/editor.h++ @@ -48,6 +48,9 @@ enum editor_mode MODE_EDIT, MODE_LOAD, MODE_SAVE, + MODE_SAVE_LOAD, + MODE_SAVE_QUIT, + MODE_ASK_LOAD, MODE_ASK_QUIT, MODE_GOTO_LINE, MODE_COMMAND, diff --git a/editor/modal.c++ b/editor/modal.c++ index e22d5140..97de8edb 100644 --- a/editor/modal.c++ +++ b/editor/modal.c++ @@ -38,6 +38,12 @@ #include "modal.h++" #include "multibyte.h++" +static void editor_reset_modal(struct editor* editor) +{ + editor->modal_used = 0; + editor->modal_cursor = 0; +} + bool is_truth_string(const char* truth) { return !strcmp(truth, "on") || !strcmp(truth, "off"); @@ -110,11 +116,71 @@ void editor_modal_save(struct editor* editor, const char* path) editor->modal_error = true; } +void editor_modal_save_load(struct editor* editor, const char* path) +{ + if ( editor_save_file(editor, path) ) + { + editor_reset_modal(editor); + editor->mode = MODE_LOAD; + } + else + editor->modal_error = true; +} + +void editor_modal_save_quit(struct editor* editor, const char* path) +{ + if ( editor_save_file(editor, path) ) + editor->mode = MODE_QUIT; + else + editor->modal_error = true; +} + +void editor_modal_ask_load(struct editor* editor, const char* answer) +{ + if ( tolower((unsigned char) answer[0]) == 'y' ) + { + editor_reset_modal(editor); + if ( editor->current_file_name ) + { + if ( editor_save_file(editor, editor->current_file_name) ) + { + editor->mode = MODE_LOAD; + return; + } + editor->modal_error = true; + } + editor->mode = MODE_SAVE_LOAD; + } + else if ( tolower((unsigned char) answer[0]) == 'n' ) + { + editor_reset_modal(editor); + editor->mode = MODE_LOAD; + } + else if ( !answer[0] ) + editor_type_edit(editor); + else + editor->modal_error = true; +} + void editor_modal_ask_quit(struct editor* editor, const char* answer) { if ( tolower((unsigned char) answer[0]) == 'y' ) + { + editor_reset_modal(editor); + if ( editor->current_file_name ) + { + if ( editor_save_file(editor, editor->current_file_name) ) + { + editor->mode = MODE_QUIT; + return; + } + editor->modal_error = true; + } + editor->mode = MODE_SAVE_QUIT; + } + else if ( tolower((unsigned char) answer[0]) == 'n' ) editor->mode = MODE_QUIT; - else if ( tolower((unsigned char) answer[0]) == 'n' || !answer[0] ) + else if ( !answer[0] ) editor_type_edit(editor); else editor->modal_error = true; @@ -321,6 +387,9 @@ void editor_modal_character(struct editor* editor, wchar_t c) { case MODE_LOAD: editor_modal_load(editor, param); break; case MODE_SAVE: editor_modal_save(editor, param); break; + case MODE_SAVE_LOAD: editor_modal_save_load(editor, param); break; + case MODE_SAVE_QUIT: editor_modal_save_quit(editor, param); break; + case MODE_ASK_LOAD: editor_modal_ask_load(editor, param); break; case MODE_ASK_QUIT: editor_modal_ask_quit(editor, param); break; case MODE_GOTO_LINE: editor_modal_goto_line(editor, param); break; case MODE_COMMAND: editor_modal_command(editor, param); break; diff --git a/editor/modal.h++ b/editor/modal.h++ index e2c5ab87..c5f0fb54 100644 --- a/editor/modal.h++ +++ b/editor/modal.h++ @@ -37,6 +37,9 @@ void editor_modal_delete(struct editor* editor); void editor_modal_load(struct editor* editor, const char* path); void editor_modal_save(struct editor* editor, const char* path); +void editor_modal_save_load(struct editor* editor, const char* path); +void editor_modal_save_quit(struct editor* editor, const char* path); +void editor_modal_ask_load(struct editor* editor, const char* answer); void editor_modal_ask_quit(struct editor* editor, const char* answer); void editor_modal_goto_line(struct editor* editor, const char* linestr); void editor_modal_margin(struct editor* editor, const char* marginstr);