Annotation of src/usr.bin/tmux/cmd-choose-list.c, Revision 1.6
1.6 ! nicm 1: /* $Id: cmd-choose-list.c,v 1.5 2013/03/21 16:09:17 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: 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]",
40: 0,
41: NULL,
42: NULL,
43: cmd_choose_list_exec
44: };
45:
46: enum cmd_retval
47: cmd_choose_list_exec(struct cmd *self, struct cmd_ctx *ctx)
48: {
49: struct args *args = self->args;
1.6 ! nicm 50: struct client *c;
1.1 nicm 51: struct winlink *wl;
1.2 nicm 52: const char *list1;
53: char *template, *item, *copy, *list;
1.1 nicm 54: u_int idx;
55:
1.6 ! nicm 56: if ((c = cmd_current_client(ctx)) == NULL) {
! 57: ctx->error(ctx, "no client available");
1.1 nicm 58: return (CMD_RETURN_ERROR);
59: }
60:
1.2 nicm 61: if ((list1 = args_get(args, 'l')) == NULL)
1.1 nicm 62: return (CMD_RETURN_ERROR);
63:
64: if ((wl = cmd_find_window(ctx, args_get(args, 't'), NULL)) == NULL)
65: return (CMD_RETURN_ERROR);
66:
67: if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
68: return (CMD_RETURN_NORMAL);
69:
70: if (args->argc != 0)
71: template = xstrdup(args->argv[0]);
72: else
73: template = xstrdup(CMD_CHOOSE_LIST_DEFAULT_TEMPLATE);
74:
1.2 nicm 75: copy = list = xstrdup(list1);
1.1 nicm 76: idx = 0;
1.2 nicm 77: while ((item = strsep(&list, ",")) != NULL)
1.1 nicm 78: {
1.2 nicm 79: if (*item == '\0') /* no empty entries */
1.1 nicm 80: continue;
1.6 ! nicm 81: window_choose_add_item(wl->window->active, c, wl, item,
1.1 nicm 82: template, idx);
83: idx++;
84: }
85: free(copy);
1.3 nicm 86:
87: if (idx == 0) {
88: free(template);
89: window_pane_reset_mode(wl->window->active);
90: return (CMD_RETURN_ERROR);
91: }
1.1 nicm 92:
1.5 nicm 93: window_choose_ready(wl->window->active, 0, NULL);
1.1 nicm 94:
95: free(template);
96:
97: return (CMD_RETURN_NORMAL);
98: }