From 100190214314cbcbdcf5cf4bb462e218a460b88e Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Sat, 16 May 2009 11:48:47 +0000 Subject: [PATCH] select-layout command and some key bindings. --- CHANGES | 4 ++- TODO | 3 +- cmd-select-layout.c | 86 +++++++++++++++++++++++++++++++++++++++++++++ cmd.c | 3 +- key-bindings.c | 7 +++- layout.c | 37 ++++++++++++++++++- tmux.h | 5 ++- 7 files changed, 138 insertions(+), 7 deletions(-) create mode 100644 cmd-select-layout.c diff --git a/CHANGES b/CHANGES index 0a2d6e21..5efcb712 100644 --- a/CHANGES +++ b/CHANGES @@ -1,5 +1,7 @@ 16 May 2009 +* select-layout command and a few default key bindings (M-0, M-1, M-2, M-9) to + select layouts. * Recreate server socket on SIGUSR1, per SF feature request 2792533. 14 May 2009 @@ -1261,7 +1263,7 @@ (including mutt, emacs). No status bar yet and no key remapping or other customisation. -$Id: CHANGES,v 1.289 2009-05-16 10:02:51 nicm Exp $ +$Id: CHANGES,v 1.290 2009-05-16 11:48:47 nicm Exp $ LocalWords: showw utf UTF fulvio ciriaco joshe OSC APC gettime abc DEF OA clr LocalWords: rivo nurges lscm Erdely eol smysession mysession ek dstname RB ms diff --git a/TODO b/TODO index b5044bd2..8693d52c 100644 --- a/TODO +++ b/TODO @@ -79,7 +79,6 @@ - attach should have a flag to create session if it doesn't exist - layout/split stuff: horiz split command, and similar resizing commands as for vert split - select-layout command make manual layout a bit less of a hack and make it handle a grid should the layout be a window option??? more layouts @@ -89,4 +88,4 @@ - document clear-history - document paste in status line - document SIGUSR1 behaviour - +- document select-layout diff --git a/cmd-select-layout.c b/cmd-select-layout.c new file mode 100644 index 00000000..852848a6 --- /dev/null +++ b/cmd-select-layout.c @@ -0,0 +1,86 @@ +/* $Id: cmd-select-layout.c,v 1.1 2009-05-16 11:48:47 nicm Exp $ */ + +/* + * Copyright (c) 2009 Nicholas Marriott + * + * 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 + +#include "tmux.h" + +/* + * Switch window to selected layout. + */ + +void cmd_select_layout_init(struct cmd *, int); +int cmd_select_layout_exec(struct cmd *, struct cmd_ctx *); + +const struct cmd_entry cmd_select_layout_entry = { + "select-layout", "selectl", + CMD_TARGET_WINDOW_USAGE " layout-name", + CMD_ARG1, + cmd_select_layout_init, + cmd_target_parse, + cmd_select_layout_exec, + cmd_target_send, + cmd_target_recv, + cmd_target_free, + cmd_target_print +}; + +void +cmd_select_layout_init(struct cmd *self, int key) +{ + struct cmd_target_data *data; + + cmd_target_init(self, key); + data = self->data; + + switch (key) { + case KEYC_ADDESC('0'): + data->arg = xstrdup("manual"); + break; + case KEYC_ADDESC('1'): + data->arg = xstrdup("even-horizontal"); + break; + case KEYC_ADDESC('2'): + data->arg = xstrdup("even-vertical"); + break; + case KEYC_ADDESC('9'): + data->arg = xstrdup("active-only"); + break; + } +} + +int +cmd_select_layout_exec(struct cmd *self, struct cmd_ctx *ctx) +{ + struct cmd_target_data *data = self->data; + struct winlink *wl; + int layout; + + if ((wl = cmd_find_window(ctx, data->target, NULL)) == NULL) + return (-1); + + if ((layout = layout_lookup(data->arg)) == -1) { + ctx->error(ctx, "unknown or ambiguous layout: %s", data->arg); + return (-1); + } + + if (layout_select(wl->window, layout) == 0) + ctx->info(ctx, "layout now: %s", layout_name(wl->window)); + + return (0); +} diff --git a/cmd.c b/cmd.c index 45188cac..2f762188 100644 --- a/cmd.c +++ b/cmd.c @@ -1,4 +1,4 @@ -/* $Id: cmd.c,v 1.94 2009-05-14 16:56:23 nicm Exp $ */ +/* $Id: cmd.c,v 1.95 2009-05-16 11:48:47 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -72,6 +72,7 @@ const struct cmd_entry *cmd_table[] = { &cmd_rotate_window_entry, &cmd_save_buffer_entry, &cmd_scroll_mode_entry, + &cmd_select_layout_entry, &cmd_select_pane_entry, &cmd_select_prompt_entry, &cmd_select_window_entry, diff --git a/key-bindings.c b/key-bindings.c index 320a1e08..91d63474 100644 --- a/key-bindings.c +++ b/key-bindings.c @@ -1,4 +1,4 @@ -/* $Id: key-bindings.c,v 1.71 2009-05-13 22:10:39 nicm Exp $ */ +/* $Id: key-bindings.c,v 1.72 2009-05-16 11:48:47 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -119,6 +119,11 @@ key_bindings_init(void) { '{', 0, &cmd_swap_pane_entry }, { '}', 0, &cmd_swap_pane_entry }, { '\002', 0, &cmd_send_prefix_entry }, + { KEYC_ADDESC('0'), 0, &cmd_select_layout_entry }, + { KEYC_ADDESC('1'), 0, &cmd_select_layout_entry }, + { KEYC_ADDESC('2'), 0, &cmd_select_layout_entry }, + { KEYC_ADDESC('9'), 0, &cmd_select_layout_entry }, + { KEYC_ADDCTL(KEYC_DOWN), 1, &cmd_resize_pane_entry }, { KEYC_PPAGE, 0, &cmd_scroll_mode_entry }, { KEYC_ADDESC('n'), 0, &cmd_next_window_entry }, { KEYC_ADDESC('p'), 0, &cmd_previous_window_entry }, diff --git a/layout.c b/layout.c index 1b80628e..a97cecf9 100644 --- a/layout.c +++ b/layout.c @@ -1,4 +1,4 @@ -/* $Id: layout.c,v 1.6 2009-05-04 17:58:27 nicm Exp $ */ +/* $Id: layout.c,v 1.7 2009-05-16 11:48:47 nicm Exp $ */ /* * Copyright (c) 2009 Nicholas Marriott @@ -18,6 +18,8 @@ #include +#include + #include "tmux.h" /* @@ -48,6 +50,39 @@ layout_name(struct window *w) return (layouts[w->layout].name); } +int +layout_lookup(const char *name) +{ + u_int i; + int matched = -1; + + for (i = 0; i < nitems(layouts); i++) { + if (strncmp(layouts[i].name, name, strlen(name)) == 0) { + if (matched != -1) /* ambiguous */ + return (-1); + matched = i; + } + } + + return (matched); +} + +int +layout_select(struct window *w, u_int layout) +{ + if (layout > nitems(layouts) - 1 || layout == w->layout) + return (-1); + w->layout = layout; + + if (w->layout == 0) { + /* XXX Special-case manual. */ + window_fit_panes(w); + window_update_panes(w); + } + layout_refresh(w, 0); + return (0); +} + void layout_next(struct window *w) { diff --git a/tmux.h b/tmux.h index bab5503b..e58ac097 100644 --- a/tmux.h +++ b/tmux.h @@ -1,4 +1,4 @@ -/* $Id: tmux.h,v 1.317 2009-05-16 10:02:51 nicm Exp $ */ +/* $Id: tmux.h,v 1.318 2009-05-16 11:48:47 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -1249,6 +1249,7 @@ extern const struct cmd_entry cmd_respawn_window_entry; extern const struct cmd_entry cmd_rotate_window_entry; extern const struct cmd_entry cmd_save_buffer_entry; extern const struct cmd_entry cmd_scroll_mode_entry; +extern const struct cmd_entry cmd_select_layout_entry; extern const struct cmd_entry cmd_select_pane_entry; extern const struct cmd_entry cmd_select_prompt_entry; extern const struct cmd_entry cmd_select_window_entry; @@ -1568,7 +1569,9 @@ void window_pane_mouse(struct window_pane *, /* layout.c */ const char * layout_name(struct window *); +int layout_lookup(const char *); void layout_refresh(struct window *, int); +int layout_select(struct window *, u_int); void layout_next(struct window *); void layout_previous(struct window *);