Bad merge.

This commit is contained in:
Nicholas Marriott 2017-01-25 16:38:13 +00:00
parent 21d8f77a0d
commit 0f9354eec2
7 changed files with 158 additions and 463 deletions

View File

@ -158,7 +158,6 @@ dist_tmux_SOURCES = \
layout-set.c \
layout.c \
log.c \
mode-key.c \
names.c \
notify.c \
options-table.c \

View File

@ -24,20 +24,17 @@
#include "tmux.h"
/*
* Bind a key to a command, this recurses through cmd_*.
* Bind a key to a command.
*/
static enum cmd_retval cmd_bind_key_exec(struct cmd *, struct cmdq_item *);
static enum cmd_retval cmd_bind_key_mode_table(struct cmd *,
struct cmdq_item *, key_code);
const struct cmd_entry cmd_bind_key_entry = {
.name = "bind-key",
.alias = "bind",
.args = { "cnrt:T:", 1, -1 },
.usage = "[-cnr] [-t mode-table] [-T key-table] key "
.args = { "cnrT:", 2, -1 },
.usage = "[-cnr] [-T key-table] key "
"command [arguments]",
.flags = CMD_AFTERHOOK,
@ -53,27 +50,12 @@ cmd_bind_key_exec(struct cmd *self, struct cmdq_item *item)
key_code key;
const char *tablename;
if (args_has(args, 't')) {
if (args->argc != 2 && args->argc != 3) {
cmdq_error(item, "not enough arguments");
return (CMD_RETURN_ERROR);
}
} else {
if (args->argc < 2) {
cmdq_error(item, "not enough arguments");
return (CMD_RETURN_ERROR);
}
}
key = key_string_lookup_string(args->argv[0]);
if (key == KEYC_NONE || key == KEYC_UNKNOWN) {
cmdq_error(item, "unknown key: %s", args->argv[0]);
return (CMD_RETURN_ERROR);
}
if (args_has(args, 't'))
return (cmd_bind_key_mode_table(self, item, key));
if (args_has(args, 'T'))
tablename = args_get(args, 'T');
else if (args_has(args, 'n'))
@ -92,39 +74,3 @@ cmd_bind_key_exec(struct cmd *self, struct cmdq_item *item)
key_bindings_add(tablename, key, args_has(args, 'r'), cmdlist);
return (CMD_RETURN_NORMAL);
}
static enum cmd_retval
cmd_bind_key_mode_table(struct cmd *self, struct cmdq_item *item, key_code key)
{
struct args *args = self->args;
const char *tablename;
const struct mode_key_table *mtab;
struct mode_key_binding *mbind, mtmp;
enum mode_key_cmd cmd;
tablename = args_get(args, 't');
if ((mtab = mode_key_findtable(tablename)) == NULL) {
cmdq_error(item, "unknown key table: %s", tablename);
return (CMD_RETURN_ERROR);
}
cmd = mode_key_fromstring(mtab->cmdstr, args->argv[1]);
if (cmd == MODEKEY_NONE) {
cmdq_error(item, "unknown command: %s", args->argv[1]);
return (CMD_RETURN_ERROR);
}
if (args->argc != 2) {
cmdq_error(item, "no argument allowed");
return (CMD_RETURN_ERROR);
}
mtmp.key = key;
if ((mbind = RB_FIND(mode_key_tree, mtab->tree, &mtmp)) == NULL) {
mbind = xmalloc(sizeof *mbind);
mbind->key = mtmp.key;
RB_INSERT(mode_key_tree, mtab->tree, mbind);
}
mbind->cmd = cmd;
return (CMD_RETURN_NORMAL);
}

View File

