summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--editor.c23
-rw-r--r--editor.h1
-rw-r--r--input.c12
3 files changed, 25 insertions, 11 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)
diff --git a/editor.h b/editor.h
index f8fbfa8..00695fe 100644
--- a/editor.h
+++ b/editor.h
@@ -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 *);
diff --git a/input.c b/input.c
index da1d6e5..73889bd 100644
--- a/input.c
+++ b/input.c
@@ -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;
}