2008-06-19 23:28:41 +02:00
|
|
|
/* $Id: key-bindings.c,v 1.33 2008-06-19 21:28:41 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"
|
|
|
|
|
2007-10-04 02:18:59 +02:00
|
|
|
struct bindings key_bindings;
|
2007-10-03 23:31:07 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
key_bindings_add(int key, struct cmd *cmd)
|
|
|
|
{
|
|
|
|
struct binding *bd;
|
|
|
|
u_int i;
|
|
|
|
|
|
|
|
bd = NULL;
|
|
|
|
for (i = 0; i < ARRAY_LENGTH(&key_bindings); i++) {
|
|
|
|
bd = ARRAY_ITEM(&key_bindings, i);
|
|
|
|
if (bd->key == key)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i == ARRAY_LENGTH(&key_bindings)) {
|
|
|
|
bd = xmalloc(sizeof *bd);
|
|
|
|
ARRAY_ADD(&key_bindings, bd);
|
2008-06-16 09:01:41 +02:00
|
|
|
} else
|
|
|
|
cmd_free(bd->cmd);
|
2007-10-03 23:31:07 +02:00
|
|
|
|
|
|
|
bd->key = key;
|
|
|
|
bd->cmd = cmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
key_bindings_remove(int key)
|
|
|
|
{
|
|
|
|
struct binding *bd;
|
|
|
|
u_int i;
|
|
|
|
|
|
|
|
bd = NULL;
|
|
|
|
for (i = 0; i < ARRAY_LENGTH(&key_bindings); i++) {
|
|
|
|
bd = ARRAY_ITEM(&key_bindings, i);
|
|
|
|
if (bd->key == key)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i == ARRAY_LENGTH(&key_bindings))
|
|
|
|
return;
|
|
|
|
|
2007-10-23 12:25:57 +02:00
|
|
|
ARRAY_REMOVE(&key_bindings, i);
|
|
|
|
|
2007-10-03 23:31:07 +02:00
|
|
|
cmd_free(bd->cmd);
|
|
|
|
xfree(bd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
key_bindings_init(void)
|
|
|
|
{
|
|
|
|
struct {
|
|
|
|
int key;
|
|
|
|
const struct cmd_entry *entry;
|
|
|
|
} table[] = {
|
2008-06-03 07:35:51 +02:00
|
|
|
{ 'D', &cmd_detach_client_entry },
|
|
|
|
{ 'd', &cmd_detach_client_entry },
|
|
|
|
{ 'S', &cmd_list_sessions_entry },
|
|
|
|
{ 's', &cmd_list_sessions_entry },
|
|
|
|
{ 'W', &cmd_list_windows_entry },
|
|
|
|
{ 'w', &cmd_list_windows_entry },
|
|
|
|
{ '?', &cmd_list_keys_entry },
|
|
|
|
{ '/', &cmd_list_keys_entry },
|
|
|
|
{ 'C', &cmd_new_window_entry },
|
|
|
|
{ 'c', &cmd_new_window_entry },
|
|
|
|
{ 'N', &cmd_next_window_entry },
|
|
|
|
{ 'n', &cmd_next_window_entry },
|
|
|
|
{ 'P', &cmd_previous_window_entry },
|
|
|
|
{ 'p', &cmd_previous_window_entry },
|
|
|
|
{ 'L', &cmd_last_window_entry },
|
|
|
|
{ 'l', &cmd_last_window_entry },
|
|
|
|
{ '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 },
|
|
|
|
{ 'R', &cmd_refresh_client_entry },
|
|
|
|
{ 'r', &cmd_refresh_client_entry },
|
|
|
|
{ '&', &cmd_kill_window_entry },
|
|
|
|
{ '=', &cmd_scroll_mode_entry },
|
|
|
|
{ '[', &cmd_copy_mode_entry },
|
|
|
|
{ ']', &cmd_paste_buffer_entry },
|
2008-06-19 22:45:21 +02:00
|
|
|
{ ':', &cmd_command_prompt_entry },
|
2008-06-19 23:28:41 +02:00
|
|
|
{ ';', &cmd_command_prompt_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;
|
|
|
|
|
2007-10-04 02:18:59 +02:00
|
|
|
ARRAY_INIT(&key_bindings);
|
|
|
|
|
2007-10-03 23:31:07 +02:00
|
|
|
for (i = 0; i < (sizeof table / sizeof table[0]); i++) {
|
|
|
|
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);
|
2007-10-03 23:31:07 +02:00
|
|
|
key_bindings_add(table[i].key, cmd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
key_bindings_free(void)
|
|
|
|
{
|
|
|
|
struct binding *bd;
|
|
|
|
u_int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_LENGTH(&key_bindings); i++) {
|
|
|
|
bd = ARRAY_ITEM(&key_bindings, i);
|
|
|
|
|
|
|
|
cmd_free(bd->cmd);
|
|
|
|
xfree(bd);
|
|
|
|
}
|
2007-12-06 10:46:23 +01:00
|
|
|
|
2007-10-04 13:55:55 +02:00
|
|
|
ARRAY_FREE(&key_bindings);
|
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);
|
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, ...)
|
|
|
|
{
|
2008-06-02 20:08:17 +02:00
|
|
|
struct window *w = ctx->cursession->curw->window;
|
2007-10-03 23:31:07 +02:00
|
|
|
va_list ap;
|
2007-11-21 20:44:05 +01:00
|
|
|
|
2007-12-06 22:42:00 +01:00
|
|
|
window_set_mode(w, &window_more_mode);
|
2007-10-03 23:31:07 +02:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2007-11-21 20:44:05 +01:00
|
|
|
window_more_vadd(w, 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
|
|
|
|
key_bindings_dispatch(int key, struct client *c)
|
|
|
|
{
|
|
|
|
struct cmd_ctx ctx;
|
|
|
|
struct binding *bd;
|
|
|
|
u_int i;
|
|
|
|
|
|
|
|
bd = NULL;
|
|
|
|
for (i = 0; i < ARRAY_LENGTH(&key_bindings); i++) {
|
|
|
|
bd = ARRAY_ITEM(&key_bindings, i);
|
|
|
|
if (bd->key == key)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i == ARRAY_LENGTH(&key_bindings))
|
|
|
|
return;
|
|
|
|
|
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
|
|
|
ctx.flags = CMD_KEY;
|
|
|
|
|
|
|
|
cmd_exec(bd->cmd, &ctx);
|
|
|
|
}
|