From 9c4999fc9ee4a071bba068e68d690ba637ec28f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Fri, 17 Oct 2014 12:22:51 +0200 Subject: Factor out editor main loop --- vis.c | 57 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 27 deletions(-) (limited to 'vis.c') diff --git a/vis.c b/vis.c index 6c35574..087ff08 100644 --- a/vis.c +++ b/vis.c @@ -1583,33 +1583,7 @@ static void die(const char *errstr, ...) { exit(EXIT_FAILURE); } -int main(int argc, char *argv[]) { - /* decide which key configuration to use based on argv[0] */ - char *arg0 = argv[0]; - while (*arg0 && (*arg0 == '.' || *arg0 == '/')) - arg0++; - for (int i = 0; i < LENGTH(editors); i++) { - if (editors[i].name[0] == arg0[0]) { - config = &editors[i]; - break; - } - } - - mode_prev = mode = config->mode; - setup(); - - if (!(vis = editor_new(screen.w, screen.h))) - die("Could not allocate editor core\n"); - if (!editor_syntax_load(vis, syntaxes, colors)) - die("Could not load syntax highlighting definitions\n"); - editor_statusbar_set(vis, config->statusbar); - - for (int i = 1; i < MAX(argc, 2); i++) { - const char *file = i < argc ? argv[i] : NULL; - if (!editor_window_new(vis, file)) - die("Could not load `%s': %s\n", file, strerror(errno)); - } - +static void mainloop() { struct timeval idle = { .tv_usec = 0 }, *timeout = NULL; Key key, key_prev, *key_mod = NULL; @@ -1674,7 +1648,36 @@ int main(int argc, char *argv[]) { if (mode->idle) timeout = &idle; } +} + +int main(int argc, char *argv[]) { + /* decide which key configuration to use based on argv[0] */ + char *arg0 = argv[0]; + while (*arg0 && (*arg0 == '.' || *arg0 == '/')) + arg0++; + for (int i = 0; i < LENGTH(editors); i++) { + if (editors[i].name[0] == arg0[0]) { + config = &editors[i]; + break; + } + } + + mode_prev = mode = config->mode; + setup(); + + if (!(vis = editor_new(screen.w, screen.h))) + die("Could not allocate editor core\n"); + if (!editor_syntax_load(vis, syntaxes, colors)) + die("Could not load syntax highlighting definitions\n"); + editor_statusbar_set(vis, config->statusbar); + + for (int i = 1; i < MAX(argc, 2); i++) { + const char *file = i < argc ? argv[i] : NULL; + if (!editor_window_new(vis, file)) + die("Could not load `%s': %s\n", file, strerror(errno)); + } + mainloop(); editor_free(vis); endwin(); return 0; -- cgit v1.2.3