Exit program with Ctrl+C, this fixes #10

This commit is contained in:
Aaron Fischer 2018-10-25 13:35:06 +02:00
parent 7dccd70e38
commit 77780bb6fb
3 changed files with 45 additions and 29 deletions

View file

@ -3,7 +3,9 @@
#define _GNU_SOURCE #define _GNU_SOURCE
#include <signal.h> // for signal, SIGWINCH #include <signal.h> // for signal, SIGWINCH
#include <stdlib.h> // for exit
#include <stdio.h> // for NULL #include <stdio.h> // for NULL
#include <unistd.h> // for STDOUT_FILENO
#include "fn.h" #include "fn.h"
#include "file.h" // for editorOpen #include "file.h" // for editorOpen
@ -12,10 +14,6 @@
#include "terminal.h" // for die, setWindowSize, enableRawMode #include "terminal.h" // for die, setWindowSize, enableRawMode
// TODO: Put this somewhere else?
void windowResizeCallback(int signum);
void initEditor();
void windowResizeCallback(int signum) { void windowResizeCallback(int signum) {
if (signum != SIGWINCH) { if (signum != SIGWINCH) {
return; return;
@ -48,15 +46,22 @@ void initEditor() {
E.screenrows -= 2; 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(); enableRawMode();
initEditor(); initEditor();
if (argc >= 2) { if (argc >= 2) {
editorOpen(argv[1]); 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); signal(SIGWINCH, windowResizeCallback);
@ -65,5 +70,6 @@ int main(int argc, char *argv[]) {
editorProcessKeypress(); editorProcessKeypress();
} }
exitEditor();
return 0; return 0;
} }

View file

@ -6,7 +6,6 @@
#define FN_VERSION "0.2" #define FN_VERSION "0.2"
#define FN_TAB_STOP 4 #define FN_TAB_STOP 4
#define FN_QUIT_TIMES 2
#define CTRL_KEY(k) ((k) & 0x1f) #define CTRL_KEY(k) ((k) & 0x1f)
@ -37,9 +36,6 @@ enum editorHighlight {
#define HL_HIGHLIGHT_NUMBERS (1<<0) #define HL_HIGHLIGHT_NUMBERS (1<<0)
#define HL_HIGHLIGHT_STRINGS (1<<1) #define HL_HIGHLIGHT_STRINGS (1<<1)
/*** data ***/
struct editorSyntax { struct editorSyntax {
char *filetype; char *filetype;
char **filematch; char **filematch;
@ -84,11 +80,11 @@ struct abuf {
int len; int len;
}; };
/*** filetypes ***/
/*** prototypes ***/
void windowResizeCallback(int signum);
void initEditor();
void exitEditor();
void editorSetStatusMessage(const char *fmt, ...); void editorSetStatusMessage(const char *fmt, ...);
void editorRefreshScreen(); void editorRefreshScreen();
char *editorPrompt(char *prompt, void (*callback)(char *, int)); char *editorPrompt(char *prompt, void (*callback)(char *, int));

View file

@ -1,6 +1,6 @@
#include <ctype.h> // for iscntrl #include <ctype.h> // for iscntrl
#include <stdio.h> // for NULL #include <stdio.h> // for NULL
#include <stdlib.h> // for exit, free #include <stdlib.h> // for free
#include <unistd.h> // for STDOUT_FILENO #include <unistd.h> // for STDOUT_FILENO
#include "input.h" #include "input.h"
@ -88,27 +88,36 @@ void editorMoveCursor(int key) {
} }
} }
void editorProcessKeypress() { void editorProcessKeypress() {
static int quit_times = FN_QUIT_TIMES; static int wantToQuit = 0;
int c = editorReadKey(); // Blocking! int c = editorReadKey(); // Blocking!
switch (c) { switch (c) {
case '\r': case '\r':
editorInsertNewLine(); if (wantToQuit == 0) {
editorInsertNewLine();
} else {
editorSetStatusMessage("");
}
break; break;
case CTRL_KEY('q'): case CTRL_KEY('c'):
if (E.dirty && quit_times > 0) { if (E.dirty) {
editorSetStatusMessage("WARNING!!! File has unsaved changes. " editorSetStatusMessage("WARNING: Unsaved changes. Exit anyway? [y/N]");
"Press CTRL-Q %d more times to quit.", quit_times); wantToQuit = 1;
quit_times--;
return; return;
} }
write(STDOUT_FILENO, "\x1b[2J", 4); exitEditor();
write(STDOUT_FILENO, "\x1b[H", 3); break;
exit(0);
// 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; break;
case CTRL_KEY('s'): case CTRL_KEY('s'):
@ -162,9 +171,14 @@ void editorProcessKeypress() {
break; break;
default: default:
editorInsertChar(c); if (wantToQuit == 1) {
wantToQuit = 0;
editorSetStatusMessage("");
} else {
editorInsertChar(c);
}
break; break;
} }
quit_times = FN_QUIT_TIMES; wantToQuit = 0;
} }