Skip to content

Commit

Permalink
vis: apply language map only to key values not modifiers
Browse files Browse the repository at this point in the history
The language map translation should not take modifiers into account.
For example if `a` is mapped to `b` then `<M-a>` should also be mapped
to `<M-b>`.

Fix martanne#404
martanne committed Oct 26, 2016

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent d4f8522 commit 8a85e07
Showing 4 changed files with 20 additions and 19 deletions.
16 changes: 5 additions & 11 deletions ui-curses.c
Original file line number Diff line number Diff line change
@@ -91,7 +91,6 @@ typedef struct {
enum UiLayout layout; /* whether windows are displayed horizontally or vertically */
TermKey *termkey; /* libtermkey instance to handle keyboard input (stdin or /dev/tty) */
struct termios tio; /* terminal state to restore before exiting */
char key[64]; /* string representation of last pressed key */
} UiCurses;

struct UiCursesWin {
@@ -1094,10 +1093,9 @@ static bool ui_haskey(Ui *ui) {
return c != ERR;
}

static const char *ui_getkey(Ui *ui) {
static bool ui_getkey(Ui *ui, TermKeyKey *key) {
UiCurses *uic = (UiCurses*)ui;
TermKeyKey key;
TermKeyResult ret = termkey_getkey(uic->termkey, &key);
TermKeyResult ret = termkey_getkey(uic->termkey, key);

if (ret == TERMKEY_RES_EOF) {
int tty = open("/dev/tty", O_RDWR);
@@ -1119,17 +1117,13 @@ static const char *ui_getkey(Ui *ui) {
fd.fd = STDIN_FILENO;
fd.events = POLLIN;
if (poll(&fd, 1, termkey_get_waittime(uic->termkey)) == 0)
ret = termkey_getkey_force(uic->termkey, &key);
ret = termkey_getkey_force(uic->termkey, key);
}

if (ret != TERMKEY_RES_KEY)
return NULL;
termkey_strfkey(uic->termkey, uic->key, sizeof(uic->key), &key, TERMKEY_FORMAT_VIM);
return uic->key;

return ret == TERMKEY_RES_KEY;
fatal:
ui_die_msg(ui, "Failed to re-open stdin as /dev/tty\n");
return NULL;
return false;
}

static void ui_terminal_save(Ui *ui) {
2 changes: 1 addition & 1 deletion ui.h
Original file line number Diff line number Diff line change
@@ -66,7 +66,7 @@ struct Ui {
void (*redraw)(Ui*);
void (*update)(Ui*);
void (*suspend)(Ui*);
const char* (*getkey)(Ui*);
bool (*getkey)(Ui*, TermKeyKey*);
bool (*haskey)(Ui*);
void (*terminal_save)(Ui*);
void (*terminal_restore)(Ui*);
1 change: 1 addition & 0 deletions vis-core.h
Original file line number Diff line number Diff line change
@@ -162,6 +162,7 @@ struct Vis {
Map *options; /* ":set"-options */
Map *keymap; /* key translation before any bindings are matched */
bool keymap_disabled; /* ignore key map for next key press, gets automatically re-enabled */
char key[64]; /* last pressed key as reported from the UI */
Buffer input_queue; /* holds pending input keys */
Buffer *keys; /* currently active keys buffer (either the input_queue or a macro) */
bool errorhandler; /* whether we are currently in an error handler, used to avoid recursion */
20 changes: 13 additions & 7 deletions vis.c
Original file line number Diff line number Diff line change
@@ -864,20 +864,26 @@ static void vis_keys_push(Vis *vis, const char *input, size_t pos, bool record)
}

static const char *getkey(Vis *vis) {
const char *key = vis->ui->getkey(vis->ui);
if (!key)
TermKeyKey key = { 0 };
if (!vis->ui->getkey(vis->ui, &key))
return NULL;
vis_info_hide(vis);
bool use_keymap = vis->mode->id != VIS_MODE_INSERT &&
vis->mode->id != VIS_MODE_REPLACE &&
!vis->keymap_disabled;
vis->keymap_disabled = false;
if (use_keymap) {
const char *mapped = map_get(vis->keymap, key);
if (mapped)
return mapped;
if (key.type == TERMKEY_TYPE_UNICODE && use_keymap) {
const char *mapped = map_get(vis->keymap, key.utf8);
if (mapped) {
size_t len = strlen(mapped);
if (len < sizeof(key.utf8))
memcpy(key.utf8, mapped, len);
}
}
return key;

TermKey *termkey = vis->ui->termkey_get(vis->ui);
termkey_strfkey(termkey, vis->key, sizeof(vis->key), &key, TERMKEY_FORMAT_VIM);
return vis->key;
}

bool vis_signal_handler(Vis *vis, int signum, const siginfo_t *siginfo, const void *context) {

0 comments on commit 8a85e07

Please sign in to comment.