diff options
author | cflip <cflip@cflip.net> | 2023-01-30 14:31:28 -0700 |
---|---|---|
committer | cflip <cflip@cflip.net> | 2023-01-30 14:31:28 -0700 |
commit | 24addd792a50ff47a904f819375bed32b32039a0 (patch) | |
tree | 144418ddc0dd45847ae76d69937b4d136203b848 | |
parent | d2a225cd108507714d505d3c133fec6660e45c8d (diff) |
Move unsaved quit warning to editor.c
-rw-r--r-- | editor.c | 23 | ||||
-rw-r--r-- | editor.h | 1 | ||||
-rw-r--r-- | input.c | 12 |
3 files changed, 25 insertions, 11 deletions
@@ -11,6 +11,14 @@ #include "syntax.h" #include "window.h" +/* How long a message stays on screen */ +#define MESSAGE_TIMEOUT_SECONDS 5 + +/* How long the user has to press Ctrl+Q again to quit an unsaved file. */ +#define QUIT_TIMEOUT_SECONDS 5 + +static int quit_message_time = 0; + void init_editor(struct editor_state* editor) { editor->cursor_x = 0; @@ -50,6 +58,16 @@ char* editor_prompt(struct editor_state* editor, char* prompt, void (*callback)( return NULL; } +void editor_try_quit(struct editor_state *editor) +{ + if (editor->dirty && quit_message_time == 0) { + editor_set_status_message(editor, "This file has unsaved changes. Press Ctrl+Q again to quit"); + quit_message_time = time(NULL); + return; + } + exit(0); +} + void editor_move_left(struct editor_state *editor) { if (editor->cursor_x != 0) { @@ -295,8 +313,11 @@ void editor_draw_message_bar(struct editor_state* editor, struct textbuf *buffer if (message_length > editor->screen_cols) message_length = editor->screen_cols; - if (message_length && time(NULL) - editor->status_message_time < 5) + if (message_length && time(NULL) - editor->status_message_time < MESSAGE_TIMEOUT_SECONDS) textbuf_append(buffer, editor->status_message, message_length); + + if (time(NULL) - quit_message_time > QUIT_TIMEOUT_SECONDS) + quit_message_time = 0; } void editor_destroy(struct editor_state *editor) @@ -39,6 +39,7 @@ void init_editor(struct editor_state* editor); void editor_set_status_message(struct editor_state* editor, const char* format, ...); char* editor_prompt(struct editor_state* editor, char* prompt, void (*callback)(struct editor_state*, char*, int)); +void editor_try_quit(struct editor_state *editor); void editor_move_left(struct editor_state *); void editor_move_right(struct editor_state *); @@ -6,8 +6,6 @@ void editor_process_keypress(struct editor_state *editor, SDL_Keysym *keysym) { - static int quit_message = 1; - /* Handle keypresses for typing modes separately. */ if (editor->mode != EDITOR_MODE_NORMAL) { if (keysym->sym == SDLK_BACKSPACE) @@ -44,12 +42,8 @@ void editor_process_keypress(struct editor_state *editor, SDL_Keysym *keysym) break; case SDLK_q: if (keysym->mod & KMOD_CTRL) { - if (editor->dirty && quit_message) { - editor_set_status_message(editor, "This file has unsaved changes. Press Ctrl+Q again to quit"); - quit_message = 0; - return; - } - exit(0); + editor_try_quit(editor); + break; } editor->cursor_x = 0; break; @@ -87,6 +81,4 @@ void editor_process_keypress(struct editor_state *editor, SDL_Keysym *keysym) editor_find(editor); break; } - - quit_message = 1; } |