2009-06-02 00:58:49 +02:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2012-01-21 12:12:13 +01:00
|
|
|
RB_GENERATE(key_bindings, key_binding, entry, key_bindings_cmp);
|
2015-04-20 17:34:56 +02:00
|
|
|
RB_GENERATE(key_tables, key_table, entry, key_table_cmp);
|
|
|
|
struct key_tables key_tables = RB_INITIALIZER(&key_tables);
|
2009-06-02 00:58:49 +02:00
|
|
|
|
2015-04-20 17:34:56 +02:00
|
|
|
int
|
|
|
|
key_table_cmp(struct key_table *e1, struct key_table *e2)
|
|
|
|
{
|
|
|
|
return (strcmp(e1->name, e2->name));
|
|
|
|
}
|
2009-06-02 00:58:49 +02:00
|
|
|
|
|
|
|
int
|
|
|
|
key_bindings_cmp(struct key_binding *bd1, struct key_binding *bd2)
|
|
|
|
{
|
2015-04-20 17:34:56 +02:00
|
|
|
return (bd1->key - bd2->key);
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|
|
|
|
|
2015-04-20 17:34:56 +02:00
|
|
|
struct key_table *
|
|
|
|
key_bindings_get_table(const char *name, int create)
|
2009-06-02 00:58:49 +02:00
|
|
|
{
|
2015-04-20 17:34:56 +02:00
|
|
|
struct key_table table_find, *table;
|
|
|
|
|
|
|
|
table_find.name = name;
|
|
|
|
table = RB_FIND(key_tables, &key_tables, &table_find);
|
|
|
|
if (table != NULL || !create)
|
|
|
|
return (table);
|
2009-06-02 00:58:49 +02:00
|
|
|
|
2015-04-20 17:34:56 +02:00
|
|
|
table = xmalloc(sizeof *table);
|
|
|
|
table->name = xstrdup(name);
|
|
|
|
RB_INIT(&table->key_bindings);
|
|
|
|
|
|
|
|
table->references = 1; /* one reference in key_tables */
|
|
|
|
RB_INSERT(key_tables, &key_tables, table);
|
|
|
|
|
|
|
|
return (table);
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-20 17:34:56 +02:00
|
|
|
key_bindings_unref_table(struct key_table *table)
|
2009-06-02 00:58:49 +02:00
|
|
|
{
|
|
|
|
struct key_binding *bd;
|
|
|
|
|
2015-04-20 17:34:56 +02:00
|
|
|
if (--table->references != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (!RB_EMPTY(&table->key_bindings)) {
|
|
|
|
bd = RB_ROOT(&table->key_bindings);
|
|
|
|
RB_REMOVE(key_bindings, &table->key_bindings, bd);
|
|
|
|
cmd_list_free(bd->cmdlist);
|
|
|
|
free(bd);
|
|
|
|
}
|
|
|
|
|
|
|
|
free((void *)table->name);
|
|
|
|
free(table);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
key_bindings_add(const char *name, int key, int can_repeat,
|
|
|
|
struct cmd_list *cmdlist)
|
|
|
|
{
|
|
|
|
struct key_table *table;
|
|
|
|
struct key_binding bd_find, *bd;
|
|
|
|
|
|
|
|
table = key_bindings_get_table(name, 1);
|
|
|
|
|
|
|
|
bd_find.key = key;
|
|
|
|
bd = RB_FIND(key_bindings, &table->key_bindings, &bd_find);
|
|
|
|
if (bd != NULL) {
|
|
|
|
RB_REMOVE(key_bindings, &table->key_bindings, bd);
|
|
|
|
cmd_list_free(bd->cmdlist);
|
|
|
|
free(bd);
|
|
|
|
}
|
2009-12-03 23:50:09 +01:00
|
|
|
|
2009-07-12 19:33:18 +02:00
|
|
|
bd = xmalloc(sizeof *bd);
|
|
|
|
bd->key = key;
|
2015-04-20 17:34:56 +02:00
|
|
|
RB_INSERT(key_bindings, &table->key_bindings, bd);
|
2009-12-03 23:50:09 +01:00
|
|
|
|
2009-06-02 00:58:49 +02:00
|
|
|
bd->can_repeat = can_repeat;
|
|
|
|
bd->cmdlist = cmdlist;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-20 17:34:56 +02:00
|
|
|
key_bindings_remove(const char *name, int key)
|
2009-06-02 00:58:49 +02:00
|
|
|
{
|
2015-04-20 17:34:56 +02:00
|
|
|
struct key_table *table;
|
|
|
|
struct key_binding bd_find, *bd;
|
|
|
|
|
|
|
|
table = key_bindings_get_table(name, 0);
|
|
|
|
if (table == NULL)
|
|
|
|
return;
|
2009-06-02 00:58:49 +02:00
|
|
|
|
2015-04-20 17:34:56 +02:00
|
|
|
bd_find.key = key;
|
|
|
|
bd = RB_FIND(key_bindings, &table->key_bindings, &bd_find);
|
|
|
|
if (bd == NULL)
|
2009-06-02 00:58:49 +02:00
|
|
|
return;
|
2015-04-20 17:34:56 +02:00
|
|
|
|
|
|
|
RB_REMOVE(key_bindings, &table->key_bindings, bd);
|
2014-05-14 08:21:19 +02:00
|
|
|
cmd_list_free(bd->cmdlist);
|
|
|
|
free(bd);
|
2015-04-20 17:34:56 +02:00
|
|
|
|
|
|
|
if (RB_EMPTY(&table->key_bindings)) {
|
|
|
|
RB_REMOVE(key_tables, &key_tables, table);
|
|
|
|
key_bindings_unref_table(table);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
key_bindings_remove_table(const char *name)
|
|
|
|
{
|
|
|
|
struct key_table *table;
|
|
|
|
|
|
|
|
table = key_bindings_get_table(name, 0);
|
|
|
|
if (table != NULL) {
|
|
|
|
RB_REMOVE(key_tables, &key_tables, table);
|
|
|
|
key_bindings_unref_table(table);
|
|
|
|
}
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
key_bindings_init(void)
|
|
|
|
{
|
2015-04-25 20:49:01 +02:00
|
|
|
static const char *defaults[] = {
|
2014-10-21 00:29:25 +02:00
|
|
|
"bind C-b send-prefix",
|
|
|
|
"bind C-o rotate-window",
|
|
|
|
"bind C-z suspend-client",
|
|
|
|
"bind Space next-layout",
|
|
|
|
"bind ! break-pane",
|
|
|
|
"bind '\"' split-window",
|
|
|
|
"bind '#' list-buffers",
|
|
|
|
"bind '$' command-prompt -I'#S' \"rename-session '%%'\"",
|
|
|
|
"bind % split-window -h",
|
|
|
|
"bind & confirm-before -p\"kill-window #W? (y/n)\" kill-window",
|
|
|
|
"bind \"'\" command-prompt -pindex \"select-window -t ':%%'\"",
|
|
|
|
"bind ( switch-client -p",
|
|
|
|
"bind ) switch-client -n",
|
|
|
|
"bind , command-prompt -I'#W' \"rename-window '%%'\"",
|
|
|
|
"bind - delete-buffer",
|
|
|
|
"bind . command-prompt \"move-window -t '%%'\"",
|
2015-06-05 10:14:16 +02:00
|
|
|
"bind 0 select-window -t:=0",
|
|
|
|
"bind 1 select-window -t:=1",
|
|
|
|
"bind 2 select-window -t:=2",
|
|
|
|
"bind 3 select-window -t:=3",
|
|
|
|
"bind 4 select-window -t:=4",
|
|
|
|
"bind 5 select-window -t:=5",
|
|
|
|
"bind 6 select-window -t:=6",
|
|
|
|
"bind 7 select-window -t:=7",
|
|
|
|
"bind 8 select-window -t:=8",
|
|
|
|
"bind 9 select-window -t:=9",
|
2014-10-21 00:29:25 +02:00
|
|
|
"bind : command-prompt",
|
|
|
|
"bind \\; last-pane",
|
|
|
|
"bind = choose-buffer",
|
|
|
|
"bind ? list-keys",
|
|
|
|
"bind D choose-client",
|
|
|
|
"bind L switch-client -l",
|
2015-06-04 13:43:51 +02:00
|
|
|
"bind M select-pane -M",
|
2014-10-21 00:29:25 +02:00
|
|
|
"bind [ copy-mode",
|
|
|
|
"bind ] paste-buffer",
|
|
|
|
"bind c new-window",
|
|
|
|
"bind d detach-client",
|
|
|
|
"bind f command-prompt \"find-window '%%'\"",
|
|
|
|
"bind i display-message",
|
|
|
|
"bind l last-window",
|
2015-06-04 13:43:51 +02:00
|
|
|
"bind m select-pane -m",
|
2014-10-21 00:29:25 +02:00
|
|
|
"bind n next-window",
|
|
|
|
"bind o select-pane -t:.+",
|
|
|
|
"bind p previous-window",
|
|
|
|
"bind q display-panes",
|
|
|
|
"bind r refresh-client",
|
|
|
|
"bind s choose-tree",
|
|
|
|
"bind t clock-mode",
|
|
|
|
"bind w choose-window",
|
|
|
|
"bind x confirm-before -p\"kill-pane #P? (y/n)\" kill-pane",
|
|
|
|
"bind z resize-pane -Z",
|
|
|
|
"bind { swap-pane -U",
|
|
|
|
"bind } swap-pane -D",
|
|
|
|
"bind '~' show-messages",
|
|
|
|
"bind PPage copy-mode -u",
|
|
|
|
"bind -r Up select-pane -U",
|
|
|
|
"bind -r Down select-pane -D",
|
|
|
|
"bind -r Left select-pane -L",
|
|
|
|
"bind -r Right select-pane -R",
|
|
|
|
"bind M-1 select-layout even-horizontal",
|
|
|
|
"bind M-2 select-layout even-vertical",
|
|
|
|
"bind M-3 select-layout main-horizontal",
|
|
|
|
"bind M-4 select-layout main-vertical",
|
|
|
|
"bind M-5 select-layout tiled",
|
|
|
|
"bind M-n next-window -a",
|
|
|
|
"bind M-o rotate-window -D",
|
|
|
|
"bind M-p previous-window -a",
|
|
|
|
"bind -r M-Up resize-pane -U 5",
|
|
|
|
"bind -r M-Down resize-pane -D 5",
|
|
|
|
"bind -r M-Left resize-pane -L 5",
|
|
|
|
"bind -r M-Right resize-pane -R 5",
|
|
|
|
"bind -r C-Up resize-pane -U",
|
|
|
|
"bind -r C-Down resize-pane -D",
|
|
|
|
"bind -r C-Left resize-pane -L",
|
|
|
|
"bind -r C-Right resize-pane -R",
|
2015-04-19 23:34:21 +02:00
|
|
|
"bind -n MouseDown1Pane select-pane -t=\\; send-keys -M",
|
|
|
|
"bind -n MouseDrag1Border resize-pane -M",
|
|
|
|
"bind -n MouseDown1Status select-window -t=",
|
2015-10-27 00:06:18 +01:00
|
|
|
"bind -n WheelDownStatus next-window",
|
|
|
|
"bind -n WheelUpStatus previous-window",
|
2015-04-21 17:34:32 +02:00
|
|
|
"bind -n MouseDrag1Pane if -Ft= '#{mouse_any_flag}' 'if -Ft= \"#{pane_in_mode}\" \"copy-mode -M\" \"send-keys -M\"' 'copy-mode -M'",
|
2015-11-05 12:05:30 +01:00
|
|
|
"bind -n MouseDown3Pane if-shell -Ft= '#{mouse_any_flag}' 'select-pane -t=; send-keys -M' 'select-pane -mt='",
|
2015-10-27 10:15:21 +01:00
|
|
|
"bind -n WheelUpPane if-shell -Ft= '#{mouse_any_flag}' 'send-keys -M' 'if -Ft= \"#{pane_in_mode}\" \"send-keys -M\" \"copy-mode -et=\"'",
|
2009-06-02 00:58:49 +02:00
|
|
|
};
|
|
|
|
u_int i;
|
|
|
|
struct cmd_list *cmdlist;
|
2015-04-25 20:49:01 +02:00
|
|
|
char *cause;
|
2014-10-21 00:29:25 +02:00
|
|
|
int error;
|
|
|
|
struct cmd_q *cmdq;
|
2009-06-02 00:58:49 +02:00
|
|
|
|
2014-10-23 01:18:53 +02:00
|
|
|
cmdq = cmdq_new(NULL);
|
2014-10-21 00:29:25 +02:00
|
|
|
for (i = 0; i < nitems(defaults); i++) {
|
|
|
|
error = cmd_string_parse(defaults[i], &cmdlist,
|
|
|
|
"<default-keys>", i, &cause);
|
|
|
|
if (error != 0)
|
|
|
|
fatalx("bad default key");
|
2015-04-19 23:34:21 +02:00
|
|
|
cmdq_run(cmdq, cmdlist, NULL);
|
2015-05-07 13:42:56 +02:00
|
|
|
cmd_list_free(cmdlist);
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|
2014-10-23 01:18:53 +02:00
|
|
|
cmdq_free(cmdq);
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-19 23:34:21 +02:00
|
|
|
key_bindings_dispatch(struct key_binding *bd, struct client *c,
|
|
|
|
struct mouse_event *m)
|
2009-06-02 00:58:49 +02:00
|
|
|
{
|
2010-02-06 23:55:31 +01:00
|
|
|
struct cmd *cmd;
|
|
|
|
int readonly;
|
2009-06-02 00:58:49 +02:00
|
|
|
|
2010-02-06 23:55:31 +01:00
|
|
|
readonly = 1;
|
2010-06-26 20:20:53 +02:00
|
|
|
TAILQ_FOREACH(cmd, &bd->cmdlist->list, qentry) {
|
2010-02-06 23:55:31 +01:00
|
|
|
if (!(cmd->entry->flags & CMD_READONLY))
|
|
|
|
readonly = 0;
|
|
|
|
}
|
2013-03-24 10:54:10 +01:00
|
|
|
if (!readonly && (c->flags & CLIENT_READONLY)) {
|
2014-04-17 09:55:43 +02:00
|
|
|
cmdq_error(c->cmdq, "client is read-only");
|
2010-02-06 23:55:31 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-19 23:34:21 +02:00
|
|
|
cmdq_run(c->cmdq, bd->cmdlist, m);
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|