@ -29,7 +29,6 @@
static enum cmd_retval cmd_list_keys_exec(struct cmd *, struct cmdq_item *);
static enum cmd_retval cmd_list_keys_table(struct cmd *, struct cmdq_item *);
static enum cmd_retval cmd_list_keys_commands(struct cmd *,
struct cmdq_item *);
@ -37,8 +36,8 @@ const struct cmd_entry cmd_list_keys_entry = {
.name = "list-keys",
.alias = "lsk",
.args = { "t:T:", 0, 0 },
.usage = "[-t mode-table] [-T key-table]",
.args = { "T:", 0, 0 },
.usage = "[-T key-table]",
.flags = CMD_STARTSERVER|CMD_AFTERHOOK,
.exec = cmd_list_keys_exec
@ -68,9 +67,6 @@ cmd_list_keys_exec(struct cmd *self, struct cmdq_item *item)
if (self->entry == &cmd_list_commands_entry)
return (cmd_list_keys_commands(self, item));
if (args_has(args, 't'))
return (cmd_list_keys_table(self, item));
tablename = args_get(args, 'T');
if (tablename != NULL && key_bindings_get_table(tablename, 0) == NULL) {
cmdq_error(item, "table %s doesn't exist", tablename);
@ -132,40 +128,6 @@ cmd_list_keys_exec(struct cmd *self, struct cmdq_item *item)
return (CMD_RETURN_NORMAL);
}
static enum cmd_retval
cmd_list_keys_table(struct cmd *self, struct cmdq_item *item)
{
struct args *args = self->args;
const char *tablename, *cmdstr;
const struct mode_key_table *mtab;
struct mode_key_binding *mbind;
int width, keywidth;
tablename = args_get(args, 't');
if ((mtab = mode_key_findtable(tablename)) == NULL) {
cmdq_error(item, "unknown key table: %s", tablename);
return (CMD_RETURN_ERROR);
}
keywidth = 0;
RB_FOREACH(mbind, mode_key_tree, mtab->tree) {
width = strlen(key_string_lookup_key(mbind->key));
if (width > keywidth)
keywidth = width;
}
RB_FOREACH(mbind, mode_key_tree, mtab->tree) {
cmdstr = mode_key_tostring(mtab->cmdstr, mbind->cmd);
if (cmdstr != NULL) {
cmdq_print(item, "bind-key -t %s %*s %s",
mtab->name, (int)keywidth,
key_string_lookup_key(mbind->key), cmdstr);
}
}
return (CMD_RETURN_NORMAL);
}
static enum cmd_retval
cmd_list_keys_commands(struct cmd *self, struct cmdq_item *item)
{

View File

@ -28,15 +28,12 @@
static enum cmd_retval cmd_unbind_key_exec(struct cmd *, struct cmdq_item *);
static enum cmd_retval cmd_unbind_key_mode_table(struct cmd *,
struct cmdq_item *, key_code);
const struct cmd_entry cmd_unbind_key_entry = {
.name = "unbind-key",
.alias = "unbind",
.args = { "ant:T:", 0, 1 },
.usage = "[-an] [-t mode-table] [-T key-table] key",
.args = { "anT:", 0, 1 },
.usage = "[-an] [-T key-table] key",
.flags = CMD_AFTERHOOK,
.exec = cmd_unbind_key_exec
@ -67,9 +64,6 @@ cmd_unbind_key_exec(struct cmd *self, struct cmdq_item *item)
key = KEYC_UNKNOWN;
}
if (args_has(args, 't'))
return (cmd_unbind_key_mode_table(self, item, key));
if (key == KEYC_UNKNOWN) {
tablename = args_get(args, 'T');
if (tablename == NULL) {
@ -98,35 +92,3 @@ cmd_unbind_key_exec(struct cmd *self, struct cmdq_item *item)
key_bindings_remove(tablename, key);
return (CMD_RETURN_NORMAL);
}
static enum cmd_retval
cmd_unbind_key_mode_table(struct cmd *self, struct cmdq_item *item,
key_code key)
{
struct args *args = self->args;
const char *tablename;
const struct mode_key_table *mtab;
struct mode_key_binding *mbind, mtmp;
tablename = args_get(args, 't');
if ((mtab = mode_key_findtable(tablename)) == NULL) {
cmdq_error(item, "unknown key table: %s", tablename);
return (CMD_RETURN_ERROR);
}
if (key == KEYC_UNKNOWN) {
while (!RB_EMPTY(mtab->tree)) {
mbind = RB_ROOT(mtab->tree);
RB_REMOVE(mode_key_tree, mtab->tree, mbind);
free(mbind);
}
return (CMD_RETURN_NORMAL);
}
mtmp.key = key;
if ((mbind = RB_FIND(mode_key_tree, mtab->tree, &mtmp)) != NULL) {
RB_REMOVE(mode_key_tree, mtab->tree, mbind);
free(mbind);
}
return (CMD_RETURN_NORMAL);
}

View File

@ -1,261 +0,0 @@
/* $OpenBSD$ */
/*
* Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <string.h>
#include "tmux.h"
/*
* Mode keys. These are the key bindings used when editing (status prompt), and
* in the modes. They are split into two sets of three tables, one set of three
* for vi and the other for emacs key bindings. The three tables are for
* editing, for menu-like modes (choice, more), and for copy modes (copy,
* scroll).
*
* The fixed tables of struct mode_key_entry below are the defaults: they are
* built into a tree of struct mode_key_binding by mode_key_init_trees, which
* can then be modified.
*
* vi command mode is handled by having a mode flag in the struct which allows
* two sets of bindings to be swapped between. A couple of editing commands
* (any matching MODEKEYEDIT_SWITCHMODE*) are special-cased to do this.
*/
/* Command to string mapping. */
struct mode_key_cmdstr {
enum mode_key_cmd cmd;
const char *name;
};
/* Entry in the default mode key tables. */
struct mode_key_entry {
key_code key;
enum mode_key_cmd cmd;
};
/* Choice keys command strings. */
static const struct mode_key_cmdstr mode_key_cmdstr_choice[] = {
{ MODEKEYCHOICE_BACKSPACE, "backspace" },
{ MODEKEYCHOICE_BOTTOMLINE, "bottom-line"},
{ MODEKEYCHOICE_CANCEL, "cancel" },
{ MODEKEYCHOICE_CHOOSE, "choose" },
{ MODEKEYCHOICE_DOWN, "down" },
{ MODEKEYCHOICE_ENDOFLIST, "end-of-list"},
{ MODEKEYCHOICE_PAGEDOWN, "page-down" },
{ MODEKEYCHOICE_PAGEUP, "page-up" },
{ MODEKEYCHOICE_SCROLLDOWN, "scroll-down" },
{ MODEKEYCHOICE_SCROLLUP, "scroll-up" },
{ MODEKEYCHOICE_STARTNUMBERPREFIX, "start-number-prefix" },
{ MODEKEYCHOICE_STARTOFLIST, "start-of-list"},
{ MODEKEYCHOICE_TOPLINE, "top-line"},
{ MODEKEYCHOICE_TREE_COLLAPSE, "tree-collapse" },
{ MODEKEYCHOICE_TREE_COLLAPSE_ALL, "tree-collapse-all" },
{ MODEKEYCHOICE_TREE_EXPAND, "tree-expand" },
{ MODEKEYCHOICE_TREE_EXPAND_ALL, "tree-expand-all" },
{ MODEKEYCHOICE_TREE_TOGGLE, "tree-toggle" },
{ MODEKEYCHOICE_UP, "up" },
{ 0, NULL }
};
/* vi choice selection keys. */
static const struct mode_key_entry mode_key_vi_choice[] = {
{ '0' | KEYC_ESCAPE, MODEKEYCHOICE_STARTNUMBERPREFIX },
{ '1' | KEYC_ESCAPE, MODEKEYCHOICE_STARTNUMBERPREFIX },
{ '2' | KEYC_ESCAPE, MODEKEYCHOICE_STARTNUMBERPREFIX },
{ '3' | KEYC_ESCAPE, MODEKEYCHOICE_STARTNUMBERPREFIX },
{ '4' | KEYC_ESCAPE, MODEKEYCHOICE_STARTNUMBERPREFIX },
{ '5' | KEYC_ESCAPE, MODEKEYCHOICE_STARTNUMBERPREFIX },
{ '6' | KEYC_ESCAPE, MODEKEYCHOICE_STARTNUMBERPREFIX },
{ '7' | KEYC_ESCAPE, MODEKEYCHOICE_STARTNUMBERPREFIX },
{ '8' | KEYC_ESCAPE, MODEKEYCHOICE_STARTNUMBERPREFIX },
{ '9' | KEYC_ESCAPE, MODEKEYCHOICE_STARTNUMBERPREFIX },
{ '\002' /* C-b */, MODEKEYCHOICE_PAGEUP },
{ '\003' /* C-c */, MODEKEYCHOICE_CANCEL },
{ '\005' /* C-e */, MODEKEYCHOICE_SCROLLDOWN },
{ '\006' /* C-f */, MODEKEYCHOICE_PAGEDOWN },
{ '\031' /* C-y */, MODEKEYCHOICE_SCROLLUP },
{ '\n', MODEKEYCHOICE_CHOOSE },
{ '\r', MODEKEYCHOICE_CHOOSE },
{ 'j', MODEKEYCHOICE_DOWN },
{ 'k', MODEKEYCHOICE_UP },
{ 'q', MODEKEYCHOICE_CANCEL },
{ KEYC_HOME, MODEKEYCHOICE_STARTOFLIST },
{ 'g', MODEKEYCHOICE_STARTOFLIST },
{ 'H', MODEKEYCHOICE_TOPLINE },
{ 'L', MODEKEYCHOICE_BOTTOMLINE },
{ 'G', MODEKEYCHOICE_ENDOFLIST },
{ KEYC_END, MODEKEYCHOICE_ENDOFLIST },
{ KEYC_BSPACE, MODEKEYCHOICE_BACKSPACE },
{ KEYC_DOWN | KEYC_CTRL, MODEKEYCHOICE_SCROLLDOWN },
{ KEYC_DOWN, MODEKEYCHOICE_DOWN },
{ KEYC_NPAGE, MODEKEYCHOICE_PAGEDOWN },
{ KEYC_PPAGE, MODEKEYCHOICE_PAGEUP },
{ KEYC_UP | KEYC_CTRL, MODEKEYCHOICE_SCROLLUP },
{ KEYC_UP, MODEKEYCHOICE_UP },
{ ' ', MODEKEYCHOICE_TREE_TOGGLE },
{ KEYC_LEFT, MODEKEYCHOICE_TREE_COLLAPSE },
{ KEYC_RIGHT, MODEKEYCHOICE_TREE_EXPAND },
{ KEYC_LEFT | KEYC_CTRL, MODEKEYCHOICE_TREE_COLLAPSE_ALL },
{ KEYC_RIGHT | KEYC_CTRL, MODEKEYCHOICE_TREE_EXPAND_ALL },
{ KEYC_MOUSEDOWN1_PANE, MODEKEYCHOICE_CHOOSE },
{ KEYC_MOUSEDOWN3_PANE, MODEKEYCHOICE_TREE_TOGGLE },
{ KEYC_WHEELUP_PANE, MODEKEYCHOICE_UP },
{ KEYC_WHEELDOWN_PANE, MODEKEYCHOICE_DOWN },
{ KEYC_NONE, -1 }
};
struct mode_key_tree mode_key_tree_vi_choice;
/* emacs choice selection keys. */
static const struct mode_key_entry mode_key_emacs_choice[] = {
{ '0' | KEYC_ESCAPE, MODEKEYCHOICE_STARTNUMBERPREFIX },
{ '1' | KEYC_ESCAPE, MODEKEYCHOICE_STARTNUMBERPREFIX },
{ '2' | KEYC_ESCAPE, MODEKEYCHOICE_STARTNUMBERPREFIX },
{ '3' | KEYC_ESCAPE, MODEKEYCHOICE_STARTNUMBERPREFIX },
{ '4' | KEYC_ESCAPE, MODEKEYCHOICE_STARTNUMBERPREFIX },
{ '5' | KEYC_ESCAPE, MODEKEYCHOICE_STARTNUMBERPREFIX },
{ '6' | KEYC_ESCAPE, MODEKEYCHOICE_STARTNUMBERPREFIX },
{ '7' | KEYC_ESCAPE, MODEKEYCHOICE_STARTNUMBERPREFIX },
{ '8' | KEYC_ESCAPE, MODEKEYCHOICE_STARTNUMBERPREFIX },
{ '9' | KEYC_ESCAPE, MODEKEYCHOICE_STARTNUMBERPREFIX },
{ '\003' /* C-c */, MODEKEYCHOICE_CANCEL },
{ '\016' /* C-n */, MODEKEYCHOICE_DOWN },
{ '\020' /* C-p */, MODEKEYCHOICE_UP },
{ '\026' /* C-v */, MODEKEYCHOICE_PAGEDOWN },
{ '\033' /* Escape */, MODEKEYCHOICE_CANCEL },
{ '\n', MODEKEYCHOICE_CHOOSE },
{ '\r', MODEKEYCHOICE_CHOOSE },
{ 'q', MODEKEYCHOICE_CANCEL },
{ 'v' | KEYC_ESCAPE, MODEKEYCHOICE_PAGEUP },
{ KEYC_HOME, MODEKEYCHOICE_STARTOFLIST },
{ '<' | KEYC_ESCAPE, MODEKEYCHOICE_STARTOFLIST },
{ 'R' | KEYC_ESCAPE, MODEKEYCHOICE_TOPLINE },
{ '>' | KEYC_ESCAPE, MODEKEYCHOICE_ENDOFLIST },
{ KEYC_END, MODEKEYCHOICE_ENDOFLIST },
{ KEYC_BSPACE, MODEKEYCHOICE_BACKSPACE },
{ KEYC_DOWN | KEYC_CTRL, MODEKEYCHOICE_SCROLLDOWN },
{ KEYC_DOWN, MODEKEYCHOICE_DOWN },
{ KEYC_NPAGE, MODEKEYCHOICE_PAGEDOWN },
{ KEYC_PPAGE, MODEKEYCHOICE_PAGEUP },
{ KEYC_UP | KEYC_CTRL, MODEKEYCHOICE_SCROLLUP },
{ KEYC_UP, MODEKEYCHOICE_UP },
{ ' ', MODEKEYCHOICE_TREE_TOGGLE },
{ KEYC_LEFT, MODEKEYCHOICE_TREE_COLLAPSE },
{ KEYC_RIGHT, MODEKEYCHOICE_TREE_EXPAND },
{ KEYC_LEFT | KEYC_CTRL, MODEKEYCHOICE_TREE_COLLAPSE_ALL },
{ KEYC_RIGHT | KEYC_CTRL, MODEKEYCHOICE_TREE_EXPAND_ALL },
{ KEYC_MOUSEDOWN1_PANE, MODEKEYCHOICE_CHOOSE },
{ KEYC_MOUSEDOWN3_PANE, MODEKEYCHOICE_TREE_TOGGLE },
{ KEYC_WHEELUP_PANE, MODEKEYCHOICE_UP },
{ KEYC_WHEELDOWN_PANE, MODEKEYCHOICE_DOWN },
{ KEYC_NONE, -1 }
};
struct mode_key_tree mode_key_tree_emacs_choice;
/* Table mapping key table names to default settings and trees. */
static const struct mode_key_table mode_key_tables[] = {
{ "vi-choice", mode_key_cmdstr_choice,
&mode_key_tree_vi_choice, mode_key_vi_choice },
{ "emacs-choice", mode_key_cmdstr_choice,
&mode_key_tree_emacs_choice, mode_key_emacs_choice },
{ NULL, NULL, NULL, NULL }
};
RB_GENERATE(mode_key_tree, mode_key_binding, entry, mode_key_cmp);
int
mode_key_cmp(struct mode_key_binding *mbind1, struct mode_key_binding *mbind2)
{
if (mbind1->key < mbind2->key)
return (-1);
if (mbind1->key > mbind2->key)
return (1);
return (0);
}
const char *
mode_key_tostring(const struct mode_key_cmdstr *cmdstr, enum mode_key_cmd cmd)
{
for (; cmdstr->name != NULL; cmdstr++) {
if (cmdstr->cmd == cmd)
return (cmdstr->name);
}
return (NULL);
}
enum mode_key_cmd
mode_key_fromstring(const struct mode_key_cmdstr *cmdstr, const char *name)
{
for (; cmdstr->name != NULL; cmdstr++) {
if (strcasecmp(cmdstr->name, name) == 0)
return (cmdstr->cmd);
}
return (MODEKEY_NONE);
}
const struct mode_key_table *
mode_key_findtable(const char *name)
{
const struct mode_key_table *mtab;
for (mtab = mode_key_tables; mtab->name != NULL; mtab++) {
if (strcasecmp(name, mtab->name) == 0)
return (mtab);
}
return (NULL);
}
void
mode_key_init_trees(void)
{
const struct mode_key_table *mtab;
const struct mode_key_entry *ment;
struct mode_key_binding *mbind;
for (mtab = mode_key_tables; mtab->name != NULL; mtab++) {
RB_INIT(mtab->tree);
for (ment = mtab->table; ment->key != KEYC_NONE; ment++) {
mbind = xmalloc(sizeof *mbind);
mbind->key = ment->key;
mbind->cmd = ment->cmd;
RB_INSERT(mode_key_tree, mtab->tree, mbind);
}
}
}
void
mode_key_init(struct mode_key_data *mdata, struct mode_key_tree *mtree)
{
mdata->tree = mtree;
}
enum mode_key_cmd
mode_key_lookup(struct mode_key_data *mdata, key_code key)
{
struct mode_key_binding *mbind, mtmp;
mtmp.key = key;
if ((mbind = RB_FIND(mode_key_tree, mdata->tree, &mtmp)) == NULL)
return (MODEKEY_OTHER);
return (mbind->cmd);
}

View File

@ -26,6 +26,7 @@
#include <errno.h>
#include <event.h>
#include <fcntl.h>
#include <paths.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
@ -147,7 +148,6 @@ server_start(struct event_base *base, int lockfd, char *lockfile)
if (log_get_level() > 3)
tty_create_log();
if (pledge("stdio rpath wpath cpath fattr unix getpw recvfd proc exec "
"tty ps", NULL) != 0)
fatal("pledge failed");
@ -157,7 +157,6 @@ server_start(struct event_base *base, int lockfd, char *lockfile)
TAILQ_INIT(&clients);
RB_INIT(&sessions);
TAILQ_INIT(&session_groups);
mode_key_init_trees();
key_bindings_init();
gettimeofday(&start_time, NULL);

View File

@ -74,8 +74,6 @@ struct window_choose_mode_item {
struct window_choose_mode_data {
struct screen screen;
struct mode_key_data mdata;
struct window_choose_mode_item *list;
u_int list_size;
struct window_choose_mode_item *old_list;
@ -91,11 +89,16 @@ struct window_choose_mode_data {
void (*callbackfn)(struct window_choose_data *);
};
static const char window_choose_keys_emacs[] = "0123456789"
"abcdefghijklmnoprstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static const char window_choose_keys_vi[] = "0123456789"
"abcdefhilmnoprstuvwxyz"
"ABCDEFIJKMNOPQRSTUVWXYZ";
static void window_choose_free1(struct window_choose_mode_data *);
static int window_choose_key_index(struct window_choose_mode_data *,
u_int);
static int window_choose_index_key(struct window_choose_mode_data *,
key_code);
static int window_choose_key_index(struct window_pane *, u_int);
static int window_choose_index_key(struct window_pane *, key_code);
static void window_choose_prompt_input(enum window_choose_input_type,
const char *, struct window_pane *, key_code);
static void window_choose_reset_top(struct window_pane *, u_int);
@ -168,7 +171,6 @@ window_choose_init(struct window_pane *wp)
{
struct window_choose_mode_data *data;
struct screen *s;
int keys;
wp->modedata = data = xcalloc(1, sizeof *data);
@ -189,12 +191,6 @@ window_choose_init(struct window_pane *wp)
screen_init(s, screen_size_x(&wp->base), screen_size_y(&wp->base), 0);
s->mode &= ~MODE_CURSOR;
keys = options_get_number(wp->window->options, "mode-keys");
if (keys == MODEKEY_EMACS)
mode_key_init(&data->mdata, &mode_key_tree_emacs_choice);
else
mode_key_init(&data->mdata, &mode_key_tree_vi_choice);
return (s);
}
@ -547,9 +543,74 @@ window_choose_get_item(struct window_pane *wp, key_code key,
return (&data->list[idx]);
}
static key_code
window_choose_translate_key(key_code key)
{
switch (key) {
case '0'|KEYC_ESCAPE:
case '1'|KEYC_ESCAPE:
case '2'|KEYC_ESCAPE:
case '3'|KEYC_ESCAPE:
case '4'|KEYC_ESCAPE:
case '5'|KEYC_ESCAPE:
case '6'|KEYC_ESCAPE:
case '7'|KEYC_ESCAPE:
case '8'|KEYC_ESCAPE:
case '9'|KEYC_ESCAPE:
case '\003': /* C-c */
case 'q':
case '\n':
case '\r':
case KEYC_BSPACE:
case ' ':
case KEYC_LEFT:
case KEYC_RIGHT:
case KEYC_LEFT|KEYC_CTRL:
case KEYC_RIGHT|KEYC_CTRL:
case KEYC_MOUSEDOWN1_PANE:
case KEYC_MOUSEDOWN3_PANE:
case KEYC_WHEELUP_PANE:
case KEYC_WHEELDOWN_PANE:
return (key);
case '\031': /* C-y */
case KEYC_UP|KEYC_CTRL:
return (KEYC_UP|KEYC_CTRL);
case '\002': /* C-b */
case KEYC_PPAGE:
return (KEYC_PPAGE);
case '\005': /* C-e */
case KEYC_DOWN|KEYC_CTRL:
return (KEYC_DOWN|KEYC_CTRL);
case '\006': /* C-f */
case KEYC_NPAGE:
return (KEYC_NPAGE);
case 'j':
case KEYC_DOWN:
return (KEYC_DOWN);
case 'k':
case KEYC_UP:
return (KEYC_UP);
case 'g':
case KEYC_HOME:
return (KEYC_HOME);
case 'G':
case KEYC_END:
return (KEYC_END);
case 'H':
return ('R'|KEYC_ESCAPE);
case 'L':
return ('r'|KEYC_ESCAPE);
}
if ((key >= '0' && key <= '9') ||
(key >= 'a' && key <= 'z') ||
(key >= 'A' && key <= 'Z'))
return (key);
return (KEYC_NONE);
}
static void
window_choose_key(struct window_pane *wp, __unused struct client *c,
__unused struct session *sess, key_code key, struct mouse_event *m)
__unused struct session *sp, key_code key, struct mouse_event *m)
{
struct window_choose_mode_data *data = wp->modedata;
struct screen *s = &data->screen;
@ -557,17 +618,26 @@ window_choose_key(struct window_pane *wp, __unused struct client *c,
struct window_choose_mode_item *item;
size_t input_len;
u_int items, n;
int idx;
int idx, keys;
keys = options_get_number(wp->window->options, "mode-keys");
if (keys == MODEKEY_VI) {
key = window_choose_translate_key(key);
if (key == KEYC_NONE)
return;
}
items = data->list_size;
if (data->input_type == WINDOW_CHOOSE_GOTO_ITEM) {
switch (mode_key_lookup(&data->mdata, key)) {
case MODEKEYCHOICE_CANCEL:
switch (key) {
case '\003': /* C-c */
case '\033': /* Escape */
case 'q':
data->input_type = WINDOW_CHOOSE_NORMAL;
window_choose_redraw_screen(wp);
break;
case MODEKEYCHOICE_CHOOSE:
case '\n':
case '\r':
n = strtonum(data->input_str, 0, INT_MAX, NULL);
if (n > items - 1) {
data->input_type = WINDOW_CHOOSE_NORMAL;
@ -576,7 +646,7 @@ window_choose_key(struct window_pane *wp, __unused struct client *c,
}
window_choose_fire_callback(wp, data->list[n].wcd);
break;
case MODEKEYCHOICE_BACKSPACE:
case KEYC_BSPACE:
input_len = strlen(data->input_str);
if (input_len > 0)
data->input_str[input_len - 1] = '\0';
@ -592,16 +662,21 @@ window_choose_key(struct window_pane *wp, __unused struct client *c,
return;
}
switch (mode_key_lookup(&data->mdata, key)) {
case MODEKEYCHOICE_CANCEL:
switch (key) {
case '\003': /* C-c */
case '\033': /* Escape */
case 'q':
window_choose_fire_callback(wp, NULL);
break;
case MODEKEYCHOICE_CHOOSE:
case '\n':
case '\r':
case KEYC_MOUSEDOWN1_PANE:
if ((item = window_choose_get_item(wp, key, m)) == NULL)
break;
window_choose_fire_callback(wp, item->wcd);
break;
case MODEKEYCHOICE_TREE_TOGGLE:
case ' ':
case KEYC_MOUSEDOWN3_PANE:
if ((item = window_choose_get_item(wp, key, m)) == NULL)
break;
if (item->state & TREE_EXPANDED) {
@ -613,7 +688,7 @@ window_choose_key(struct window_pane *wp, __unused struct client *c,
}
window_choose_redraw_screen(wp);
break;
case MODEKEYCHOICE_TREE_COLLAPSE:
case KEYC_LEFT:
if ((item = window_choose_get_item(wp, key, m)) == NULL)
break;
if (item->state & TREE_EXPANDED) {
@ -622,10 +697,10 @@ window_choose_key(struct window_pane *wp, __unused struct client *c,
window_choose_redraw_screen(wp);
}
break;
case MODEKEYCHOICE_TREE_COLLAPSE_ALL:
case KEYC_LEFT|KEYC_CTRL:
window_choose_collapse_all(wp);
break;
case MODEKEYCHOICE_TREE_EXPAND:
case KEYC_RIGHT:
if ((item = window_choose_get_item(wp, key, m)) == NULL)
break;
if (!(item->state & TREE_EXPANDED)) {
@ -634,10 +709,12 @@ window_choose_key(struct window_pane *wp, __unused struct client *c,
window_choose_redraw_screen(wp);
}
break;
case MODEKEYCHOICE_TREE_EXPAND_ALL:
case KEYC_RIGHT|KEYC_CTRL:
window_choose_expand_all(wp);
break;
case MODEKEYCHOICE_UP:
case '\020': /* C-p */
case KEYC_UP:
case KEYC_WHEELUP_PANE:
if (items == 0)
break;
if (data->selected == 0) {
@ -659,7 +736,9 @@ window_choose_key(struct window_pane *wp, __unused struct client *c,
screen_write_stop(&ctx);
}
break;
case MODEKEYCHOICE_DOWN:
case '\016': /* C-n */
case KEYC_DOWN:
case KEYC_WHEELDOWN_PANE:
if (items == 0)
break;
if (data->selected == items - 1) {
@ -680,7 +759,7 @@ window_choose_key(struct window_pane *wp, __unused struct client *c,
} else
window_choose_scroll_down(wp);
break;
case MODEKEYCHOICE_SCROLLUP:
case KEYC_UP|KEYC_CTRL:
if (items == 0 || data->top == 0)
break;
if (data->selected == data->top + screen_size_y(s) - 1) {
@ -693,7 +772,7 @@ window_choose_key(struct window_pane *wp, __unused struct client *c,
} else
window_choose_scroll_up(wp);
break;
case MODEKEYCHOICE_SCROLLDOWN:
case KEYC_DOWN|KEYC_CTRL:
if (items == 0 ||
data->top + screen_size_y(&data->screen) >= items)
break;
@ -706,7 +785,7 @@ window_choose_key(struct window_pane *wp, __unused struct client *c,
} else
window_choose_scroll_down(wp);
break;
case MODEKEYCHOICE_PAGEUP:
case KEYC_PPAGE:
if (data->selected < screen_size_y(s)) {
data->selected = 0;
data->top = 0;
@ -719,7 +798,7 @@ window_choose_key(struct window_pane *wp, __unused struct client *c,
}
window_choose_redraw_screen(wp);
break;
case MODEKEYCHOICE_PAGEDOWN:
case KEYC_NPAGE:
data->selected += screen_size_y(s);
if (data->selected > items - 1)
data->selected = items - 1;
@ -733,35 +812,46 @@ window_choose_key(struct window_pane *wp, __unused struct client *c,
data->top = data->selected;
window_choose_redraw_screen(wp);
break;
case MODEKEYCHOICE_BACKSPACE:
case KEYC_BSPACE:
input_len = strlen(data->input_str);
if (input_len > 0)
data->input_str[input_len - 1] = '\0';
window_choose_redraw_screen(wp);
break;
case MODEKEYCHOICE_STARTNUMBERPREFIX:
case '0'|KEYC_ESCAPE:
case '1'|KEYC_ESCAPE:
case '2'|KEYC_ESCAPE:
case '3'|KEYC_ESCAPE:
case '4'|KEYC_ESCAPE:
case '5'|KEYC_ESCAPE:
case '6'|KEYC_ESCAPE:
case '7'|KEYC_ESCAPE:
case '8'|KEYC_ESCAPE:
case '9'|KEYC_ESCAPE:
key &= KEYC_MASK_KEY;
if (key < '0' || key > '9')
break;
window_choose_prompt_input(WINDOW_CHOOSE_GOTO_ITEM,
"Goto Item", wp, key);
break;
case MODEKEYCHOICE_STARTOFLIST:
case KEYC_HOME:
case '<'|KEYC_ESCAPE:
data->selected = 0;
data->top = 0;
window_choose_redraw_screen(wp);
break;
case MODEKEYCHOICE_TOPLINE:
case 'R'|KEYC_ESCAPE:
data->selected = data->top;
window_choose_redraw_screen(wp);
break;
case MODEKEYCHOICE_BOTTOMLINE:
case 'r'|KEYC_ESCAPE:
data->selected = data->top + screen_size_y(s) - 1;
if (data->selected > items - 1)
data->selected = items - 1;
window_choose_redraw_screen(wp);
break;
case MODEKEYCHOICE_ENDOFLIST:
case KEYC_END:
case '>'|KEYC_ESCAPE:
data->selected = items - 1;
if (screen_size_y(s) < items)
data->top = items - screen_size_y(s);
@ -770,7 +860,7 @@ window_choose_key(struct window_pane *wp, __unused struct client *c,
window_choose_redraw_screen(wp);
break;
default:
idx = window_choose_index_key(data, key);
idx = window_choose_index_key(wp, key);
if (idx < 0 || (u_int) idx >= data->list_size)
break;
data->selected = idx;
@ -808,7 +898,7 @@ window_choose_write_line(struct window_pane *wp, struct screen_write_ctx *ctx,
item->wcd->wl->flags & WINLINK_ALERTFLAGS)
gc.attr |= GRID_ATTR_BRIGHT;
key = window_choose_key_index(data, data->top + py);
key = window_choose_key_index(wp, data->top + py);
if (key != -1)
xsnprintf(label, sizeof label, "(%c)", key);
else
@ -839,18 +929,17 @@ window_choose_write_line(struct window_pane *wp, struct screen_write_ctx *ctx,
}
static int
window_choose_key_index(struct window_choose_mode_data *data, u_int idx)
window_choose_key_index(struct window_pane *wp, u_int idx)
{
static const char keys[] = "0123456789"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const char *ptr;
int mkey;
const char *ptr;
int keys;
for (ptr = keys; *ptr != '\0'; ptr++) {
mkey = mode_key_lookup(&data->mdata, *ptr);
if (mkey != MODEKEY_NONE && mkey != MODEKEY_OTHER)
continue;
keys = options_get_number(wp->window->options, "mode-keys");
if (keys == MODEKEY_VI)
ptr = window_choose_keys_vi;
else
ptr = window_choose_keys_emacs;
for (; *ptr != '\0'; ptr++) {
if (idx-- == 0)
return (*ptr);
}
@ -858,19 +947,18 @@ window_choose_key_index(struct window_choose_mode_data *data, u_int idx)
}
static int
window_choose_index_key(struct window_choose_mode_data *data, key_code key)
window_choose_index_key(struct window_pane *wp, key_code key)
{
static const char keys[] = "0123456789"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const char *ptr;
int mkey;
u_int idx = 0;
const char *ptr;
int keys;
u_int idx = 0;
for (ptr = keys; *ptr != '\0'; ptr++) {
mkey = mode_key_lookup(&data->mdata, *ptr);
if (mkey != MODEKEY_NONE && mkey != MODEKEY_OTHER)
continue;
keys = options_get_number(wp->window->options, "mode-keys");
if (keys == MODEKEY_VI)
ptr = window_choose_keys_vi;
else
ptr = window_choose_keys_emacs;
for (; *ptr != '\0'; ptr++) {
if (key == (key_code)*ptr)
return (idx);
idx++;