diff options
author | 2012-09-03 09:57:57 +0000 | |
---|---|---|
committer | 2012-09-03 09:57:57 +0000 | |
commit | e0a16d1ec90800b58e62fc7a94598502d0fe6317 (patch) | |
tree | 0675fd1f34f2dfb93c7e527ac6c6265263bccf09 | |
parent | Remove an unused variable. (diff) | |
download | wireguard-openbsd-e0a16d1ec90800b58e62fc7a94598502d0fe6317.tar.xz wireguard-openbsd-e0a16d1ec90800b58e62fc7a94598502d0fe6317.zip |
add cmd-choose-list to allow arbitrary options to be selected. From
Thomas Adam.
-rw-r--r-- | usr.bin/tmux/Makefile | 3 | ||||
-rw-r--r-- | usr.bin/tmux/cmd-choose-list.c | 117 | ||||
-rw-r--r-- | usr.bin/tmux/cmd.c | 3 | ||||
-rw-r--r-- | usr.bin/tmux/tmux.1 | 31 | ||||
-rw-r--r-- | usr.bin/tmux/tmux.h | 6 | ||||
-rw-r--r-- | usr.bin/tmux/window-choose.c | 34 |
6 files changed, 188 insertions, 6 deletions
diff --git a/usr.bin/tmux/Makefile b/usr.bin/tmux/Makefile index cb75e9b73b2..8da32a5cd39 100644 --- a/usr.bin/tmux/Makefile +++ b/usr.bin/tmux/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.60 2012/09/03 09:32:38 nicm Exp $ +# $OpenBSD: Makefile,v 1.61 2012/09/03 09:57:57 nicm Exp $ PROG= tmux SRCS= arguments.c \ @@ -12,6 +12,7 @@ SRCS= arguments.c \ cmd-capture-pane.c \ cmd-choose-buffer.c \ cmd-choose-client.c \ + cmd-choose-list.c \ cmd-choose-tree.c \ cmd-clear-history.c \ cmd-clock-mode.c \ diff --git a/usr.bin/tmux/cmd-choose-list.c b/usr.bin/tmux/cmd-choose-list.c new file mode 100644 index 00000000000..fc94a2ada03 --- /dev/null +++ b/usr.bin/tmux/cmd-choose-list.c @@ -0,0 +1,117 @@ +/* $Id: cmd-choose-list.c,v 1.1 2012/09/03 09:57:57 nicm Exp $ */ + +/* + * Copyright (c) 2012 Thomas Adam <thomas@xteddy.org> + * + * 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" + +#define CMD_CHOOSE_LIST_DEFAULT_TEMPLATE "run-shell '%%'" + +/* + * Enter choose mode to choose a custom list. + */ + +enum cmd_retval cmd_choose_list_exec(struct cmd *, struct cmd_ctx *); + +void cmd_choose_list_callback(struct window_choose_data *); +void cmd_choose_list_free(struct window_choose_data *); + +const struct cmd_entry cmd_choose_list_entry = { + "choose-list", NULL, + "l:t:", 0, 1, + "[-l items] " CMD_TARGET_WINDOW_USAGE "[template]", + 0, + NULL, + NULL, + cmd_choose_list_exec +}; + +enum cmd_retval +cmd_choose_list_exec(struct cmd *self, struct cmd_ctx *ctx) +{ + struct args *args = self->args; + struct winlink *wl; + const char *lists; + char *template, *list, *copy, *lists1; + u_int idx; + + if (ctx->curclient == NULL) { + ctx->error(ctx, "must be run interactively"); + return (CMD_RETURN_ERROR); + } + + if ((lists = args_get(args, 'l')) == NULL) + return (CMD_RETURN_ERROR); + + if ((wl = cmd_find_window(ctx, args_get(args, 't'), NULL)) == NULL) + return (CMD_RETURN_ERROR); + + if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0) + return (CMD_RETURN_NORMAL); + + if (args->argc != 0) + template = xstrdup(args->argv[0]); + else + template = xstrdup(CMD_CHOOSE_LIST_DEFAULT_TEMPLATE); + + copy = lists1 = xstrdup(lists); + idx = 0; + while ((list = strsep(&lists1, ",")) != NULL) + { + if (*list == '\0') /* no empty entries */ + continue; + window_choose_add_item(wl->window->active, ctx, wl, list, + template, idx); + idx++; + } + free(copy); + + window_choose_ready(wl->window->active, 0, cmd_choose_list_callback, + cmd_choose_list_free); + + free(template); + + return (CMD_RETURN_NORMAL); +} + +void +cmd_choose_list_callback(struct window_choose_data *cdata) +{ + if (cdata == NULL || (cdata->client->flags & CLIENT_DEAD)) + return; + + window_choose_ctx(cdata); +} + +void +cmd_choose_list_free(struct window_choose_data *cdata) +{ + cdata->session->references--; + cdata->client->references--; + + free(cdata->ft_template); + free(cdata->command); + format_free(cdata->ft); + free(cdata); + +} diff --git a/usr.bin/tmux/cmd.c b/usr.bin/tmux/cmd.c index b562d9492e0..c3002f9f5e0 100644 --- a/usr.bin/tmux/cmd.c +++ b/usr.bin/tmux/cmd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd.c,v 1.69 2012/07/11 07:10:15 nicm Exp $ */ +/* $OpenBSD: cmd.c,v 1.70 2012/09/03 09:57:57 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -35,6 +35,7 @@ const struct cmd_entry *cmd_table[] = { &cmd_capture_pane_entry, &cmd_choose_buffer_entry, &cmd_choose_client_entry, + &cmd_choose_list_entry, &cmd_choose_session_entry, &cmd_choose_tree_entry, &cmd_choose_window_entry, diff --git a/usr.bin/tmux/tmux.1 b/usr.bin/tmux/tmux.1 index c27d922fa8a..596045ca3d0 100644 --- a/usr.bin/tmux/tmux.1 +++ b/usr.bin/tmux/tmux.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: tmux.1,v 1.302 2012/08/11 07:10:01 nicm Exp $ +.\" $OpenBSD: tmux.1,v 1.303 2012/09/03 09:57:57 nicm Exp $ .\" .\" Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> .\" @@ -14,7 +14,7 @@ .\" IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING .\" OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: August 11 2012 $ +.Dd $Mdocdate: September 3 2012 $ .Dt TMUX 1 .Os .Sh NAME @@ -1067,6 +1067,33 @@ section. This command works only from inside .Nm . .It Xo +.Ic choose-list +.Op Fl l Ar items +.Op Fl t Ar target-window +.Op Ar template +.Xc +Put a window into list choice mode, allowing +.Ar items +to be selected. +.Ar items +can be a comma-separated list to display more than one item. +If an item has spaces, that entry must be quoted. +After an item is chosen, +.Ql %% +is replaced by the chosen item in the +.Ar template +and the result is executed as a command. +If +.Ar template +is not given, "run-shell '%%'" is used. +.Ar items +also accepts format specifiers. +For the meaning of this see the +.Sx FORMATS +section. +This command works only from inside +.Nm . +.It Xo .Ic choose-session .Op Fl F Ar format .Op Fl t Ar target-window diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h index a70e06c2a80..1133237c7bd 100644 --- a/usr.bin/tmux/tmux.h +++ b/usr.bin/tmux/tmux.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tmux.h,v 1.353 2012/09/03 09:32:38 nicm Exp $ */ +/* $OpenBSD: tmux.h,v 1.354 2012/09/03 09:57:57 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -1716,6 +1716,7 @@ extern const struct cmd_entry cmd_break_pane_entry; extern const struct cmd_entry cmd_capture_pane_entry; extern const struct cmd_entry cmd_choose_buffer_entry; extern const struct cmd_entry cmd_choose_client_entry; +extern const struct cmd_entry cmd_choose_list_entry; extern const struct cmd_entry cmd_choose_session_entry; extern const struct cmd_entry cmd_choose_tree_entry; extern const struct cmd_entry cmd_choose_window_entry; @@ -2204,6 +2205,9 @@ struct window_choose_data *window_choose_add_window(struct window_pane *, struct window_choose_data *window_choose_add_session(struct window_pane *, struct cmd_ctx *, struct session *, const char *, char *, u_int); +struct window_choose_data *window_choose_add_item(struct window_pane *, + struct cmd_ctx *, struct winlink *, const char *, + char *, u_int); /* names.c */ void queue_window_name(struct window *); diff --git a/usr.bin/tmux/window-choose.c b/usr.bin/tmux/window-choose.c index 4b18472ae49..1c95512c851 100644 --- a/usr.bin/tmux/window-choose.c +++ b/usr.bin/tmux/window-choose.c @@ -1,4 +1,4 @@ -/* $OpenBSD: window-choose.c,v 1.25 2012/08/27 21:29:23 nicm Exp $ */ +/* $OpenBSD: window-choose.c,v 1.26 2012/09/03 09:57:57 nicm Exp $ */ /* * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net> @@ -637,6 +637,38 @@ window_choose_add_session(struct window_pane *wp, struct cmd_ctx *ctx, } struct window_choose_data * +window_choose_add_item(struct window_pane *wp, struct cmd_ctx *ctx, + struct winlink *wl, const char *template, char *action, u_int idx) +{ + struct window_choose_data *wcd; + char *action_data; + + wcd = window_choose_data_create(ctx); + wcd->idx = wl->idx; + wcd->ft_template = xstrdup(template); + format_add(wcd->ft, "line", "%u", idx); + format_session(wcd->ft, wcd->session); + format_winlink(wcd->ft, wcd->session, wl); + format_window_pane(wcd->ft, wl->window->active); + + wcd->client->references++; + wcd->session->references++; + + window_choose_add(wp, wcd); + + /* + * Interpolate action_data here, since the data we pass back is the + * expanded template itself. + */ + xasprintf(&action_data, "%s", format_expand(wcd->ft, wcd->ft_template)); + wcd->command = cmd_template_replace(action, action_data, 1); + free(action_data); + + return (wcd); + +} + +struct window_choose_data * window_choose_add_window(struct window_pane *wp, struct cmd_ctx *ctx, struct session *s, struct winlink *wl, const char *template, char *action, u_int idx) |