2018-09-17 14:48:14 +02:00
|
|
|
#define _DEFAULT_SOURCE
|
|
|
|
#define _BSD_SOURCE
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
2018-10-25 12:17:28 +02:00
|
|
|
#include <signal.h> // for signal, SIGWINCH
|
2018-10-25 13:35:06 +02:00
|
|
|
#include <stdlib.h> // for exit
|
2018-10-25 12:17:28 +02:00
|
|
|
#include <stdio.h> // for NULL
|
2018-10-25 13:35:06 +02:00
|
|
|
#include <unistd.h> // for STDOUT_FILENO
|
2018-09-17 09:07:22 +02:00
|
|
|
|
2018-10-18 12:00:56 +02:00
|
|
|
#include "fn.h"
|
2018-10-25 12:17:28 +02:00
|
|
|
#include "file.h" // for editorOpen
|
|
|
|
#include "input.h" // for editorProcessKeypress
|
|
|
|
#include "output.h" // for editorRefreshScreen, editorSetStatusMessage
|
|
|
|
#include "terminal.h" // for die, setWindowSize, enableRawMode
|
|
|
|
|
2018-10-19 13:24:58 +02:00
|
|
|
|
2018-10-18 13:27:51 +02:00
|
|
|
void windowResizeCallback(int signum) {
|
|
|
|
if (signum != SIGWINCH) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setWindowSize(&E.screenrows, &E.screencols) == -1) {
|
|
|
|
die("setWindowSize");
|
|
|
|
}
|
|
|
|
E.screenrows -= 2;
|
|
|
|
editorRefreshScreen();
|
|
|
|
}
|
|
|
|
|
2018-09-17 09:07:22 +02:00
|
|
|
void initEditor() {
|
2018-09-17 13:27:19 +02:00
|
|
|
E.cx = 0;
|
|
|
|
E.cy = 0;
|
2018-10-02 10:30:17 +02:00
|
|
|
E.rx = 0;
|
2018-09-21 17:05:22 +02:00
|
|
|
E.rowoff = 0;
|
2018-10-01 17:03:40 +02:00
|
|
|
E.coloff = 0;
|
2018-09-17 14:48:14 +02:00
|
|
|
E.numrows = 0;
|
2018-09-21 09:10:15 +02:00
|
|
|
E.row = NULL;
|
2018-10-05 11:59:27 +02:00
|
|
|
E.dirty = 0;
|
2018-10-02 10:30:17 +02:00
|
|
|
E.filename = NULL;
|
|
|
|
E.statusmsg[0] = '\0';
|
|
|
|
E.statusmsg_time = 0;
|
2018-10-16 16:59:23 +02:00
|
|
|
E.syntax = NULL;
|
2018-09-17 13:27:19 +02:00
|
|
|
|
2018-10-18 13:27:51 +02:00
|
|
|
if (setWindowSize(&E.screenrows, &E.screencols) == -1) {
|
|
|
|
die("setWindowSize");
|
|
|
|
}
|
2018-10-02 10:30:17 +02:00
|
|
|
E.screenrows -= 2;
|
2018-09-17 09:07:22 +02:00
|
|
|
}
|
|
|
|
|
2018-10-25 13:35:06 +02:00
|
|
|
void exitEditor() {
|
|
|
|
// Restore from raw mode and exit
|
|
|
|
write(STDOUT_FILENO, "\x1b[2J", 4);
|
|
|
|
write(STDOUT_FILENO, "\x1b[H", 3);
|
|
|
|
exit(0);
|
|
|
|
}
|
2018-10-18 13:27:51 +02:00
|
|
|
|
2018-10-25 13:35:06 +02:00
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2018-09-17 09:07:22 +02:00
|
|
|
enableRawMode();
|
|
|
|
initEditor();
|
2018-09-17 14:48:14 +02:00
|
|
|
if (argc >= 2) {
|
|
|
|
editorOpen(argv[1]);
|
|
|
|
}
|
2018-09-17 09:07:22 +02:00
|
|
|
|
2018-10-25 13:35:06 +02:00
|
|
|
editorSetStatusMessage("Ctrl-C = Quit, Ctrl-S = save, Ctrl-F = find");
|
2018-10-02 10:30:17 +02:00
|
|
|
|
2018-10-18 13:27:51 +02:00
|
|
|
signal(SIGWINCH, windowResizeCallback);
|
|
|
|
|
2018-09-17 09:07:22 +02:00
|
|
|
while (1) {
|
|
|
|
editorRefreshScreen();
|
|
|
|
editorProcessKeypress();
|
|
|
|
}
|
|
|
|
|
2018-10-25 13:35:06 +02:00
|
|
|
exitEditor();
|
2018-09-17 09:07:22 +02:00
|
|
|
return 0;
|
|
|
|
}
|