diff --git a/src/fn.c b/src/fn.c index f04b964..394efd6 100644 --- a/src/fn.c +++ b/src/fn.c @@ -3,7 +3,9 @@ #define _GNU_SOURCE #include // for signal, SIGWINCH +#include // for exit #include // for NULL +#include // for STDOUT_FILENO #include "fn.h" #include "file.h" // for editorOpen @@ -12,10 +14,6 @@ #include "terminal.h" // for die, setWindowSize, enableRawMode -// TODO: Put this somewhere else? -void windowResizeCallback(int signum); -void initEditor(); - void windowResizeCallback(int signum) { if (signum != SIGWINCH) { return; @@ -48,15 +46,22 @@ void initEditor() { E.screenrows -= 2; } -int main(int argc, char *argv[]) { +void exitEditor() { + // Restore from raw mode and exit + write(STDOUT_FILENO, "\x1b[2J", 4); + write(STDOUT_FILENO, "\x1b[H", 3); + exit(0); +} + +int main(int argc, char *argv[]) { enableRawMode(); initEditor(); if (argc >= 2) { editorOpen(argv[1]); } - editorSetStatusMessage("HELP: Ctrl-S = save | Ctrl-Q = Quit | Ctrl-F = find"); + editorSetStatusMessage("Ctrl-C = Quit, Ctrl-S = save, Ctrl-F = find"); signal(SIGWINCH, windowResizeCallback); @@ -65,5 +70,6 @@ int main(int argc, char *argv[]) { editorProcessKeypress(); } + exitEditor(); return 0; } diff --git a/src/fn.h b/src/fn.h index 8294720..8760155 100644 --- a/src/fn.h +++ b/src/fn.h @@ -6,7 +6,6 @@ #define FN_VERSION "0.2" #define FN_TAB_STOP 4 -#define FN_QUIT_TIMES 2 #define CTRL_KEY(k) ((k) & 0x1f) @@ -37,9 +36,6 @@ enum editorHighlight { #define HL_HIGHLIGHT_NUMBERS (1<<0) #define HL_HIGHLIGHT_STRINGS (1<<1) - -/*** data ***/ - struct editorSyntax { char *filetype; char **filematch; @@ -84,11 +80,11 @@ struct abuf { int len; }; -/*** filetypes ***/ - - -/*** prototypes ***/ +void windowResizeCallback(int signum); +void initEditor(); +void exitEditor(); + void editorSetStatusMessage(const char *fmt, ...); void editorRefreshScreen(); char *editorPrompt(char *prompt, void (*callback)(char *, int)); diff --git a/src/input.c b/src/input.c index 5e0ba6b..91069c8 100644 --- a/src/input.c +++ b/src/input.c @@ -1,6 +1,6 @@ #include // for iscntrl #include // for NULL -#include // for exit, free +#include // for free #include // for STDOUT_FILENO #include "input.h" @@ -88,27 +88,36 @@ void editorMoveCursor(int key) { } } - void editorProcessKeypress() { - static int quit_times = FN_QUIT_TIMES; - + static int wantToQuit = 0; int c = editorReadKey(); // Blocking! switch (c) { case '\r': - editorInsertNewLine(); + if (wantToQuit == 0) { + editorInsertNewLine(); + } else { + editorSetStatusMessage(""); + } break; - case CTRL_KEY('q'): - if (E.dirty && quit_times > 0) { - editorSetStatusMessage("WARNING!!! File has unsaved changes. " - "Press CTRL-Q %d more times to quit.", quit_times); - quit_times--; + case CTRL_KEY('c'): + if (E.dirty) { + editorSetStatusMessage("WARNING: Unsaved changes. Exit anyway? [y/N]"); + wantToQuit = 1; return; } - write(STDOUT_FILENO, "\x1b[2J", 4); - write(STDOUT_FILENO, "\x1b[H", 3); - exit(0); + exitEditor(); + break; + + // Special cases if the user want to quit + // TODO: Make a "question mode" with a callback for y and n + case 'y': + if (wantToQuit == 0) { + editorInsertChar(c); + } else { + exitEditor(); + } break; case CTRL_KEY('s'): @@ -162,9 +171,14 @@ void editorProcessKeypress() { break; default: - editorInsertChar(c); + if (wantToQuit == 1) { + wantToQuit = 0; + editorSetStatusMessage(""); + } else { + editorInsertChar(c); + } break; } - quit_times = FN_QUIT_TIMES; + wantToQuit = 0; }