Annotation of src/usr.bin/tmux/cmd-choose-list.c, Revision 1.3
1.3 ! nicm 1: /* $Id: cmd-choose-list.c,v 1.2 2012/09/03 10:02:39 nicm Exp $ */
1.1 nicm 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;
1.2 nicm 54: const char *list1;
55: char *template, *item, *copy, *list;
1.1 nicm 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:
1.2 nicm 63: if ((list1 = args_get(args, 'l')) == NULL)
1.1 nicm 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:
1.2 nicm 77: copy = list = xstrdup(list1);
1.1 nicm 78: idx = 0;
1.2 nicm 79: while ((item = strsep(&list, ",")) != NULL)
1.1 nicm 80: {
1.2 nicm 81: if (*item == '\0') /* no empty entries */
1.1 nicm 82: continue;
1.2 nicm 83: window_choose_add_item(wl->window->active, ctx, wl, item,
1.1 nicm 84: template, idx);
85: idx++;
86: }
87: free(copy);
1.3 ! nicm 88:
! 89: if (idx == 0) {
! 90: free(template);
! 91: window_pane_reset_mode(wl->window->active);
! 92: return (CMD_RETURN_ERROR);
! 93: }
1.1 nicm 94:
95: window_choose_ready(wl->window->active, 0, cmd_choose_list_callback,
96: cmd_choose_list_free);
97:
98: free(template);
99:
100: return (CMD_RETURN_NORMAL);
101: }
102:
103: void
104: cmd_choose_list_callback(struct window_choose_data *cdata)
105: {
106: if (cdata == NULL || (cdata->client->flags & CLIENT_DEAD))
107: return;
108:
109: window_choose_ctx(cdata);
110: }
111:
112: void
113: cmd_choose_list_free(struct window_choose_data *cdata)
114: {
115: cdata->session->references--;
116: cdata->client->references--;
117:
118: free(cdata->ft_template);
119: free(cdata->command);
120: format_free(cdata->ft);
121: free(cdata);
122:
123: }