2011-07-04 10:22:37 +02:00
|
|
|
/* $Id$ */
|
2007-10-03 23:31:07 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
2007-10-04 02:02:10 +02:00
|
|
|
#include <ctype.h>
|
2007-10-03 23:31:07 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2009-01-06 15:10:32 +01:00
|
|
|
SPLAY_GENERATE(key_bindings, key_binding, entry, key_bindings_cmp);
|
|
|
|
|
|
|
|
struct key_bindings key_bindings;
|
2009-07-14 08:39:25 +02:00
|
|
|
struct key_bindings dead_key_bindings;
|
2009-01-06 15:10:32 +01:00
|
|
|
|
|
|
|
int
|
|
|
|
key_bindings_cmp(struct key_binding *bd1, struct key_binding *bd2)
|
|
|
|
{
|
2009-07-25 10:52:04 +02:00
|
|
|
int key1, key2;
|
|
|
|
|
|
|
|
key1 = bd1->key & ~KEYC_PREFIX;
|
|
|
|
key2 = bd2->key & ~KEYC_PREFIX;
|
|
|
|
if (key1 != key2)
|
|
|
|
return (key1 - key2);
|
|
|
|
|
|
|
|
if (bd1->key & KEYC_PREFIX && !(bd2->key & KEYC_PREFIX))
|
|
|
|
return (-1);
|
|
|
|
if (bd2->key & KEYC_PREFIX && !(bd1->key & KEYC_PREFIX))
|
|
|
|
return (1);
|
|
|
|
return (0);
|
2009-01-06 15:10:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct key_binding *
|
|
|
|
key_bindings_lookup(int key)
|
|
|
|
{
|
|
|
|
struct key_binding bd;
|
|
|
|
|
|
|
|
bd.key = key;
|
|
|
|
return (SPLAY_FIND(key_bindings, &key_bindings, &bd));
|
|
|
|
}
|
2007-10-03 23:31:07 +02:00
|
|
|
|
|
|
|
void
|
2009-03-28 15:08:09 +01:00
|
|
|
key_bindings_add(int key, int can_repeat, struct cmd_list *cmdlist)
|
2007-10-03 23:31:07 +02:00
|
|
|
{
|
2009-01-06 15:10:32 +01:00
|
|
|
struct key_binding *bd;
|
2007-10-03 23:31:07 +02:00
|
|
|
|
2009-07-14 08:39:25 +02:00
|
|
|
key_bindings_remove(key);
|
2009-12-04 23:14:47 +01:00
|
|
|
|
2009-07-14 08:39:25 +02:00
|
|
|
bd = xmalloc(sizeof *bd);
|
|
|
|
bd->key = key;
|
|
|
|
SPLAY_INSERT(key_bindings, &key_bindings, bd);
|
2009-12-04 23:14:47 +01:00
|
|
|
|
2009-03-28 15:08:09 +01:00
|
|
|
bd->can_repeat = can_repeat;
|
2009-01-18 15:40:48 +01:00
|
|
|
bd->cmdlist = cmdlist;
|
2007-10-03 23:31:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
key_bindings_remove(int key)
|
|
|
|
{
|
2009-01-06 15:10:32 +01:00
|
|
|
struct key_binding *bd;
|
2007-10-03 23:31:07 +02:00
|
|
|
|
2009-01-06 15:10:32 +01:00
|
|
|
if ((bd = key_bindings_lookup(key)) == NULL)
|
2007-10-03 23:31:07 +02:00
|
|
|
return;
|
2009-01-06 15:10:32 +01:00
|
|
|
SPLAY_REMOVE(key_bindings, &key_bindings, bd);
|
2009-07-14 08:39:25 +02:00
|
|
|
SPLAY_INSERT(key_bindings, &dead_key_bindings, bd);
|
|
|
|
}
|
2007-10-23 12:25:57 +02:00
|
|
|
|
2009-07-14 08:39:25 +02:00
|
|
|
void
|
|
|
|
key_bindings_clean(void)
|
|
|
|
{
|
|
|
|
struct key_binding *bd;
|
|
|
|
|
|
|
|
while (!SPLAY_EMPTY(&dead_key_bindings)) {
|
|
|
|
bd = SPLAY_ROOT(&dead_key_bindings);
|
|
|
|
SPLAY_REMOVE(key_bindings, &dead_key_bindings, bd);
|
|
|
|
cmd_list_free(bd->cmdlist);
|
|
|
|
xfree(bd);
|
|
|
|
}
|
2007-10-03 23:31:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
key_bindings_init(void)
|
|
|
|
{
|
2009-04-30 20:39:34 +02:00
|
|
|
static const struct {
|
2007-10-03 23:31:07 +02:00
|
|
|
int key;
|
2009-03-28 15:08:09 +01:00
|
|
|
int can_repeat;
|
2007-10-03 23:31:07 +02:00
|
|
|
const struct cmd_entry *entry;
|
|
|
|
} table[] = {
|
2009-04-01 20:21:42 +02:00
|
|
|
{ ' ', 0, &cmd_next_layout_entry },
|
2009-03-28 15:08:09 +01:00
|
|
|
{ '!', 0, &cmd_break_pane_entry },
|
2009-12-04 23:14:47 +01:00
|
|
|
{ '"', 0, &cmd_split_window_entry },
|
2009-03-28 15:08:09 +01:00
|
|
|
{ '#', 0, &cmd_list_buffers_entry },
|
2011-07-04 10:22:37 +02:00
|
|
|
{ '$', 0, &cmd_command_prompt_entry },
|
2009-12-04 23:14:47 +01:00
|
|
|
{ '%', 0, &cmd_split_window_entry },
|
2009-04-27 15:21:16 +02:00
|
|
|
{ '&', 0, &cmd_confirm_before_entry },
|
2010-09-10 15:36:17 +02:00
|
|
|
{ '(', 0, &cmd_switch_client_entry },
|
|
|
|
{ ')', 0, &cmd_switch_client_entry },
|
2009-03-28 15:08:09 +01:00
|
|
|
{ ',', 0, &cmd_command_prompt_entry },
|
|
|
|
{ '-', 0, &cmd_delete_buffer_entry },
|
|
|
|
{ '.', 0, &cmd_command_prompt_entry },
|
|
|
|
{ '0', 0, &cmd_select_window_entry },
|
|
|
|
{ '1', 0, &cmd_select_window_entry },
|
|
|
|
{ '2', 0, &cmd_select_window_entry },
|
|
|
|
{ '3', 0, &cmd_select_window_entry },
|
|
|
|
{ '4', 0, &cmd_select_window_entry },
|
|
|
|
{ '5', 0, &cmd_select_window_entry },
|
|
|
|
{ '6', 0, &cmd_select_window_entry },
|
|
|
|
{ '7', 0, &cmd_select_window_entry },
|
|
|
|
{ '8', 0, &cmd_select_window_entry },
|
|
|
|
{ '9', 0, &cmd_select_window_entry },
|
|
|
|
{ ':', 0, &cmd_command_prompt_entry },
|
2010-10-24 03:34:30 +02:00
|
|
|
{ ';', 0, &cmd_last_pane_entry },
|
2010-06-23 01:35:20 +02:00
|
|
|
{ '=', 0, &cmd_choose_buffer_entry },
|
2009-03-28 15:08:09 +01:00
|
|
|
{ '?', 0, &cmd_list_keys_entry },
|
2009-11-19 23:20:04 +01:00
|
|
|
{ 'D', 0, &cmd_choose_client_entry },
|
2010-12-11 19:42:20 +01:00
|
|
|
{ 'L', 0, &cmd_switch_client_entry },
|
2009-03-28 15:08:09 +01:00
|
|
|
{ '[', 0, &cmd_copy_mode_entry },
|
2010-05-14 16:33:39 +02:00
|
|
|
{ '\'', 0, &cmd_command_prompt_entry },
|
2009-11-19 23:20:04 +01:00
|
|
|
{ '\002', /* C-b */ 0, &cmd_send_prefix_entry },
|
|
|
|
{ '\017', /* C-o */ 0, &cmd_rotate_window_entry },
|
2009-04-03 01:28:16 +02:00
|
|
|
{ '\032', /* C-z */ 0, &cmd_suspend_client_entry },
|
2009-03-28 15:08:09 +01:00
|
|
|
{ ']', 0, &cmd_paste_buffer_entry },
|
|
|
|
{ 'c', 0, &cmd_new_window_entry },
|
|
|
|
{ 'd', 0, &cmd_detach_client_entry },
|
|
|
|
{ 'f', 0, &cmd_command_prompt_entry },
|
2009-07-17 20:32:54 +02:00
|
|
|
{ 'i', 0, &cmd_display_message_entry },
|
2009-03-28 15:08:09 +01:00
|
|
|
{ 'l', 0, &cmd_last_window_entry },
|
|
|
|
{ 'n', 0, &cmd_next_window_entry },
|
2010-03-15 23:03:38 +01:00
|
|
|
{ 'o', 0, &cmd_select_pane_entry },
|
2009-03-28 15:08:09 +01:00
|
|
|
{ 'p', 0, &cmd_previous_window_entry },
|
2009-09-01 00:30:15 +02:00
|
|
|
{ 'q', 0, &cmd_display_panes_entry },
|
2009-03-28 15:08:09 +01:00
|
|
|
{ 'r', 0, &cmd_refresh_client_entry },
|
|
|
|
{ 's', 0, &cmd_choose_session_entry },
|
|
|
|
{ 't', 0, &cmd_clock_mode_entry },
|
|
|
|
{ 'w', 0, &cmd_choose_window_entry },
|
2009-04-27 15:21:16 +02:00
|
|
|
{ 'x', 0, &cmd_confirm_before_entry },
|
2009-04-03 01:28:16 +02:00
|
|
|
{ '{', 0, &cmd_swap_pane_entry },
|
|
|
|
{ '}', 0, &cmd_swap_pane_entry },
|
2009-11-19 23:20:04 +01:00
|
|
|
{ '~', 0, &cmd_show_messages_entry },
|
2009-07-22 18:24:59 +02:00
|
|
|
{ '1' | KEYC_ESCAPE, 0, &cmd_select_layout_entry },
|
|
|
|
{ '2' | KEYC_ESCAPE, 0, &cmd_select_layout_entry },
|
|
|
|
{ '3' | KEYC_ESCAPE, 0, &cmd_select_layout_entry },
|
|
|
|
{ '4' | KEYC_ESCAPE, 0, &cmd_select_layout_entry },
|
2010-05-14 16:16:37 +02:00
|
|
|
{ '5' | KEYC_ESCAPE, 0, &cmd_select_layout_entry },
|
2009-10-06 16:14:07 +02:00
|
|
|
{ KEYC_PPAGE, 0, &cmd_copy_mode_entry },
|
2009-07-22 18:24:59 +02:00
|
|
|
{ 'n' | KEYC_ESCAPE, 0, &cmd_next_window_entry },
|
2009-11-19 23:20:04 +01:00
|
|
|
{ 'o' | KEYC_ESCAPE, 0, &cmd_rotate_window_entry },
|
2009-07-22 18:24:59 +02:00
|
|
|
{ 'p' | KEYC_ESCAPE, 0, &cmd_previous_window_entry },
|
2010-03-15 23:03:38 +01:00
|
|
|
{ KEYC_UP, 1, &cmd_select_pane_entry },
|
|
|
|
{ KEYC_DOWN, 1, &cmd_select_pane_entry },
|
|
|
|
{ KEYC_LEFT, 1, &cmd_select_pane_entry },
|
|
|
|
{ KEYC_RIGHT, 1, &cmd_select_pane_entry },
|
2009-07-22 18:24:59 +02:00
|
|
|
{ KEYC_UP | KEYC_ESCAPE, 1, &cmd_resize_pane_entry },
|
|
|
|
{ KEYC_DOWN | KEYC_ESCAPE, 1, &cmd_resize_pane_entry },
|
|
|
|
{ KEYC_LEFT | KEYC_ESCAPE, 1, &cmd_resize_pane_entry },
|
|
|
|
{ KEYC_RIGHT | KEYC_ESCAPE, 1, &cmd_resize_pane_entry },
|
|
|
|
{ KEYC_UP | KEYC_CTRL, 1, &cmd_resize_pane_entry },
|
2009-12-04 23:14:47 +01:00
|
|
|
{ KEYC_DOWN | KEYC_CTRL, 1, &cmd_resize_pane_entry },
|
2009-07-22 18:24:59 +02:00
|
|
|
{ KEYC_LEFT | KEYC_CTRL, 1, &cmd_resize_pane_entry },
|
|
|
|
{ KEYC_RIGHT | KEYC_CTRL, 1, &cmd_resize_pane_entry },
|
2007-10-03 23:31:07 +02:00
|
|
|
};
|
|
|
|
u_int i;
|
|
|
|
struct cmd *cmd;
|
2009-01-18 15:40:48 +01:00
|
|
|
struct cmd_list *cmdlist;
|
2007-10-03 23:31:07 +02:00
|
|
|
|
2009-01-06 15:10:32 +01:00
|
|
|
SPLAY_INIT(&key_bindings);
|
2007-10-04 02:18:59 +02:00
|
|
|
|
2009-01-07 23:52:33 +01:00
|
|
|
for (i = 0; i < nitems(table); i++) {
|
2009-01-18 15:40:48 +01:00
|
|
|
cmdlist = xmalloc(sizeof *cmdlist);
|
2010-07-02 04:43:01 +02:00
|
|
|
TAILQ_INIT(&cmdlist->list);
|
|
|
|
cmdlist->references = 1;
|
2009-01-18 15:40:48 +01:00
|
|
|
|
2007-10-03 23:31:07 +02:00
|
|
|
cmd = xmalloc(sizeof *cmd);
|
|
|
|
cmd->entry = table[i].entry;
|
2011-01-07 15:45:34 +01:00
|
|
|
if (cmd->entry->key_binding != NULL)
|
|
|
|
cmd->entry->key_binding(cmd, table[i].key);
|
|
|
|
else
|
|
|
|
cmd->args = args_create(0);
|
2010-07-02 04:43:01 +02:00
|
|
|
TAILQ_INSERT_HEAD(&cmdlist->list, cmd, qentry);
|
2009-01-18 15:40:48 +01:00
|
|
|
|
2009-07-25 10:52:04 +02:00
|
|
|
key_bindings_add(
|
|
|
|
table[i].key | KEYC_PREFIX, table[i].can_repeat, cmdlist);
|
2007-10-03 23:31:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-20 19:11:37 +01:00
|
|
|
void printflike2
|
2007-10-03 23:31:07 +02:00
|
|
|
key_bindings_error(struct cmd_ctx *ctx, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char *msg;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
xvasprintf(&msg, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
2007-10-04 02:02:10 +02:00
|
|
|
*msg = toupper((u_char) *msg);
|
2009-12-04 23:14:47 +01:00
|
|
|
status_message_set(ctx->curclient, "%s", msg);
|
2007-10-03 23:31:07 +02:00
|
|
|
xfree(msg);
|
|
|
|
}
|
|
|
|
|
2007-11-20 19:11:37 +01:00
|
|
|
void printflike2
|
2007-10-03 23:31:07 +02:00
|
|
|
key_bindings_print(struct cmd_ctx *ctx, const char *fmt, ...)
|
|
|
|
{
|
2009-08-24 18:24:18 +02:00
|
|
|
struct winlink *wl = ctx->curclient->session->curw;
|
2007-10-03 23:31:07 +02:00
|
|
|
va_list ap;
|
2007-11-21 20:44:05 +01:00
|
|
|
|
2010-04-05 07:11:44 +02:00
|
|
|
if (wl->window->active->mode != &window_copy_mode) {
|
2009-02-13 21:19:30 +01:00
|
|
|
window_pane_reset_mode(wl->window->active);
|
2010-04-05 07:11:44 +02:00
|
|
|
window_pane_set_mode(wl->window->active, &window_copy_mode);
|
|
|
|
window_copy_init_for_output(wl->window->active);
|
|
|
|
}
|
2007-10-03 23:31:07 +02:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2010-04-05 07:11:44 +02:00
|
|
|
window_copy_vadd(wl->window->active, fmt, ap);
|
2007-10-03 23:31:07 +02:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2008-06-16 19:35:40 +02:00
|
|
|
void printflike2
|
|
|
|
key_bindings_info(struct cmd_ctx *ctx, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char *msg;
|
|
|
|
|
2009-12-10 17:59:02 +01:00
|
|
|
if (options_get_number(&global_options, "quiet"))
|
2008-06-16 19:35:40 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
xvasprintf(&msg, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
*msg = toupper((u_char) *msg);
|
2009-12-04 23:14:47 +01:00
|
|
|
status_message_set(ctx->curclient, "%s", msg);
|
2008-06-16 19:35:40 +02:00
|
|
|
xfree(msg);
|
|
|
|
}
|
|
|
|
|
2007-10-03 23:31:07 +02:00
|
|
|
void
|
2009-01-14 23:13:30 +01:00
|
|
|
key_bindings_dispatch(struct key_binding *bd, struct client *c)
|
2007-10-03 23:31:07 +02:00
|
|
|
{
|
2010-02-08 19:27:34 +01:00
|
|
|
struct cmd_ctx ctx;
|
|
|
|
struct cmd *cmd;
|
|
|
|
int readonly;
|
2007-10-03 23:31:07 +02:00
|
|
|
|
2008-06-02 20:23:37 +02:00
|
|
|
ctx.msgdata = NULL;
|
2008-06-02 20:08:17 +02:00
|
|
|
ctx.curclient = c;
|
2007-10-03 23:31:07 +02:00
|
|
|
|
|
|
|
ctx.error = key_bindings_error;
|
|
|
|
ctx.print = key_bindings_print;
|
2008-06-16 19:35:40 +02:00
|
|
|
ctx.info = key_bindings_info;
|
2007-10-03 23:31:07 +02:00
|
|
|
|
2007-11-16 22:12:31 +01:00
|
|
|
ctx.cmdclient = NULL;
|
2007-10-03 23:31:07 +02:00
|
|
|
|
2010-02-08 19:27:34 +01:00
|
|
|
readonly = 1;
|
2010-07-02 04:43:01 +02:00
|
|
|
TAILQ_FOREACH(cmd, &bd->cmdlist->list, qentry) {
|
2010-02-08 19:27:34 +01:00
|
|
|
if (!(cmd->entry->flags & CMD_READONLY))
|
|
|
|
readonly = 0;
|
|
|
|
}
|
|
|
|
if (!readonly && c->flags & CLIENT_READONLY) {
|
|
|
|
key_bindings_info(&ctx, "Client is read-only");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-01-18 15:40:48 +01:00
|
|
|
cmd_list_exec(bd->cmdlist, &ctx);
|
2007-10-03 23:31:07 +02:00
|
|
|
}
|