Annotation of src/usr.bin/tmux/cmd-choose-list.c, Revision 1.1
1.1 ! nicm 1: /* $Id$ */
! 2:
! 3: /*
! 4: * Copyright (c) 2012 Thomas Adam <thomas@xteddy.org>
! 5: *
! 6: * Permission to use, copy, modify, and distribute this software for any
! 7: * purpose with or without fee is hereby granted, provided that the above
! 8: * copyright notice and this permission notice appear in all copies.
! 9: *
! 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
! 11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
! 12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
! 13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
! 14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
! 15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
! 16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
! 17: */
! 18:
! 19: #include <sys/types.h>
! 20:
! 21: #include <ctype.h>
! 22: #include <stdlib.h>
! 23:
! 24: #include <string.h>
! 25:
! 26: #include "tmux.h"
! 27:
! 28: #define CMD_CHOOSE_LIST_DEFAULT_TEMPLATE "run-shell '%%'"
! 29:
! 30: /*
! 31: * Enter choose mode to choose a custom list.
! 32: */
! 33:
! 34: enum cmd_retval cmd_choose_list_exec(struct cmd *, struct cmd_ctx *);
! 35:
! 36: void cmd_choose_list_callback(struct window_choose_data *);
! 37: void cmd_choose_list_free(struct window_choose_data *);
! 38:
! 39: const struct cmd_entry cmd_choose_list_entry = {
! 40: "choose-list", NULL,
! 41: "l:t:", 0, 1,
! 42: "[-l items] " CMD_TARGET_WINDOW_USAGE "[template]",
! 43: 0,
! 44: NULL,
! 45: NULL,
! 46: cmd_choose_list_exec
! 47: };
! 48:
! 49: enum cmd_retval
! 50: cmd_choose_list_exec(struct cmd *self, struct cmd_ctx *ctx)
! 51: {
! 52: struct args *args = self->args;
! 53: struct winlink *wl;
! 54: const char *lists;
! 55: char *template, *list, *copy, *lists1;
! 56: u_int idx;
! 57:
! 58: if (ctx->curclient == NULL) {
! 59: ctx->error(ctx, "must be run interactively");
! 60: return (CMD_RETURN_ERROR);
! 61: }
! 62:
! 63: if ((lists = args_get(args, 'l')) == NULL)
! 64: return (CMD_RETURN_ERROR);
! 65:
! 66: if ((wl = cmd_find_window(ctx, args_get(args, 't'), NULL)) == NULL)
! 67: return (CMD_RETURN_ERROR);
! 68:
! 69: if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
! 70: return (CMD_RETURN_NORMAL);
! 71:
! 72: if (args->argc != 0)
! 73: template = xstrdup(args->argv[0]);
! 74: else
! 75: template = xstrdup(CMD_CHOOSE_LIST_DEFAULT_TEMPLATE);
! 76:
! 77: copy = lists1 = xstrdup(lists);
! 78: idx = 0;
! 79: while ((list = strsep(&lists1, ",")) != NULL)
! 80: {
! 81: if (*list == '\0') /* no empty entries */
! 82: continue;
! 83: window_choose_add_item(wl->window->active, ctx, wl, list,
! 84: template, idx);
! 85: idx++;
! 86: }
! 87: free(copy);
! 88:
! 89: window_choose_ready(wl->window->active, 0, cmd_choose_list_callback,
! 90: cmd_choose_list_free);
! 91:
! 92: free(template);
! 93:
! 94: return (CMD_RETURN_NORMAL);
! 95: }
! 96:
! 97: void
! 98: cmd_choose_list_callback(struct window_choose_data *cdata)
! 99: {
! 100: if (cdata == NULL || (cdata->client->flags & CLIENT_DEAD))
! 101: return;
! 102:
! 103: window_choose_ctx(cdata);
! 104: }
! 105:
! 106: void
! 107: cmd_choose_list_free(struct window_choose_data *cdata)
! 108: {
! 109: cdata->session->references--;
! 110: cdata->client->references--;
! 111:
! 112: free(cdata->ft_template);
! 113: free(cdata->command);
! 114: format_free(cdata->ft);
! 115: free(cdata);
! 116:
! 117: }