1*d39fdc49Snicm /* $OpenBSD: cmd-paste-buffer.c,v 1.42 2024/02/13 08:03:50 nicm Exp $ */
2311827fbSnicm
3311827fbSnicm /*
498ca8272Snicm * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5311827fbSnicm *
6311827fbSnicm * Permission to use, copy, modify, and distribute this software for any
7311827fbSnicm * purpose with or without fee is hereby granted, provided that the above
8311827fbSnicm * copyright notice and this permission notice appear in all copies.
9311827fbSnicm *
10311827fbSnicm * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11311827fbSnicm * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12311827fbSnicm * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13311827fbSnicm * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14311827fbSnicm * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15311827fbSnicm * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16311827fbSnicm * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17311827fbSnicm */
18311827fbSnicm
19311827fbSnicm #include <sys/types.h>
20311827fbSnicm
21a24c6edeSnicm #include <stdlib.h>
22311827fbSnicm #include <string.h>
23a24c6edeSnicm #include <vis.h>
24311827fbSnicm
25311827fbSnicm #include "tmux.h"
26311827fbSnicm
27311827fbSnicm /*
28311827fbSnicm * Paste paste buffer if present.
29311827fbSnicm */
30311827fbSnicm
3168e0a7f2Snicm static enum cmd_retval cmd_paste_buffer_exec(struct cmd *, struct cmdq_item *);
32311827fbSnicm
33311827fbSnicm const struct cmd_entry cmd_paste_buffer_entry = {
34c057646bSnicm .name = "paste-buffer",
35c057646bSnicm .alias = "pasteb",
36c057646bSnicm
37a51dead1Snicm .args = { "db:prs:t:", 0, 0, NULL },
38c057646bSnicm .usage = "[-dpr] [-s separator] " CMD_BUFFER_USAGE " "
39c057646bSnicm CMD_TARGET_PANE_USAGE,
40c057646bSnicm
41bf0d297eSnicm .target = { 't', CMD_FIND_PANE, 0 },
428d471e80Snicm
437a61a8ddSnicm .flags = CMD_AFTERHOOK,
44c057646bSnicm .exec = cmd_paste_buffer_exec
45311827fbSnicm };
46311827fbSnicm
47dc1f0f5fSnicm static enum cmd_retval
cmd_paste_buffer_exec(struct cmd * self,struct cmdq_item * item)4868e0a7f2Snicm cmd_paste_buffer_exec(struct cmd *self, struct cmdq_item *item)
49311827fbSnicm {
5090d7ba38Snicm struct args *args = cmd_get_args(self);
51040343aeSnicm struct cmd_find_state *target = cmdq_get_target(item);
52040343aeSnicm struct window_pane *wp = target->wp;
53311827fbSnicm struct paste_buffer *pb;
5495e33999Snicm const char *sepstr, *bufname, *bufdata, *bufend, *line;
5595e33999Snicm size_t seplen, bufsize;
5695e33999Snicm int bracket = args_has(args, 'p');
57311827fbSnicm
58*d39fdc49Snicm if (window_pane_exited(wp)) {
59*d39fdc49Snicm cmdq_error(item, "target pane has exited");
60*d39fdc49Snicm return (CMD_RETURN_ERROR);
61*d39fdc49Snicm }
62*d39fdc49Snicm
63a41fa27aSnicm bufname = NULL;
64a41fa27aSnicm if (args_has(args, 'b'))
65a41fa27aSnicm bufname = args_get(args, 'b');
66ca7befccSnicm
67a41fa27aSnicm if (bufname == NULL)
68909ecc87Snicm pb = paste_get_top(NULL);
69311827fbSnicm else {
70a41fa27aSnicm pb = paste_get_name(bufname);
71eca05f1fSnicm if (pb == NULL) {
7268e0a7f2Snicm cmdq_error(item, "no buffer %s", bufname);
73a224d0d3Snicm return (CMD_RETURN_ERROR);
74311827fbSnicm }
75311827fbSnicm }
76311827fbSnicm
7795e33999Snicm if (pb != NULL && ~wp->flags & PANE_INPUTOFF) {
78ca7befccSnicm sepstr = args_get(args, 's');
79ca7befccSnicm if (sepstr == NULL) {
80ca7befccSnicm if (args_has(args, 'r'))
81ca7befccSnicm sepstr = "\n";
82ca7befccSnicm else
83ca7befccSnicm sepstr = "\r";
84ca7befccSnicm }
8595e33999Snicm seplen = strlen(sepstr);
8695e33999Snicm
8795e33999Snicm if (bracket && (wp->screen->mode & MODE_BRACKETPASTE))
8895e33999Snicm bufferevent_write(wp->event, "\033[200~", 6);
8995e33999Snicm
9095e33999Snicm bufdata = paste_buffer_data(pb, &bufsize);
9195e33999Snicm bufend = bufdata + bufsize;
9295e33999Snicm
9395e33999Snicm for (;;) {
9495e33999Snicm line = memchr(bufdata, '\n', bufend - bufdata);
9595e33999Snicm if (line == NULL)
9695e33999Snicm break;
9795e33999Snicm
9895e33999Snicm bufferevent_write(wp->event, bufdata, line - bufdata);
9995e33999Snicm bufferevent_write(wp->event, sepstr, seplen);
10095e33999Snicm
10195e33999Snicm bufdata = line + 1;
10295e33999Snicm }
10395e33999Snicm if (bufdata != bufend)
10495e33999Snicm bufferevent_write(wp->event, bufdata, bufend - bufdata);
10595e33999Snicm
10695e33999Snicm if (bracket && (wp->screen->mode & MODE_BRACKETPASTE))
10795e33999Snicm bufferevent_write(wp->event, "\033[201~", 6);
108ca7befccSnicm }
109311827fbSnicm
11064185147Snicm if (pb != NULL && args_has(args, 'd'))
11164185147Snicm paste_free(pb);
112311827fbSnicm
113a224d0d3Snicm return (CMD_RETURN_NORMAL);
114311827fbSnicm }
115