mirror of
https://gitlab.com/sortix/sortix.git
synced 2023-02-13 20:55:38 -05:00
Save modified file on editor(1) open and quit.
This commit is contained in:
parent
d472818380
commit
4ee8f9ec38
5 changed files with 86 additions and 4 deletions
|
@ -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);
|
||||
|
|
|
@ -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 )
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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;
|
||||
else if ( tolower((unsigned char) answer[0]) == 'n' || !answer[0] )
|
||||
return;
|
||||
}
|
||||
editor->modal_error = true;
|
||||
}
|
||||
editor->mode = MODE_SAVE_QUIT;
|
||||
}
|
||||
else if ( tolower((unsigned char) answer[0]) == 'n' )
|
||||
editor->mode = MODE_QUIT;
|
||||
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;
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue