MSG_EXIT can now have a return code in the message, so check for that
[tmux-openbsd.git] / cmd-choose-buffer.c
blob4008c54a72575a7ec637c7d146b4013b4ab5aa51
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2010 Nicholas Marriott <nicm@users.sourceforge.net>
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>
23 #include "tmux.h"
26 * Enter choice mode to choose a buffer.
29 int cmd_choose_buffer_exec(struct cmd *, struct cmd_ctx *);
31 void cmd_choose_buffer_callback(void *, int);
32 void cmd_choose_buffer_free(void *);
34 const struct cmd_entry cmd_choose_buffer_entry = {
35 "choose-buffer", NULL,
36 CMD_TARGET_WINDOW_USAGE " [template]",
37 CMD_ARG01, "",
38 cmd_target_init,
39 cmd_target_parse,
40 cmd_choose_buffer_exec,
41 cmd_target_free,
42 cmd_target_print
45 struct cmd_choose_buffer_data {
46 struct client *client;
47 char *template;
50 int
51 cmd_choose_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
53 struct cmd_target_data *data = self->data;
54 struct cmd_choose_buffer_data *cdata;
55 struct session *s;
56 struct winlink *wl;
57 struct paste_buffer *pb;
58 u_int idx;
59 char *tmp;
61 if (ctx->curclient == NULL) {
62 ctx->error(ctx, "must be run interactively");
63 return (-1);
65 s = ctx->curclient->session;
67 if ((wl = cmd_find_window(ctx, data->target, NULL)) == NULL)
68 return (-1);
70 if (paste_get_top(&s->buffers) == NULL)
71 return (0);
73 if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
74 return (0);
76 idx = 0;
77 while ((pb = paste_walk_stack(&s->buffers, &idx)) != NULL) {
78 tmp = paste_print(pb, 50);
79 window_choose_add(wl->window->active, idx - 1,
80 "%u: %zu bytes: \"%s\"", idx - 1, pb->size, tmp);
81 xfree(tmp);
84 cdata = xmalloc(sizeof *cdata);
85 if (data->arg != NULL)
86 cdata->template = xstrdup(data->arg);
87 else
88 cdata->template = xstrdup("paste-buffer -b '%%'");
89 cdata->client = ctx->curclient;
90 cdata->client->references++;
92 window_choose_ready(wl->window->active,
93 0, cmd_choose_buffer_callback, cmd_choose_buffer_free, cdata);
95 return (0);
98 void
99 cmd_choose_buffer_callback(void *data, int idx)
101 struct cmd_choose_buffer_data *cdata = data;
102 struct cmd_list *cmdlist;
103 struct cmd_ctx ctx;
104 char *template, *cause, tmp[16];
106 if (idx == -1)
107 return;
108 if (cdata->client->flags & CLIENT_DEAD)
109 return;
111 xsnprintf(tmp, sizeof tmp, "%u", idx);
112 template = cmd_template_replace(cdata->template, tmp, 1);
114 if (cmd_string_parse(template, &cmdlist, &cause) != 0) {
115 if (cause != NULL) {
116 *cause = toupper((u_char) *cause);
117 status_message_set(cdata->client, "%s", cause);
118 xfree(cause);
120 xfree(template);
121 return;
123 xfree(template);
125 ctx.msgdata = NULL;
126 ctx.curclient = cdata->client;
128 ctx.error = key_bindings_error;
129 ctx.print = key_bindings_print;
130 ctx.info = key_bindings_info;
132 ctx.cmdclient = NULL;
134 cmd_list_exec(cmdlist, &ctx);
135 cmd_list_free(cmdlist);
138 void
139 cmd_choose_buffer_free(void *data)
141 struct cmd_choose_buffer_data *cdata = data;
143 cdata->client->references--;
144 xfree(cdata->template);
145 xfree(cdata);