aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--config.def.h26
-rw-r--r--editor.h10
-rw-r--r--main.c35
-rw-r--r--vis.c162
-rw-r--r--vis.h48
5 files changed, 154 insertions, 127 deletions
diff --git a/config.def.h b/config.def.h
index 64d3648..c1ba0af 100644
--- a/config.def.h
+++ b/config.def.h
@@ -1,31 +1,5 @@
/** start by reading from the top of vis.c up until config.h is included */
-/* a mode contains a set of key bindings which are currently valid.
- *
- * each mode can specify one parent mode which is consultated if a given key
- * is not found in the current mode. hence the modes form a tree which is
- * searched from the current mode up towards the root mode until a valid binding
- * is found.
- *
- * if no binding is found, mode->input(...) is called and the user entered
- * keys are passed as argument. this is used to change the document content.
- */
-enum {
- VIS_MODE_BASIC,
- VIS_MODE_MOVE,
- VIS_MODE_OPERATOR,
- VIS_MODE_OPERATOR_OPTION,
- VIS_MODE_NORMAL,
- VIS_MODE_TEXTOBJ,
- VIS_MODE_VISUAL,
- VIS_MODE_VISUAL_LINE,
- VIS_MODE_READLINE,
- VIS_MODE_PROMPT,
- VIS_MODE_INSERT,
- VIS_MODE_REPLACE,
- VIS_MODE_LAST,
-};
-
static Mode vis_modes[VIS_MODE_LAST];
/* command recognized at the ':'-prompt. commands are found using a unique
diff --git a/editor.h b/editor.h
index 428839b..098b7fa 100644
--- a/editor.h
+++ b/editor.h
@@ -44,6 +44,16 @@ typedef struct {
const char *alias;
} KeyBinding;
+/* a mode contains a set of key bindings which are currently valid.
+ *
+ * each mode can specify one parent mode which is consultated if a given key
+ * is not found in the current mode. hence the modes form a tree which is
+ * searched from the current mode up towards the root mode until a valid binding
+ * is found.
+ *
+ * if no binding is found, mode->input(...) is called and the user entered
+ * keys are passed as argument. this is used to change the document content.
+ */
typedef struct Mode Mode;
struct Mode {
Mode *parent; /* if no match is found in this mode, search will continue there */
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..a66c21c
--- /dev/null
+++ b/main.c
@@ -0,0 +1,35 @@
+#include <signal.h>
+#include <string.h>
+#include <errno.h>
+
+#include "ui-curses.h"
+#include "vis.h"
+
+static Vis *vis;
+
+static void signal_handler(int signum, siginfo_t *siginfo, void *context) {
+ vis_signal_handler(vis, signum, siginfo, context);
+}
+
+int main(int argc, char *argv[]) {
+
+ vis = vis_new(ui_curses_new());
+
+ /* install signal handlers etc. */
+ struct sigaction sa;
+ memset(&sa, 0, sizeof sa);
+ sa.sa_flags = SA_SIGINFO;
+ sa.sa_sigaction = signal_handler;
+ if (sigaction(SIGBUS, &sa, NULL) || sigaction(SIGINT, &sa, NULL))
+ vis_die(vis, "sigaction: %s", strerror(errno));
+
+ sigset_t blockset;
+ sigemptyset(&blockset);
+ sigaddset(&blockset, SIGWINCH);
+ sigprocmask(SIG_BLOCK, &blockset, NULL);
+ signal(SIGPIPE, SIG_IGN);
+
+ vis_run(vis, argc, argv);
+ vis_free(vis);
+ return 0;
+}
diff --git a/vis.c b/vis.c
index ac41ca6..2261285 100644
--- a/vis.c
+++ b/vis.c
@@ -34,8 +34,7 @@
#include <sys/mman.h>
#include <termkey.h>
-#include "ui-curses.h"
-#include "editor.h"
+#include "vis.h"
#include "text-util.h"
#include "text-motions.h"
#include "text-objects.h"
@@ -56,21 +55,6 @@ static size_t op_repeat_insert(Vis*, Text*, OperatorContext *c);
static size_t op_repeat_replace(Vis*, Text*, OperatorContext *c);
static size_t op_cursor(Vis*, Text*, OperatorContext *c);
-/* these can be passed as int argument to operator(&(const Arg){ .i = OP_*}) */
-enum {
- OP_DELETE,
- OP_CHANGE,
- OP_YANK,
- OP_PUT,
- OP_SHIFT_RIGHT,
- OP_SHIFT_LEFT,
- OP_CASE_CHANGE,
- OP_JOIN,
- OP_REPEAT_INSERT,
- OP_REPEAT_REPLACE,
- OP_CURSOR,
-};
-
static Operator ops[] = {
[OP_DELETE] = { op_delete },
[OP_CHANGE] = { op_change },
@@ -2587,7 +2571,7 @@ static bool vis_window_split(Win *win) {
return true;
}
-static void vis_die(Vis *vis, const char *msg, ...) {
+void vis_die(Vis *vis, const char *msg, ...) {
va_list ap;
va_start(ap, msg);
vis->ui->die(vis->ui, msg, ap);
@@ -2703,7 +2687,7 @@ static const char *getkey(Vis *vis) {
return key;
}
-static bool vis_signal_handler(Vis *vis, int signum, const siginfo_t *siginfo, const void *context) {
+bool vis_signal_handler(Vis *vis, int signum, const siginfo_t *siginfo, const void *context) {
switch (signum) {
case SIGBUS:
for (File *file = vis->files; file; file = file->next) {
@@ -2721,7 +2705,64 @@ static bool vis_signal_handler(Vis *vis, int signum, const siginfo_t *siginfo, c
return false;
}
-static void vis_run(Vis *vis) {
+static void vis_args(Vis *vis, int argc, char *argv[]) {
+ char *cmd = NULL;
+ bool end_of_options = false;
+ for (int i = 1; i < argc; i++) {
+ if (argv[i][0] == '-' && !end_of_options) {
+ switch (argv[i][1]) {
+ case '-':
+ end_of_options = true;
+ break;
+ case 'v':
+ vis_die(vis, "vis %s, compiled " __DATE__ " " __TIME__ "\n", VERSION);
+ break;
+ case '\0':
+ break;
+ default:
+ vis_die(vis, "Unknown command option: %s\n", argv[i]);
+ break;
+ }
+ } else if (argv[i][0] == '+') {
+ cmd = argv[i] + (argv[i][1] == '/' || argv[i][1] == '?');
+ } else if (!vis_window_new(vis, argv[i])) {
+ vis_die(vis, "Can not load `%s': %s\n", argv[i], strerror(errno));
+ } else if (cmd) {
+ exec_command(vis, cmd[0], cmd+1);
+ cmd = NULL;
+ }
+ }
+
+ if (!vis->windows) {
+ if (!strcmp(argv[argc-1], "-")) {
+ if (!vis_window_new(vis, NULL))
+ vis_die(vis, "Can not create empty buffer\n");
+ ssize_t len = 0;
+ char buf[PIPE_BUF];
+ File *file = vis->win->file;
+ Text *txt = file->text;
+ file->is_stdin = true;
+ while ((len = read(STDIN_FILENO, buf, sizeof buf)) > 0)
+ text_insert(txt, text_size(txt), buf, len);
+ if (len == -1)
+ vis_die(vis, "Can not read from stdin\n");
+ text_snapshot(txt);
+ int fd = open("/dev/tty", O_RDONLY);
+ if (fd == -1)
+ vis_die(vis, "Can not reopen stdin\n");
+ dup2(fd, STDIN_FILENO);
+ close(fd);
+ } else if (!vis_window_new(vis, NULL)) {
+ vis_die(vis, "Can not create empty buffer\n");
+ }
+ if (cmd)
+ exec_command(vis, cmd[0], cmd+1);
+ }
+}
+
+void vis_run(Vis *vis, int argc, char *argv[]) {
+ vis_args(vis, argc, argv);
+
struct timespec idle = { .tv_nsec = 0 }, *timeout = NULL;
sigset_t emptyset;
@@ -2809,84 +2850,3 @@ Vis *vis_new(Ui *ui) {
return vis;
}
-static Vis *vis; /* global editor instance */
-
-static void signal_handler(int signum, siginfo_t *siginfo, void *context) {
- vis_signal_handler(vis, signum, siginfo, context);
-}
-
-int main(int argc, char *argv[]) {
-
- vis = vis_new(ui_curses_new());
-
- char *cmd = NULL;
- bool end_of_options = false;
- for (int i = 1; i < argc; i++) {
- if (argv[i][0] == '-' && !end_of_options) {
- switch (argv[i][1]) {
- case '-':
- end_of_options = true;
- break;
- case 'v':
- vis_die(vis, "vis %s, compiled " __DATE__ " " __TIME__ "\n", VERSION);
- break;
- case '\0':
- break;
- default:
- vis_die(vis, "Unknown command option: %s\n", argv[i]);
- break;
- }
- } else if (argv[i][0] == '+') {
- cmd = argv[i] + (argv[i][1] == '/' || argv[i][1] == '?');
- } else if (!vis_window_new(vis, argv[i])) {
- vis_die(vis, "Can not load `%s': %s\n", argv[i], strerror(errno));
- } else if (cmd) {
- exec_command(vis, cmd[0], cmd+1);
- cmd = NULL;
- }
- }
-
- if (!vis->windows) {
- if (!strcmp(argv[argc-1], "-")) {
- if (!vis_window_new(vis, NULL))
- vis_die(vis, "Can not create empty buffer\n");
- ssize_t len = 0;
- char buf[PIPE_BUF];
- File *file = vis->win->file;
- Text *txt = file->text;
- file->is_stdin = true;
- while ((len = read(STDIN_FILENO, buf, sizeof buf)) > 0)
- text_insert(txt, text_size(txt), buf, len);
- if (len == -1)
- vis_die(vis, "Can not read from stdin\n");
- text_snapshot(txt);
- int fd = open("/dev/tty", O_RDONLY);
- if (fd == -1)
- vis_die(vis, "Can not reopen stdin\n");
- dup2(fd, STDIN_FILENO);
- close(fd);
- } else if (!vis_window_new(vis, NULL)) {
- vis_die(vis, "Can not create empty buffer\n");
- }
- if (cmd)
- exec_command(vis, cmd[0], cmd+1);
- }
-
- /* install signal handlers etc. */
- struct sigaction sa;
- memset(&sa, 0, sizeof sa);
- sa.sa_flags = SA_SIGINFO;
- sa.sa_sigaction = signal_handler;
- if (sigaction(SIGBUS, &sa, NULL) || sigaction(SIGINT, &sa, NULL))
- vis_die(vis, "sigaction: %s", strerror(errno));
-
- sigset_t blockset;
- sigemptyset(&blockset);
- sigaddset(&blockset, SIGWINCH);
- sigprocmask(SIG_BLOCK, &blockset, NULL);
- signal(SIGPIPE, SIG_IGN);
-
- vis_run(vis);
- editor_free(vis);
- return 0;
-}
diff --git a/vis.h b/vis.h
new file mode 100644
index 0000000..496c0d3
--- /dev/null
+++ b/vis.h
@@ -0,0 +1,48 @@
+#ifndef VIS_H
+#define VIS_H
+
+#include "ui.h"
+#include "editor.h"
+
+typedef Editor Vis;
+
+Vis *vis_new(Ui*);
+#define vis_free editor_free
+
+void vis_run(Vis*, int argc, char *argv[]);
+void vis_die(Vis*, const char *msg, ...);
+
+enum VisMode {
+ VIS_MODE_BASIC,
+ VIS_MODE_MOVE,
+ VIS_MODE_OPERATOR,
+ VIS_MODE_OPERATOR_OPTION,
+ VIS_MODE_NORMAL,
+ VIS_MODE_TEXTOBJ,
+ VIS_MODE_VISUAL,
+ VIS_MODE_VISUAL_LINE,
+ VIS_MODE_READLINE,
+ VIS_MODE_PROMPT,
+ VIS_MODE_INSERT,
+ VIS_MODE_REPLACE,
+ VIS_MODE_LAST,
+};
+
+enum VisOperator {
+ OP_DELETE,
+ OP_CHANGE,
+ OP_YANK,
+ OP_PUT,
+ OP_SHIFT_RIGHT,
+ OP_SHIFT_LEFT,
+ OP_CASE_CHANGE,
+ OP_JOIN,
+ OP_REPEAT_INSERT,
+ OP_REPEAT_REPLACE,
+ OP_CURSOR,
+};
+
+bool vis_signal_handler(Vis*, int signum, const siginfo_t *siginfo,
+ const void *context);
+
+#endif