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
#include <signal.h> // for signal, SIGWINCH
#include <stdlib.h> // for exit
#include <stdio.h> // for NULL
#include <unistd.h> // 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;
}

View file

@ -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));

View file

@ -1,6 +1,6 @@
#include <ctype.h> // for iscntrl
#include <stdio.h> // for NULL
#include <stdlib.h> // for exit, free
#include <stdlib.h> // for free
#include <unistd.h> // 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;
}