aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2016-05-04 13:23:44 +0200
committerMarc André Tanner <mat@brain-dump.org>2016-05-04 13:47:45 +0200
commit8f92b98848f9366e78c7aa824615bade83971513 (patch)
tree8c3253181320ae22abf76a370da339840a96a464
parent583045c2d85f30cfebc7a7689c27c797db2d30f4 (diff)
downloadvis-8f92b98848f9366e78c7aa824615bade83971513.tar.gz
vis-8f92b98848f9366e78c7aa824615bade83971513.tar.xz
vis: clean up interaction between vis and ui
A concrete user interface implementation should not have to depend on libtermkey. Therefore the vis core now uses an independent instance to parse keys.
-rw-r--r--main.c1
-rw-r--r--ui-curses.c20
-rw-r--r--ui.h5
-rw-r--r--vis-cmds.c3
-rw-r--r--vis-core.h2
-rw-r--r--vis.c14
6 files changed, 20 insertions, 25 deletions
diff --git a/main.c b/main.c
index 6556700..eb98d44 100644
--- a/main.c
+++ b/main.c
@@ -4,6 +4,7 @@
#include <wchar.h>
#include <ctype.h>
#include <errno.h>
+#include <stdlib.h>
#include "ui-curses.h"
#include "vis.h"
diff --git a/ui-curses.c b/ui-curses.c
index 475225b..1de0f99 100644
--- a/ui-curses.c
+++ b/ui-curses.c
@@ -15,6 +15,7 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
+#include <termkey.h>
#include "ui-curses.h"
#include "vis.h"
@@ -1066,23 +1067,14 @@ static TermKey *ui_termkey_new(int fd) {
return termkey;
}
-static TermKey *ui_termkey_get(Ui *ui) {
- UiCurses *uic = (UiCurses*)ui;
- return uic->termkey;
-}
-
static void ui_suspend(Ui *ui) {
endwin();
raise(SIGSTOP);
}
-static bool ui_haskey(Ui *ui) {
- nodelay(stdscr, TRUE);
- int c = getch();
- if (c != ERR)
- ungetch(c);
- nodelay(stdscr, FALSE);
- return c != ERR;
+static void ui_needkey(Ui *ui) {
+ UiCurses *uic = (UiCurses*)ui;
+ termkey_advisereadable(uic->termkey);
}
static const char *ui_getkey(Ui *ui) {
@@ -1171,9 +1163,7 @@ Ui *ui_curses_new(void) {
.init = ui_init,
.start = ui_start,
.free = ui_curses_free,
- .termkey_get = ui_termkey_get,
.suspend = ui_suspend,
- .resize = ui_resize,
.update = ui_update,
.window_new = ui_window_new,
.window_free = ui_window_free,
@@ -1185,7 +1175,7 @@ Ui *ui_curses_new(void) {
.die = ui_die,
.info = ui_info,
.info_hide = ui_info_hide,
- .haskey = ui_haskey,
+ .needkey = ui_needkey,
.getkey = ui_getkey,
.terminal_save = ui_terminal_save,
.terminal_restore = ui_terminal_restore,
diff --git a/ui.h b/ui.h
index de1b948..d846deb 100644
--- a/ui.h
+++ b/ui.h
@@ -3,7 +3,6 @@
#include <stdbool.h>
#include <stdarg.h>
-#include <termkey.h>
typedef struct Ui Ui;
typedef struct UiWin UiWin;
@@ -48,7 +47,6 @@ struct Ui {
bool (*init)(Ui*, Vis*);
bool (*start)(Ui*);
void (*free)(Ui*);
- void (*resize)(Ui*);
UiWin* (*window_new)(Ui*, View*, File*, enum UiOption);
void (*window_free)(UiWin*);
void (*window_focus)(UiWin*);
@@ -62,10 +60,9 @@ struct Ui {
void (*update)(Ui*);
void (*suspend)(Ui*);
const char* (*getkey)(Ui*);
- bool (*haskey)(Ui*);
+ void (*needkey)(Ui*);
void (*terminal_save)(Ui*);
void (*terminal_restore)(Ui*);
- TermKey* (*termkey_get)(Ui*);
};
struct UiWin {
diff --git a/vis-cmds.c b/vis-cmds.c
index e52072d..34f75f2 100644
--- a/vis-cmds.c
+++ b/vis-cmds.c
@@ -1,6 +1,5 @@
/* this file is included from sam.c */
-#include <termkey.h>
#include "vis-lua.h"
#ifndef VIS_OPEN
@@ -633,7 +632,7 @@ static void print_symbolic_keys(Vis *vis, Text *txt) {
TERMKEY_SYM_KPEQUALS,
};
- TermKey *termkey = vis->ui->termkey_get(vis->ui);
+ TermKey *termkey = vis->termkey;
text_appendf(txt, " ␣ (a literal \" \" space symbol must be used to refer to <Space>)\n");
for (size_t i = 0; i < LENGTH(keys); i++) {
text_appendf(txt, " <%s>\n", termkey_get_keyname(termkey, keys[i]));
diff --git a/vis-core.h b/vis-core.h
index 6d3b35f..68b4e43 100644
--- a/vis-core.h
+++ b/vis-core.h
@@ -2,6 +2,7 @@
#define VIS_CORE_H
#include <setjmp.h>
+#include <termkey.h>
#include "vis.h"
#include "register.h"
#include "text.h"
@@ -174,6 +175,7 @@ struct Vis {
VisEvent *event;
Array motions;
Array textobjects;
+ TermKey *termkey; /* libtermkey instance used to parse special keys given by ui */
};
/** stuff used by multiple of the vis-* files */
diff --git a/vis.c b/vis.c
index 7c1ab78..c05eb3c 100644
--- a/vis.c
+++ b/vis.c
@@ -19,7 +19,6 @@
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <pwd.h>
-#include <termkey.h>
#include "vis.h"
#include "text-util.h"
@@ -350,6 +349,12 @@ Vis *vis_new(Ui *ui, VisEvent *event) {
Vis *vis = calloc(1, sizeof(Vis));
if (!vis)
return NULL;
+ const char *term = getenv("TERM");
+ if (!term)
+ term = "xterm";
+ vis->termkey = termkey_new_abstract(term, TERMKEY_FLAG_UTF8);
+ if (!vis->termkey)
+ goto err;
vis->ui = ui;
vis->ui->init(vis->ui, vis);
vis->tabwidth = 8;
@@ -408,6 +413,8 @@ void vis_free(Vis *vis) {
map_free(vis_modes[i].bindings);
array_release_full(&vis->motions);
array_release_full(&vis->textobjects);
+ if (vis->termkey)
+ termkey_destroy(vis->termkey);
free(vis);
}
@@ -671,7 +678,7 @@ const char *vis_keys_next(Vis *vis, const char *keys) {
if (!keys || !*keys)
return NULL;
TermKeyKey key;
- TermKey *termkey = vis->ui->termkey_get(vis->ui);
+ TermKey *termkey = vis->termkey;
const char *next = NULL;
/* first try to parse a special key of the form <Key> */
if (*keys == '<' && (next = termkey_strpkey(termkey, keys+1, &key, TERMKEY_FORMAT_VIM)) && *next == '>')
@@ -927,8 +934,7 @@ int vis_run(Vis *vis, int argc, char *argv[]) {
continue;
}
- TermKey *termkey = vis->ui->termkey_get(vis->ui);
- termkey_advisereadable(termkey);
+ vis->ui->needkey(vis->ui);
const char *key;
while ((key = getkey(vis)))