Annotation of src/usr.bin/tmux/cmd-display-panes.c, Revision 1.22
1.22 ! kn 1: /* $OpenBSD: cmd-display-panes.c,v 1.21 2018/08/02 07:55:16 nicm Exp $ */
1.1 nicm 2:
3: /*
1.12 nicm 4: * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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:
1.13 nicm 21: #include <ctype.h>
22: #include <stdlib.h>
23:
1.1 nicm 24: #include "tmux.h"
25:
26: /*
27: * Display panes on a client.
28: */
29:
1.16 nicm 30: static enum cmd_retval cmd_display_panes_exec(struct cmd *,
31: struct cmdq_item *);
1.13 nicm 32:
1.16 nicm 33: static void cmd_display_panes_callback(struct client *,
34: struct window_pane *);
1.1 nicm 35:
36: const struct cmd_entry cmd_display_panes_entry = {
1.10 nicm 37: .name = "display-panes",
38: .alias = "displayp",
39:
1.20 nicm 40: .args = { "d:t:", 0, 1 },
1.22 ! kn 41: .usage = "[-d duration] " CMD_TARGET_CLIENT_USAGE " [template]",
1.10 nicm 42:
1.14 nicm 43: .flags = CMD_AFTERHOOK,
1.10 nicm 44: .exec = cmd_display_panes_exec
1.1 nicm 45: };
46:
1.13 nicm 47: static enum cmd_retval
1.16 nicm 48: cmd_display_panes_exec(struct cmd *self, struct cmdq_item *item)
1.1 nicm 49: {
1.13 nicm 50: struct args *args = self->args;
1.19 nicm 51: struct client *c;
1.20 nicm 52: struct session *s;
53: u_int delay;
54: char *cause;
1.19 nicm 55:
56: if ((c = cmd_find_client(item, args_get(args, 't'), 0)) == NULL)
57: return (CMD_RETURN_ERROR);
1.21 nicm 58: s = c->session;
1.13 nicm 59:
60: if (c->identify_callback != NULL)
61: return (CMD_RETURN_NORMAL);
62:
63: c->identify_callback = cmd_display_panes_callback;
64: if (args->argc != 0)
65: c->identify_callback_data = xstrdup(args->argv[0]);
66: else
67: c->identify_callback_data = xstrdup("select-pane -t '%%'");
1.21 nicm 68: c->identify_callback_item = item;
1.13 nicm 69:
1.20 nicm 70: if (args_has(args, 'd')) {
71: delay = args_strtonum(args, 'd', 0, UINT_MAX, &cause);
72: if (cause != NULL) {
73: cmdq_error(item, "delay %s", cause);
74: free(cause);
75: return (CMD_RETURN_ERROR);
76: }
77: } else
78: delay = options_get_number(s->options, "display-panes-time");
79: server_client_set_identify(c, delay);
1.1 nicm 80:
1.21 nicm 81: return (CMD_RETURN_WAIT);
1.13 nicm 82: }
83:
1.15 nicm 84: static enum cmd_retval
1.16 nicm 85: cmd_display_panes_error(struct cmdq_item *item, void *data)
1.15 nicm 86: {
87: char *error = data;
88:
1.16 nicm 89: cmdq_error(item, "%s", error);
1.15 nicm 90: free(error);
91:
92: return (CMD_RETURN_NORMAL);
93: }
94:
1.13 nicm 95: static void
96: cmd_display_panes_callback(struct client *c, struct window_pane *wp)
97: {
1.16 nicm 98: struct cmd_list *cmdlist;
99: struct cmdq_item *new_item;
1.21 nicm 100: char *cmd, *expanded, *cause;
1.13 nicm 101:
1.15 nicm 102: if (wp == NULL)
103: goto out;
1.21 nicm 104:
1.15 nicm 105: xasprintf(&expanded, "%%%u", wp->id);
1.21 nicm 106: cmd = cmd_template_replace(c->identify_callback_data, expanded, 1);
1.15 nicm 107:
1.17 nicm 108: cmdlist = cmd_string_parse(cmd, NULL, 0, &cause);
1.21 nicm 109: if (cmdlist == NULL && cause != NULL)
110: new_item = cmdq_get_callback(cmd_display_panes_error, cause);
111: else if (cmdlist == NULL)
112: new_item = NULL;
113: else {
1.16 nicm 114: new_item = cmdq_get_command(cmdlist, NULL, NULL, 0);
1.15 nicm 115: cmd_list_free(cmdlist);
116: }
1.13 nicm 117:
1.16 nicm 118: if (new_item != NULL)
1.21 nicm 119: cmdq_insert_after(c->identify_callback_item, new_item);
1.15 nicm 120:
121: free(cmd);
122: free(expanded);
1.13 nicm 123:
1.15 nicm 124: out:
1.21 nicm 125: c->identify_callback_item->flags &= ~CMDQ_WAITING;
126: c->identify_callback_item = NULL;
127:
1.13 nicm 128: free(c->identify_callback_data);
129: c->identify_callback_data = NULL;
1.21 nicm 130:
1.13 nicm 131: c->identify_callback = NULL;
1.1 nicm 132: }