diff options
author | Nicholas Marriott <nicm@cvs.openbsd.org> | 2009-07-24 14:52:48 +0000 |
---|---|---|
committer | Nicholas Marriott <nicm@cvs.openbsd.org> | 2009-07-24 14:52:48 +0000 |
commit | c42ba807b68fc85c850deda68fbc87eae89e1495 (patch) | |
tree | 8b9aeea1ddf34384b30c4062cea63b82b6a8bc10 /usr.bin/tmux | |
parent | ceecf3cf0e9a301bf6a6aa807d3bcc300cef003c (diff) |
Permit commands to be bound to key presses without the prefix key first. The
new -n flag to bind-key and unbind-key sets or removes these bindings, and
list-key shows them in []s.
Diffstat (limited to 'usr.bin/tmux')
-rw-r--r-- | usr.bin/tmux/cmd-bind-key.c | 13 | ||||
-rw-r--r-- | usr.bin/tmux/cmd-list-keys.c | 20 | ||||
-rw-r--r-- | usr.bin/tmux/cmd-unbind-key.c | 13 | ||||
-rw-r--r-- | usr.bin/tmux/key-bindings.c | 18 | ||||
-rw-r--r-- | usr.bin/tmux/server.c | 13 | ||||
-rw-r--r-- | usr.bin/tmux/tmux.1 | 21 | ||||
-rw-r--r-- | usr.bin/tmux/tmux.h | 16 |
7 files changed, 83 insertions, 31 deletions
diff --git a/usr.bin/tmux/cmd-bind-key.c b/usr.bin/tmux/cmd-bind-key.c index 9f1220b6003..c4775474dc4 100644 --- a/usr.bin/tmux/cmd-bind-key.c +++ b/usr.bin/tmux/cmd-bind-key.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-bind-key.c,v 1.2 2009/07/13 23:11:35 nicm Exp $ */ +/* $OpenBSD: cmd-bind-key.c,v 1.3 2009/07/24 14:52:47 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -39,7 +39,7 @@ struct cmd_bind_key_data { const struct cmd_entry cmd_bind_key_entry = { "bind-key", "bind", - "[-r] key command [arguments]", + "[-nr] key command [arguments]", 0, 0, NULL, cmd_bind_key_parse, @@ -54,14 +54,17 @@ int cmd_bind_key_parse(struct cmd *self, int argc, char **argv, char **cause) { struct cmd_bind_key_data *data; - int opt; + int opt, no_prefix = 0; self->data = data = xmalloc(sizeof *data); data->can_repeat = 0; data->cmdlist = NULL; - while ((opt = getopt(argc, argv, "r")) != -1) { + while ((opt = getopt(argc, argv, "nr")) != -1) { switch (opt) { + case 'n': + no_prefix = 1; + break; case 'r': data->can_repeat = 1; break; @@ -78,6 +81,8 @@ cmd_bind_key_parse(struct cmd *self, int argc, char **argv, char **cause) xasprintf(cause, "unknown key: %s", argv[0]); goto error; } + if (!no_prefix) + data->key |= KEYC_PREFIX; argc--; argv++; diff --git a/usr.bin/tmux/cmd-list-keys.c b/usr.bin/tmux/cmd-list-keys.c index dc4bc5d9d0b..5cfa7ae6818 100644 --- a/usr.bin/tmux/cmd-list-keys.c +++ b/usr.bin/tmux/cmd-list-keys.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-list-keys.c,v 1.3 2009/07/21 22:41:00 nicm Exp $ */ +/* $OpenBSD: cmd-list-keys.c,v 1.4 2009/07/24 14:52:47 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -46,27 +46,33 @@ cmd_list_keys_exec(unused struct cmd *self, struct cmd_ctx *ctx) { struct key_binding *bd; const char *key; - char tmp[BUFSIZ]; - int width, keywidth; + char tmp[BUFSIZ], keytmp[64]; + int width, keywidth; width = 0; SPLAY_FOREACH(bd, key_bindings, &key_bindings) { - if ((key = key_string_lookup_key(bd->key)) == NULL) + key = key_string_lookup_key(bd->key & ~KEYC_PREFIX); + if (key == NULL) continue; keywidth = strlen(key) + 1; + if (!(bd->key & KEYC_PREFIX)) + keywidth += 2; if (keywidth > width) width = keywidth; } - SPLAY_FOREACH(bd, key_bindings, &key_bindings) { - if ((key = key_string_lookup_key(bd->key)) == NULL) + key = key_string_lookup_key(bd->key & ~KEYC_PREFIX); + if (key == NULL) continue; *tmp = '\0'; cmd_list_print(bd->cmdlist, tmp, sizeof tmp); - + if (!(bd->key & KEYC_PREFIX)) { + xsnprintf(keytmp, sizeof keytmp, "[%s]", key); + key = keytmp; + } ctx->print(ctx, "%*s: %s", width, key, tmp); } diff --git a/usr.bin/tmux/cmd-unbind-key.c b/usr.bin/tmux/cmd-unbind-key.c index 94c0e8d2585..5415c601833 100644 --- a/usr.bin/tmux/cmd-unbind-key.c +++ b/usr.bin/tmux/cmd-unbind-key.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-unbind-key.c,v 1.2 2009/07/13 23:11:35 nicm Exp $ */ +/* $OpenBSD: cmd-unbind-key.c,v 1.3 2009/07/24 14:52:47 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -36,7 +36,7 @@ struct cmd_unbind_key_data { const struct cmd_entry cmd_unbind_key_entry = { "unbind-key", "unbind", - "key", + "[-n] key", 0, 0, NULL, cmd_unbind_key_parse, @@ -51,12 +51,15 @@ int cmd_unbind_key_parse(struct cmd *self, int argc, char **argv, char **cause) { struct cmd_unbind_key_data *data; - int opt; + int opt, no_prefix = 0; self->data = data = xmalloc(sizeof *data); - while ((opt = getopt(argc, argv, "")) != -1) { + while ((opt = getopt(argc, argv, "n")) != -1) { switch (opt) { + case 'n': + no_prefix = 1; + break; default: goto usage; } @@ -70,6 +73,8 @@ cmd_unbind_key_parse(struct cmd *self, int argc, char **argv, char **cause) xasprintf(cause, "unknown key: %s", argv[0]); goto error; } + if (!no_prefix) + data->key |= KEYC_PREFIX; return (0); diff --git a/usr.bin/tmux/key-bindings.c b/usr.bin/tmux/key-bindings.c index ed15338c715..5a07b0ace2d 100644 --- a/usr.bin/tmux/key-bindings.c +++ b/usr.bin/tmux/key-bindings.c @@ -1,4 +1,4 @@ -/* $OpenBSD: key-bindings.c,v 1.7 2009/07/21 17:57:29 nicm Exp $ */ +/* $OpenBSD: key-bindings.c,v 1.8 2009/07/24 14:52:47 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -32,7 +32,18 @@ struct key_bindings dead_key_bindings; int key_bindings_cmp(struct key_binding *bd1, struct key_binding *bd2) { - return (bd1->key - bd2->key); + 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); } struct key_binding * @@ -170,7 +181,8 @@ key_bindings_init(void) cmd->entry->init(cmd, table[i].key); TAILQ_INSERT_HEAD(cmdlist, cmd, qentry); - key_bindings_add(table[i].key, table[i].can_repeat, cmdlist); + key_bindings_add( + table[i].key | KEYC_PREFIX, table[i].can_repeat, cmdlist); } } diff --git a/usr.bin/tmux/server.c b/usr.bin/tmux/server.c index 8b0b8a1ed3c..90981597fd7 100644 --- a/usr.bin/tmux/server.c +++ b/usr.bin/tmux/server.c @@ -1,4 +1,4 @@ -/* $OpenBSD: server.c,v 1.13 2009/07/21 19:54:22 nicm Exp $ */ +/* $OpenBSD: server.c,v 1.14 2009/07/24 14:52:47 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -802,14 +802,19 @@ server_handle_client(struct client *c) if (!(c->flags & CLIENT_PREFIX)) { if (key == prefix) c->flags |= CLIENT_PREFIX; - else - window_pane_key(wp, c, key); + else { + /* Try as a non-prefix key binding. */ + if ((bd = key_bindings_lookup(key)) == NULL) + window_pane_key(wp, c, key); + else + key_bindings_dispatch(bd, c); + } continue; } /* Prefix key already pressed. Reset prefix and lookup key. */ c->flags &= ~CLIENT_PREFIX; - if ((bd = key_bindings_lookup(key)) == NULL) { + if ((bd = key_bindings_lookup(key | KEYC_PREFIX)) == NULL) { /* If repeating, treat this as a key, else ignore. */ if (c->flags & CLIENT_REPEAT) { c->flags &= ~CLIENT_REPEAT; diff --git a/usr.bin/tmux/tmux.1 b/usr.bin/tmux/tmux.1 index af27f6d9145..56be4aadc3c 100644 --- a/usr.bin/tmux/tmux.1 +++ b/usr.bin/tmux/tmux.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: tmux.1,v 1.44 2009/07/24 14:50:24 sthen Exp $ +.\" $OpenBSD: tmux.1,v 1.45 2009/07/24 14:52:47 nicm Exp $ .\" .\" Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> .\" @@ -617,7 +617,7 @@ If no server is started, will attempt to start it; this will fail unless sessions are created in the configuration file. .It Xo Ic bind-key -.Op Fl r +.Op Fl nr .Ar key Ar command Op Ar arguments .Xc .D1 (alias: Ic bind ) @@ -632,6 +632,13 @@ or for Ctrl keys, or .Ql M- for Alt (meta) keys. +If +.Fl n +is specified, it is not necessary to use the prefix key, +.Ar command +is bound to +.Ar key +alone. The .Fl r flag indicates this key may repeat, see the @@ -842,6 +849,10 @@ List the syntax of all commands supported by .Xc .D1 (alias: Ic lsk ) List all key bindings. +Keys bound without the prefix key (see +.Ic bind-key +.Fl n ) +are enclosed in square brackets. .It Xo Ic list-sessions .Xc .D1 (alias: Ic ls ) @@ -1613,11 +1624,17 @@ Switch the current session for client to .Ar target-session . .It Xo Ic unbind-key +.Op Fl n .Ar key .Xc .D1 (alias: Ic unbind ) Unbind the command bound to .Ar key . +If +.Fl n +is specified, the command bound to +.Ar key +without a prefix (if any) is removed. .It Xo Ic unlink-window .Op Fl k .Op Fl t Ar target-window diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h index 2d9e7ec4f10..6dc712b1546 100644 --- a/usr.bin/tmux/tmux.h +++ b/usr.bin/tmux/tmux.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tmux.h,v 1.53 2009/07/23 20:24:27 nicm Exp $ */ +/* $OpenBSD: tmux.h,v 1.54 2009/07/24 14:52:47 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -102,10 +102,12 @@ struct buffer { #define BELL_CURRENT 2 /* Key codes. ncurses defines KEY_*. Grrr. */ -#define KEYC_NONE 0x0fff -#define KEYC_ESCAPE 0x2000 -#define KEYC_CTRL 0x4000 -#define KEYC_SHIFT 0x8000 +#define KEYC_NONE 0xfff +/* 0x1000 is base for special keys */ +#define KEYC_ESCAPE 0x2000 +#define KEYC_CTRL 0x4000 +#define KEYC_SHIFT 0x8000 +#define KEYC_PREFIX 0x10000 enum key_code { /* Mouse key. */ @@ -856,8 +858,6 @@ struct client_ctx { /* Key/command line command. */ struct cmd_ctx { - struct client *cmdclient; - /* * curclient is the client where this command was executed if inside * tmux. This is NULL if the command came from the command-line. @@ -869,6 +869,8 @@ struct cmd_ctx { * configuration file. */ struct client *curclient; + struct client *cmdclient; + struct session *cursession; struct msg_command_data *msgdata; |