From 7836298f296f8f41407dc4a6dbdb4aac1e3d4b96 Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Wed, 25 Jun 2008 20:33:20 +0000 Subject: [PATCH] Allow use of alt keys. move-window command. --- CHANGES | 5 ++- GNUmakefile | 4 +-- Makefile | 4 +-- TODO | 2 +- cmd-link-window.c | 43 +++++++++++------------- cmd.c | 3 +- key-string.c | 86 +++++++++++++++++++++++++++-------------------- tmux.1 | 37 +++++++++++--------- tmux.h | 3 +- 9 files changed, 104 insertions(+), 83 deletions(-) diff --git a/CHANGES b/CHANGES index bced973a..d05b0b48 100644 --- a/CHANGES +++ b/CHANGES @@ -1,5 +1,8 @@ 25 June 2008 +* move-window command. +* Support binding alt keys (prefixed with M-). Change default to use + C- for ctrl keys (^ is still accepted as an alternative). * Slim down default key bindings: support lowercase only. * Handle escaped keys properly (parse eg \033b into a single key code) and use this to change copy mode next/previous work to M-f and M-b to match @@ -567,4 +570,4 @@ (including mutt, emacs). No status bar yet and no key remapping or other customisation. -$Id: CHANGES,v 1.142 2008-06-25 19:18:20 nicm Exp $ +$Id: CHANGES,v 1.143 2008-06-25 20:33:20 nicm Exp $ diff --git a/GNUmakefile b/GNUmakefile index cfebe459..12c96c9c 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -1,4 +1,4 @@ -# $Id: GNUmakefile,v 1.33 2008-06-23 22:24:16 nicm Exp $ +# $Id: GNUmakefile,v 1.34 2008-06-25 20:33:20 nicm Exp $ .PHONY: clean @@ -28,7 +28,7 @@ SRCS= tmux.c server.c server-msg.c server-fn.c buffer.c buffer-poll.c status.c \ cmd-kill-server.c cmd-set-window-option.c cmd-show-options.c \ cmd-show-window-options.c cmd-command-prompt.c cmd-set-buffer.c \ cmd-show-buffer.c cmd-list-buffers.c cmd-delete-buffer.c \ - cmd-list-commands.c \ + cmd-list-commands.c cmd-move-window.c \ window-scroll.c window-more.c window-copy.c options.c paste.c \ tty.c tty-keys.c tty-write.c screen-write.c screen-redraw.c diff --git a/Makefile b/Makefile index bef56996..dff27fb2 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -# $Id: Makefile,v 1.66 2008-06-23 22:24:16 nicm Exp $ +# $Id: Makefile,v 1.67 2008-06-25 20:33:20 nicm Exp $ .SUFFIXES: .c .o .y .h .PHONY: clean update-index.html upload-index.html @@ -32,7 +32,7 @@ SRCS= tmux.c server.c server-msg.c server-fn.c buffer.c buffer-poll.c status.c \ cmd-kill-server.c cmd-set-window-option.c cmd-show-options.c \ cmd-show-window-options.c cmd-command-prompt.c cmd-set-buffer.c \ cmd-show-buffer.c cmd-list-buffers.c cmd-delete-buffer.c \ - cmd-list-commands.c \ + cmd-list-commands.c cmd-move-window.c \ window-scroll.c window-more.c window-copy.c options.c paste.c \ tty.c tty-keys.c tty-write.c screen-write.c screen-redraw.c diff --git a/TODO b/TODO index e94c1a57..d642f2e7 100644 --- a/TODO +++ b/TODO @@ -49,7 +49,7 @@ -- For 0.4 -------------------------------------------------------------------- -- document list-commands. document switch-client +- document list-commands. document switch-client. document move-window - document buffer stuff - document next/prev word - commands: diff --git a/cmd-link-window.c b/cmd-link-window.c index 3a3c3e5e..8a9bb6df 100644 --- a/cmd-link-window.c +++ b/cmd-link-window.c @@ -1,4 +1,4 @@ -/* $Id: cmd-link-window.c,v 1.23 2008-06-18 22:21:51 nicm Exp $ */ +/* $Id: cmd-link-window.c,v 1.24 2008-06-25 20:33:20 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -46,29 +46,29 @@ void cmd_link_window_exec(struct cmd *self, struct cmd_ctx *ctx) { struct cmd_srcdst_data *data = self->data; - struct session *s; + struct session *dst; struct winlink *wl_src, *wl_dst; int idx; if ((wl_src = cmd_find_window(ctx, data->src, NULL)) == NULL) return; - if (arg_parse_window(data->dst, &s, &idx) != 0) { + if (arg_parse_window(data->dst, &dst, &idx) != 0) { ctx->error(ctx, "bad window: %s", data->dst); return; } - if (s == NULL) - s = ctx->cursession; - if (s == NULL) - s = cmd_current_session(ctx); - if (s == NULL) { + if (dst == NULL) + dst = ctx->cursession; + if (dst == NULL) + dst = cmd_current_session(ctx); + if (dst == NULL) { ctx->error(ctx, "session not found: %s", data->dst); return; } wl_dst = NULL; if (idx != -1) - wl_dst = winlink_find_by_index(&s->windows, idx); + wl_dst = winlink_find_by_index(&dst->windows, idx); if (wl_dst != NULL) { if (wl_dst->window == wl_src->window) goto out; @@ -78,35 +78,30 @@ cmd_link_window_exec(struct cmd *self, struct cmd_ctx *ctx) * Can't use session_detach as it will destroy session * if this makes it empty. */ - session_alert_cancel(s, wl_dst); - winlink_remove(&s->windows, wl_dst); + session_alert_cancel(dst, wl_dst); + winlink_remove(&dst->windows, wl_dst); /* Force select/redraw if current. */ - if (wl_dst == s->curw) { + if (wl_dst == dst->curw) { data->flags &= ~CMD_DFLAG; - s->curw = NULL; + dst->curw = NULL; } - if (wl_dst == s->lastw) - s->lastw = NULL; - - /* - * Can't error out after this or there could be an - * empty session! - */ + if (wl_dst == dst->lastw) + dst->lastw = NULL; } } - wl_dst = session_attach(s, wl_src->window, idx); + wl_dst = session_attach(dst, wl_src->window, idx); if (wl_dst == NULL) { ctx->error(ctx, "index in use: %d", idx); return; } if (data->flags & CMD_DFLAG) - server_status_session(s); + server_status_session(dst); else { - session_select(s, wl_dst->idx); - server_redraw_session(s); + session_select(dst, wl_dst->idx); + server_redraw_session(dst); } recalculate_sizes(); diff --git a/cmd.c b/cmd.c index 88fcf6ee..84947be3 100644 --- a/cmd.c +++ b/cmd.c @@ -1,4 +1,4 @@ -/* $Id: cmd.c,v 1.56 2008-06-23 22:24:16 nicm Exp $ */ +/* $Id: cmd.c,v 1.57 2008-06-25 20:33:20 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -45,6 +45,7 @@ const struct cmd_entry *cmd_table[] = { &cmd_list_keys_entry, &cmd_list_sessions_entry, &cmd_list_windows_entry, + &cmd_move_window_entry, &cmd_new_session_entry, &cmd_new_window_entry, &cmd_next_window_entry, diff --git a/key-string.c b/key-string.c index e2d03a2f..605c982b 100644 --- a/key-string.c +++ b/key-string.c @@ -1,4 +1,4 @@ -/* $Id: key-string.c,v 1.5 2008-06-04 20:01:36 nicm Exp $ */ +/* $Id: key-string.c,v 1.6 2008-06-25 20:33:20 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -173,39 +173,7 @@ struct { { "SUNDO", KEYC_SUNDO }, { "SUSPEND", KEYC_SUSPEND }, { "UNDO", KEYC_UNDO }, - { "UP", KEYC_UP }, - { "^ ", 0 }, - { "^A", 1 }, - { "^B", 2 }, - { "^C", 3 }, - { "^D", 4 }, - { "^E", 5 }, - { "^F", 6 }, - { "^G", 7 }, - { "^H", 8 }, - { "^I", 9 }, - { "^J", 10 }, - { "^K", 11 }, - { "^L", 12 }, - { "^M", 13 }, - { "^N", 14 }, - { "^O", 15 }, - { "^P", 16 }, - { "^Q", 17 }, - { "^R", 18 }, - { "^S", 19 }, - { "^T", 20 }, - { "^U", 21 }, - { "^V", 22 }, - { "^W", 23 }, - { "^X", 24 }, - { "^Y", 25 }, - { "^Z", 26 }, - { "^[", 27 }, - { "^\\", 28 }, - { "^]", 29 }, - { "^^", 30 }, - { "^_", 31 } + { "UP", KEYC_UP } }; #define NKEYSTRINGS (sizeof key_string_table / sizeof key_string_table[0]) @@ -213,12 +181,39 @@ int key_string_lookup_string(const char *string) { u_int i; + int key; if (string[0] == '\0') return (KEYC_NONE); if (string[1] == '\0') return (string[0]); + if (string[0] == 'C' && string[1] == '-') { + if (string[2] == '\0' || string[3] != '\0') + return (KEYC_NONE); + if (string[2] >= 64 && string[2] <= 95) + return (string[2] - 64); + if (string[2] >= 97 && string[2] <= 122) + return (string[2] - 96); + return (KEYC_NONE); + } + + if (string[0] == '^') { + if (string[1] == '\0' || string[2] != '\0') + return (KEYC_NONE); + if (string[1] >= 64 && string[1] <= 95) + return (string[1] - 64); + if (string[1] >= 97 && string[1] <= 122) + return (string[1] - 96); + return (KEYC_NONE); + } + + if (string[0] == 'M' && string[1] == '-') { + if ((key = key_string_lookup_string(string + 2)) == KEYC_NONE) + return (KEYC_NONE); + return (KEYC_ADDESCAPE(key)); + } + for (i = 0; i < NKEYSTRINGS; i++) { if (strcasecmp(string, key_string_table[i].string) == 0) return (key_string_table[i].key); @@ -229,15 +224,34 @@ key_string_lookup_string(const char *string) const char * key_string_lookup_key(int key) { - static char tmp[2]; + static char tmp[24], tmp2[24]; + const char *s; u_int i; - if (key > 31 && key < 256) { + if (key == 127) + return (NULL); + + if (KEYC_ISESCAPE(key)) { + if ((s = key_string_lookup_key(KEYC_REMOVEESCAPE(key))) == NULL) + return (NULL); + xsnprintf(tmp2, sizeof tmp2, "M-%s", s); + return (tmp2); + } + + if (key >= 32 && key <= 255) { tmp[0] = key; tmp[1] = '\0'; return (tmp); } + if (key >= 0 && key <= 32) { + if (key == 0 || key > 26) + xsnprintf(tmp, sizeof tmp, "C-%c", 64 + key); + else + xsnprintf(tmp, sizeof tmp, "C-%c", 96 + key); + return (tmp); + } + for (i = 0; i < NKEYSTRINGS; i++) { if (key == key_string_table[i].key) return (key_string_table[i].string); diff --git a/tmux.1 b/tmux.1 index 1a0967b8..5c156e2b 100644 --- a/tmux.1 +++ b/tmux.1 @@ -1,4 +1,4 @@ -.\" $Id: tmux.1,v 1.42 2008-06-19 22:14:12 nicm Exp $ +.\" $Id: tmux.1,v 1.43 2008-06-25 20:33:20 nicm Exp $ .\" .\" Copyright (c) 2007 Nicholas Marriott .\" @@ -112,30 +112,30 @@ $ .Ed .Pp Within an active session, a new window may be created by typing -.Ql ^B -(ctrl-B, known as the prefix key) +.Ql C-b +(ctrl-b, known as the prefix key) followed by the .Ql c key. .Pp Windows may be navigated with: -.Ql ^B 0 +.Ql C-b 0 (to select window 0), -.Ql ^B 1 +.Ql C-b 1 (to select window 1), and so on; -.Ql ^B n +.Ql C-b n to select the next window; and -.Ql ^B p +.Ql C-b p to select the previous window. .Pp A session may be detached using -.Ql ^B d +.Ql C-b d and reattached with: .Pp .Dl $ tmux attach-session .Pp Typing -.Ql ^B \&? +.Ql C-b \&? lists the current key bindings in the current window; up and down may be used to navigate the list or .Ql Q @@ -144,8 +144,8 @@ to exit from it. .Nm may be controlled from an attached client by using a key combination of a prefix key, -.Ql ^B -(ctrl-B) by default, followed by a command key. +.Ql C-b +(ctrl-b) by default, followed by a command key. .Pp Some of the default key bindings include: .Pp @@ -227,9 +227,9 @@ This permits a section of a window or its history to be copied to the for later insertion into another window. The navigation keys move the cursor around the window, scrolling as necessary. In addition, -.Ql ^A +.Ql C-a and -.Ql ^E +.Ql C-e (or the .Xr vi 1 style @@ -238,7 +238,7 @@ and .Ql $ keys) move to the start and end of the line; the space key begins a selection; and the enter key or -.Ql ^W +.Ql C-w copies the selection to the paste buffer and exits copy mode. .Pp This mode is entered with the @@ -333,6 +333,13 @@ Bind key .Ar key to .Ar command . +Keys may be specified prefixed with +.Ql C- +or +.Ql ^ +for ctrl keys, or +.Ql M- +for alt (meta) keys. .It Xo Ic command-prompt .Op Fl t Ar target-client .Xc @@ -525,7 +532,7 @@ Send a key or keys to a window. Each argument .Ar key is the name of the key (such as -.Ql ^A +.Ql C-a or .Ql npage ) to send; if the string is not recognised as a key, it is sent as a series of diff --git a/tmux.h b/tmux.h index 541941d8..80342d11 100644 --- a/tmux.h +++ b/tmux.h @@ -1,4 +1,4 @@ -/* $Id: tmux.h,v 1.168 2008-06-25 07:30:08 nicm Exp $ */ +/* $Id: tmux.h,v 1.169 2008-06-25 20:33:20 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -958,6 +958,7 @@ extern const struct cmd_entry cmd_list_commands_entry; extern const struct cmd_entry cmd_list_keys_entry; extern const struct cmd_entry cmd_list_sessions_entry; extern const struct cmd_entry cmd_list_windows_entry; +extern const struct cmd_entry cmd_move_window_entry; extern const struct cmd_entry cmd_new_session_entry; extern const struct cmd_entry cmd_new_window_entry; extern const struct cmd_entry cmd_next_window_entry;