2009-01-28 00:35:44 +01:00
|
|
|
/* $Id: key-bindings.c,v 1.58 2009-01-27 23:35:44 nicm Exp $ */
|
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;
|
|
|
|
|
|
|
|
int
|
|
|
|
key_bindings_cmp(struct key_binding *bd1, struct key_binding *bd2)
|
|
|
|
{
|
|
|
|
return (bd1->key - bd2->key);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-18 15:40:48 +01:00
|
|
|
key_bindings_add(int key, 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-01-06 15:10:32 +01:00
|
|
|
if ((bd = key_bindings_lookup(key)) == NULL) {
|
2007-10-03 23:31:07 +02:00
|
|
|
bd = xmalloc(sizeof *bd);
|
2009-01-06 15:47:56 +01:00
|
|
|
bd->key = key;
|
2009-01-06 15:10:32 +01:00
|
|
|
SPLAY_INSERT(key_bindings, &key_bindings, bd);
|
2008-06-16 09:01:41 +02:00
|
|
|
} else
|
2009-01-18 15:40:48 +01:00
|
|
|
cmd_list_free(bd->cmdlist);
|
|
|
|
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);
|
2007-10-23 12:25:57 +02:00
|
|
|
|
2009-01-18 15:40:48 +01:00
|
|
|
cmd_list_free(bd->cmdlist);
|
2007-10-03 23:31:07 +02:00
|
|
|
xfree(bd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
key_bindings_init(void)
|
|
|
|
{
|
|
|
|
struct {
|
|
|
|
int key;
|
|
|
|
const struct cmd_entry *entry;
|
|
|
|
} table[] = {
|
2009-01-12 00:31:46 +01:00
|
|
|
{ '"', &cmd_split_window_entry },
|
2008-06-25 21:18:20 +02:00
|
|
|
{ '#', &cmd_list_buffers_entry },
|
|
|
|
{ '&', &cmd_kill_window_entry },
|
2009-01-13 02:08:40 +01:00
|
|
|
{ ',', &cmd_command_prompt_entry },
|
2008-06-25 21:18:20 +02:00
|
|
|
{ '-', &cmd_delete_buffer_entry },
|
2008-06-03 07:35:51 +02:00
|
|
|
{ '0', &cmd_select_window_entry },
|
|
|
|
{ '1', &cmd_select_window_entry },
|
|
|
|
{ '2', &cmd_select_window_entry },
|
|
|
|
{ '3', &cmd_select_window_entry },
|
|
|
|
{ '4', &cmd_select_window_entry },
|
|
|
|
{ '5', &cmd_select_window_entry },
|
|
|
|
{ '6', &cmd_select_window_entry },
|
|
|
|
{ '7', &cmd_select_window_entry },
|
|
|
|
{ '8', &cmd_select_window_entry },
|
|
|
|
{ '9', &cmd_select_window_entry },
|
2008-06-25 21:18:20 +02:00
|
|
|
{ ':', &cmd_command_prompt_entry },
|
2008-06-03 07:35:51 +02:00
|
|
|
{ '=', &cmd_scroll_mode_entry },
|
2008-06-25 21:18:20 +02:00
|
|
|
{ '?', &cmd_list_keys_entry },
|
2008-06-03 07:35:51 +02:00
|
|
|
{ '[', &cmd_copy_mode_entry },
|
2009-01-12 00:31:46 +01:00
|
|
|
{ '\'', &cmd_select_prompt_entry },
|
2008-06-03 07:35:51 +02:00
|
|
|
{ ']', &cmd_paste_buffer_entry },
|
2008-06-25 21:18:20 +02:00
|
|
|
{ 'c', &cmd_new_window_entry },
|
|
|
|
{ 'd', &cmd_detach_client_entry },
|
2009-01-18 18:20:52 +01:00
|
|
|
{ 'f', &cmd_command_prompt_entry },
|
2008-06-25 21:18:20 +02:00
|
|
|
{ 'l', &cmd_last_window_entry },
|
|
|
|
{ 'n', &cmd_next_window_entry },
|
2009-01-14 20:56:55 +01:00
|
|
|
{ 'o', &cmd_down_pane_entry },
|
2008-06-25 21:18:20 +02:00
|
|
|
{ 'p', &cmd_previous_window_entry },
|
|
|
|
{ 'r', &cmd_refresh_client_entry },
|
2009-01-15 20:27:31 +01:00
|
|
|
{ 's', &cmd_choose_session_entry },
|
2009-01-10 20:35:40 +01:00
|
|
|
{ 't', &cmd_clock_mode_entry },
|
2009-01-15 20:27:31 +01:00
|
|
|
{ 'w', &cmd_choose_window_entry },
|
2009-01-18 13:09:42 +01:00
|
|
|
{ 'x', &cmd_kill_pane_entry, },
|
|
|
|
{ '\032', &cmd_suspend_client_entry },
|
2009-01-28 00:35:44 +01:00
|
|
|
{ KEYC_PPAGE, &cmd_scroll_mode_entry },
|
2009-01-18 19:31:45 +01:00
|
|
|
{ KEYC_ADDESC('n'), &cmd_next_window_entry },
|
|
|
|
{ KEYC_ADDESC('p'), &cmd_previous_window_entry },
|
2009-01-14 20:41:15 +01:00
|
|
|
{ KEYC_UP, &cmd_up_pane_entry },
|
|
|
|
{ KEYC_DOWN, &cmd_down_pane_entry },
|
2009-01-14 22:08:52 +01:00
|
|
|
{ KEYC_ADDESC(KEYC_UP), &cmd_resize_pane_up_entry },
|
|
|
|
{ KEYC_ADDESC(KEYC_DOWN), &cmd_resize_pane_down_entry },
|
2009-01-12 20:23:14 +01:00
|
|
|
{ KEYC_ADDCTL(KEYC_UP), &cmd_resize_pane_up_entry },
|
|
|
|
{ KEYC_ADDCTL(KEYC_DOWN), &cmd_resize_pane_down_entry },
|
2008-06-03 07:35:51 +02:00
|
|
|
{ META, &cmd_send_prefix_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);
|
|
|
|
TAILQ_INIT(cmdlist);
|
|
|
|
|
2007-10-03 23:31:07 +02:00
|
|
|
cmd = xmalloc(sizeof *cmd);
|
|
|
|
cmd->entry = table[i].entry;
|
|
|
|
cmd->data = NULL;
|
2008-06-03 07:35:51 +02:00
|
|
|
if (cmd->entry->init != NULL)
|
2008-06-05 18:35:32 +02:00
|
|
|
cmd->entry->init(cmd, table[i].key);
|
2009-01-18 15:40:48 +01:00
|
|
|
TAILQ_INSERT_HEAD(cmdlist, cmd, qentry);
|
|
|
|
|
|
|
|
key_bindings_add(table[i].key, cmdlist);
|
2007-10-03 23:31:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
key_bindings_free(void)
|
|
|
|
{
|
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
|
|
|
while (!SPLAY_EMPTY(&key_bindings)) {
|
|
|
|
bd = SPLAY_ROOT(&key_bindings);
|
2009-01-17 19:34:12 +01:00
|
|
|
SPLAY_REMOVE(key_bindings, &key_bindings, bd);
|
2009-01-18 15:40:48 +01:00
|
|
|
cmd_list_free(bd->cmdlist);
|
2007-10-03 23:31:07 +02:00
|
|
|
xfree(bd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2008-06-19 22:45:21 +02:00
|
|
|
server_set_client_message(ctx->curclient, 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-01-12 00:31:46 +01:00
|
|
|
struct winlink *wl = ctx->cursession->curw;
|
2007-10-03 23:31:07 +02:00
|
|
|
va_list ap;
|
2007-11-21 20:44:05 +01:00
|
|
|
|
2009-01-12 00:31:46 +01:00
|
|
|
window_pane_set_mode(wl->window->active, &window_more_mode);
|
2007-10-03 23:31:07 +02:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2009-01-12 00:31:46 +01:00
|
|
|
window_more_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;
|
|
|
|
|
|
|
|
if (be_quiet)
|
|
|
|
return;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
xvasprintf(&msg, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
*msg = toupper((u_char) *msg);
|
2008-06-19 22:45:21 +02:00
|
|
|
server_set_client_message(ctx->curclient, 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
|
|
|
{
|
2009-01-06 15:10:32 +01:00
|
|
|
struct cmd_ctx ctx;
|
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.cursession = c->session;
|
|
|
|
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
|
|
|
|
2009-01-18 15:40:48 +01:00
|
|
|
cmd_list_exec(bd->cmdlist, &ctx);
|
2007-10-03 23:31:07 +02:00
|
|
|
}
|