This defines the choose-list command. --- trunk/cmd-choose-list.c | 125 +++++++++++++++++++++++++++++++++++++++++++++++ trunk/cmd.c | 1 + trunk/tmux.h | 1 + 3 files changed, 127 insertions(+) create mode 100644 trunk/cmd-choose-list.c
diff --git a/trunk/cmd-choose-list.c b/trunk/cmd-choose-list.c new file mode 100644 index 0000000..6f28c64 --- /dev/null +++ b/trunk/cmd-choose-list.c @@ -0,0 +1,125 @@ +/* $Id$ */ + +/* + * Copyright (c) 2012 Thomas Adam <tho...@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" + +/* Default template. */ +#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; + const char *template; + char *list; + 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); + + idx = 0; + while ((list = strsep((char **)&lists, ",")) != NULL) + { + /* + * Skip past trailing commas, resulting in the empty string + * being displayed. + */ + if (strlen(list) == 0) + continue; + + window_choose_add_item(wl->window->active, ctx, wl, + list, (char *)template, idx); + idx++; + } + + window_choose_ready(wl->window->active, 0, cmd_choose_list_callback, + cmd_choose_list_free); + + free((char *)template); + + return (CMD_RETURN_NORMAL); +} + +void +cmd_choose_list_callback(struct window_choose_data *cdata) +{ + if (cdata == NULL) + return; + + if (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/trunk/cmd.c b/trunk/cmd.c index 2413642..d4b7e97 100644 --- a/trunk/cmd.c +++ b/trunk/cmd.c @@ -34,6 +34,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/trunk/tmux.h b/trunk/tmux.h index 0dedd14..3cb4af7 100644 --- a/trunk/tmux.h +++ b/trunk/tmux.h @@ -1666,6 +1666,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; -- 1.7.10 ------------------------------------------------------------------------------ Live Security Virtual Conference Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ _______________________________________________ tmux-users mailing list tmux-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/tmux-users