summaryrefslogtreecommitdiff
path: root/editor.c
diff options
context:
space:
mode:
authorcflip <cflip@cflip.net>2023-01-30 14:31:28 -0700
committercflip <cflip@cflip.net>2023-01-30 14:31:28 -0700
commit24addd792a50ff47a904f819375bed32b32039a0 (patch)
tree144418ddc0dd45847ae76d69937b4d136203b848 /editor.c
parentd2a225cd108507714d505d3c133fec6660e45c8d (diff)
Move unsaved quit warning to editor.c
Diffstat (limited to 'editor.c')
-rw-r--r--editor.c23
1 files changed, 22 insertions, 1 deletions
diff --git a/editor.c b/editor.c
index 4a489a2..d7d92a1 100644
--- a/editor.c
+++ b/editor.c
@@ -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)