Instead of eating 1024 bytes or so for the arguments of each command,
[tmux-openbsd.git] / cmd-choose-list.c
blob15f872940f941272da3096559ef064f901514a61
1 /* $Id$ */
3 /*
4 * Copyright (c) 2012 Thomas Adam <thomas@xteddy.org>
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.
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.
19 #include <sys/types.h>
21 #include <ctype.h>
22 #include <stdlib.h>
24 #include <string.h>
26 #include "tmux.h"
28 #define CMD_CHOOSE_LIST_DEFAULT_TEMPLATE "run-shell '%%'"
31 * Enter choose mode to choose a custom list.
34 enum cmd_retval cmd_choose_list_exec(struct cmd *, struct cmd_q *);
36 const struct cmd_entry cmd_choose_list_entry = {
37 "choose-list", NULL,
38 "l:t:", 0, 1,
39 "[-l items] " CMD_TARGET_WINDOW_USAGE "[template]",
41 NULL,
42 NULL,
43 cmd_choose_list_exec
46 enum cmd_retval
47 cmd_choose_list_exec(struct cmd *self, struct cmd_q *cmdq)
49 struct args *args = self->args;
50 struct client *c;
51 struct winlink *wl;
52 const char *list1;
53 char *template, *item, *copy, *list;
54 u_int idx;
56 if ((c = cmd_current_client(cmdq)) == NULL) {
57 cmdq_error(cmdq, "no client available");
58 return (CMD_RETURN_ERROR);
61 if ((list1 = args_get(args, 'l')) == NULL)
62 return (CMD_RETURN_ERROR);
64 if ((wl = cmd_find_window(cmdq, args_get(args, 't'), NULL)) == NULL)
65 return (CMD_RETURN_ERROR);
67 if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
68 return (CMD_RETURN_NORMAL);
70 if (args->argc != 0)
71 template = xstrdup(args->argv[0]);
72 else
73 template = xstrdup(CMD_CHOOSE_LIST_DEFAULT_TEMPLATE);
75 copy = list = xstrdup(list1);
76 idx = 0;
77 while ((item = strsep(&list, ",")) != NULL)
79 if (*item == '\0') /* no empty entries */
80 continue;
81 window_choose_add_item(wl->window->active, c, wl, item,
82 template, idx);
83 idx++;
85 free(copy);
87 if (idx == 0) {
88 free(template);
89 window_pane_reset_mode(wl->window->active);
90 return (CMD_RETURN_ERROR);
93 window_choose_ready(wl->window->active, 0, NULL);
95 free(template);
97 return (CMD_RETURN_NORMAL);