2011-07-04 08:22:37 +00:00
|
|
|
/* $Id$ */
|
2007-10-03 21:31:07 +00: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 00:02:10 +00:00
|
|
|
#include <ctype.h>
|
2007-10-03 21:31:07 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2012-01-21 19:38:26 +00:00
|
|
|
RB_GENERATE(key_bindings, key_binding, entry, key_bindings_cmp);
|
2009-01-06 14:10:32 +00:00
|
|
|
|
|
|
|
struct key_bindings key_bindings;
|
|
|
|
|
|
|
|
int
|
|
|
|
key_bindings_cmp(struct key_binding *bd1, struct key_binding *bd2)
|
|
|
|
{
|
2009-07-25 08:52:04 +00: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 14:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct key_binding *
|
|
|
|
key_bindings_lookup(int key)
|
|
|
|
{
|
|
|
|
struct key_binding bd;
|
|
|
|
|
|
|
|
bd.key = key;
|
2012-01-21 19:38:26 +00:00
|
|
|
return (RB_FIND(key_bindings, &key_bindings, &bd));
|
2009-01-06 14:10:32 +00:00
|
|
|
}
|
2007-10-03 21:31:07 +00:00
|
|
|
|
|
|
|
void
|
2009-03-28 14:08:09 +00:00
|
|
|
key_bindings_add(int key, int can_repeat, struct cmd_list *cmdlist)
|
2007-10-03 21:31:07 +00:00
|
|
|
{
|
2009-01-06 14:10:32 +00:00
|
|
|
struct key_binding *bd;
|
2007-10-03 21:31:07 +00:00
|
|
|
|
2009-07-14 06:39:25 +00:00
|
|
|
key_bindings_remove(key);
|
2009-12-04 22:14:47 +00:00
|
|
|
|
2009-07-14 06:39:25 +00:00
|
|
|
bd = xmalloc(sizeof *bd);
|
|
|
|
bd->key = key;
|
2012-01-21 19:38:26 +00:00
|
|
|
RB_INSERT(key_bindings, &key_bindings, bd);
|
2009-12-04 22:14:47 +00:00
|
|
|
|
2009-03-28 14:08:09 +00:00
|
|
|
bd->can_repeat = can_repeat;
|
2009-01-18 14:40:48 +00:00
|
|
|
bd->cmdlist = cmdlist;
|
2007-10-03 21:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
key_bindings_remove(int key)
|
|
|
|
{
|
2009-01-06 14:10:32 +00:00
|
|
|
struct key_binding *bd;
|
2007-10-03 21:31:07 +00:00
|
|
|
|
2009-01-06 14:10:32 +00:00
|
|
|
if ((bd = key_bindings_lookup(key)) == NULL)
|
2007-10-03 21:31:07 +00:00
|
|
|
return;
|
2012-01-21 19:38:26 +00:00
|
|
|
RB_REMOVE(key_bindings, &key_bindings, bd);
|
2014-05-14 06:21:19 +00:00
|
|
|
cmd_list_free(bd->cmdlist);
|
|
|
|
free(bd);
|
2007-10-03 21:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
key_bindings_init(void)
|
|
|
|
{
|
2014-10-20 22:29:25 +00:00
|
|
|
static const char* defaults[] = {
|
|
|
|
"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 '%%'\"",
|
|
|
|
"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",
|
|
|
|
"bind : command-prompt",
|
|
|
|
"bind \\; last-pane",
|
|
|
|
"bind = choose-buffer",
|
|
|
|
"bind ? list-keys",
|
|
|
|
"bind D choose-client",
|
|
|
|
"bind L switch-client -l",
|
|
|
|
"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",
|
|
|
|
"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",
|
2007-10-03 21:31:07 +00:00
|
|
|
};
|
|
|
|
u_int i;
|
2009-01-18 14:40:48 +00:00
|
|
|
struct cmd_list *cmdlist;
|
2014-10-20 22:29:25 +00:00
|
|
|
char* cause;
|
|
|
|
int error;
|
|
|
|
struct cmd_q *cmdq;
|
2007-10-03 21:31:07 +00:00
|
|
|
|
2012-01-21 19:38:26 +00:00
|
|
|
RB_INIT(&key_bindings);
|
2007-10-04 00:18:59 +00:00
|
|
|
|
2014-10-22 23:18:53 +00:00
|
|
|
cmdq = cmdq_new(NULL);
|
2014-10-20 22:29:25 +00: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");
|
2014-10-22 23:18:53 +00:00
|
|
|
cmdq_run(cmdq, cmdlist);
|
|
|
|
cmd_list_free(cmdlist);
|
2007-10-03 21:31:07 +00:00
|
|
|
}
|
2014-10-22 23:18:53 +00:00
|
|
|
cmdq_free(cmdq);
|
2007-10-03 21:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-01-14 22:13:30 +00:00
|
|
|
key_bindings_dispatch(struct key_binding *bd, struct client *c)
|
2007-10-03 21:31:07 +00:00
|
|
|
{
|
2010-02-08 18:27:34 +00:00
|
|
|
struct cmd *cmd;
|
|
|
|
int readonly;
|
2007-10-03 21:31:07 +00:00
|
|
|
|
2010-02-08 18:27:34 +00:00
|
|
|
readonly = 1;
|
2010-07-02 02:43:01 +00:00
|
|
|
TAILQ_FOREACH(cmd, &bd->cmdlist->list, qentry) {
|
2010-02-08 18:27:34 +00:00
|
|
|
if (!(cmd->entry->flags & CMD_READONLY))
|
|
|
|
readonly = 0;
|
|
|
|
}
|
2013-02-23 22:25:58 +00:00
|
|
|
if (!readonly && (c->flags & CLIENT_READONLY)) {
|
2014-04-17 07:55:43 +00:00
|
|
|
cmdq_error(c->cmdq, "client is read-only");
|
2010-02-08 18:27:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-23 22:25:58 +00:00
|
|
|
cmdq_run(c->cmdq, bd->cmdlist);
|
2007-10-03 21:31:07 +00:00
|
|
|
}
|