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