1 /* $OpenBSD$ */
2 
3 /*
4  * Copyright (c) 2010 Nicholas Marriott <nicholas.marriott@gmail.com>
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 "tmux.h"
25 
26 /*
27  * Enter choice mode to choose a buffer.
28  */
29 
30 #define CHOOSE_BUFFER_TEMPLATE						\
31 	"#{buffer_name}: #{buffer_size} bytes: #{buffer_sample}"
32 
33 enum cmd_retval	 cmd_choose_buffer_exec(struct cmd *, struct cmd_q *);
34 
35 const struct cmd_entry cmd_choose_buffer_entry = {
36 	.name = "choose-buffer",
37 	.alias = NULL,
38 
39 	.args = { "F:t:", 0, 1 },
40 	.usage = CMD_TARGET_WINDOW_USAGE " [-F format] [template]",
41 
42 	.tflag = CMD_WINDOW,
43 
44 	.flags = 0,
45 	.exec = cmd_choose_buffer_exec
46 };
47 
48 enum cmd_retval
cmd_choose_buffer_exec(struct cmd * self,struct cmd_q * cmdq)49 cmd_choose_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
50 {
51 	struct args			*args = self->args;
52 	struct client			*c = cmdq->state.c;
53 	struct winlink			*wl = cmdq->state.tflag.wl;
54 	struct window_choose_data	*cdata;
55 	struct paste_buffer		*pb;
56 	char				*action, *action_data;
57 	const char			*template;
58 	u_int				 idx;
59 
60 	if (c == NULL) {
61 		cmdq_error(cmdq, "no client available");
62 		return (CMD_RETURN_ERROR);
63 	}
64 
65 	if ((template = args_get(args, 'F')) == NULL)
66 		template = CHOOSE_BUFFER_TEMPLATE;
67 
68 	if (paste_get_top(NULL) == NULL)
69 		return (CMD_RETURN_NORMAL);
70 
71 	if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
72 		return (CMD_RETURN_NORMAL);
73 
74 	if (args->argc != 0)
75 		action = xstrdup(args->argv[0]);
76 	else
77 		action = xstrdup("paste-buffer -b '%%'");
78 
79 	idx = 0;
80 	pb = NULL;
81 	while ((pb = paste_walk(pb)) != NULL) {
82 		cdata = window_choose_data_create(TREE_OTHER, c, c->session);
83 		cdata->idx = idx;
84 
85 		cdata->ft_template = xstrdup(template);
86 		format_defaults_paste_buffer(cdata->ft, pb);
87 
88 		xasprintf(&action_data, "%s", paste_buffer_name(pb));
89 		cdata->command = cmd_template_replace(action, action_data, 1);
90 		free(action_data);
91 
92 		window_choose_add(wl->window->active, cdata);
93 		idx++;
94 	}
95 	free(action);
96 
97 	window_choose_ready(wl->window->active, 0, NULL);
98 
99 	return (CMD_RETURN_NORMAL);
100 }
101