Skip to content

Commit

Permalink
vis: clean up interaction between vis and ui
Browse files Browse the repository at this point in the history
A concrete user interface implementation should not have to depend
on libtermkey. Therefore the vis core now uses an independent instance
to parse keys.
  • Loading branch information
martanne committed May 4, 2016
1 parent 583045c commit 8f92b98
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 25 deletions.
1 change: 1 addition & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <wchar.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>

#include "ui-curses.h"
#include "vis.h"
Expand Down
20 changes: 5 additions & 15 deletions ui-curses.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <termkey.h>

#include "ui-curses.h"
#include "vis.h"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
5 changes: 1 addition & 4 deletions ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include <stdbool.h>
#include <stdarg.h>
#include <termkey.h>

typedef struct Ui Ui;
typedef struct UiWin UiWin;
Expand Down Expand Up @@ -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*);
Expand All @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions vis-cmds.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* this file is included from sam.c */

#include <termkey.h>
#include "vis-lua.h"

#ifndef VIS_OPEN
Expand Down Expand Up @@ -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]));
Expand Down
2 changes: 2 additions & 0 deletions vis-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define VIS_CORE_H

#include <setjmp.h>
#include <termkey.h>
#include "vis.h"
#include "register.h"
#include "text.h"
Expand Down Expand Up @@ -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 */
Expand Down
14 changes: 10 additions & 4 deletions vis.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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 == '>')
Expand Down Expand Up @@ -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)))
Expand Down

0 comments on commit 8f92b98

Please sign in to comment.