2009-06-01 22:58:49 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2009-06-01 22:58:49 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2010-05-19 22:28:14 +00:00
|
|
|
#include <stdlib.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
#include <string.h>
|
2010-05-19 22:28:14 +00:00
|
|
|
#include <vis.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Paste paste buffer if present.
|
|
|
|
*/
|
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
static enum cmd_retval cmd_paste_buffer_exec(struct cmd *, struct cmdq_item *);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
const struct cmd_entry cmd_paste_buffer_entry = {
|
2015-12-13 21:53:57 +00:00
|
|
|
.name = "paste-buffer",
|
|
|
|
.alias = "pasteb",
|
|
|
|
|
2021-08-21 10:22:38 +00:00
|
|
|
.args = { "db:prs:t:", 0, 0, NULL },
|
2015-12-13 21:53:57 +00:00
|
|
|
.usage = "[-dpr] [-s separator] " CMD_BUFFER_USAGE " "
|
|
|
|
CMD_TARGET_PANE_USAGE,
|
|
|
|
|
2017-04-22 10:22:39 +00:00
|
|
|
.target = { 't', CMD_FIND_PANE, 0 },
|
2015-12-14 00:31:54 +00:00
|
|
|
|
2016-10-14 22:14:22 +00:00
|
|
|
.flags = CMD_AFTERHOOK,
|
2015-12-13 21:53:57 +00:00
|
|
|
.exec = cmd_paste_buffer_exec
|
2009-06-01 22:58:49 +00:00
|
|
|
};
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_paste_buffer_exec(struct cmd *self, struct cmdq_item *item)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2020-04-13 08:26:27 +00:00
|
|
|
struct args *args = cmd_get_args(self);
|
2020-04-13 10:59:58 +00:00
|
|
|
struct cmd_find_state *target = cmdq_get_target(item);
|
|
|
|
struct window_pane *wp = target->wp;
|
2011-01-04 00:42:46 +00:00
|
|
|
struct paste_buffer *pb;
|
2015-08-29 09:36:46 +00:00
|
|
|
const char *sepstr, *bufname, *bufdata, *bufend, *line;
|
|
|
|
size_t seplen, bufsize;
|
|
|
|
int bracket = args_has(args, 'p');
|
2011-01-04 00:42:46 +00:00
|
|
|
|
2024-02-13 08:03:50 +00:00
|
|
|
if (window_pane_exited(wp)) {
|
|
|
|
cmdq_error(item, "target pane has exited");
|
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
|
|
|
|
2014-05-13 07:34:35 +00:00
|
|
|
bufname = NULL;
|
|
|
|
if (args_has(args, 'b'))
|
|
|
|
bufname = args_get(args, 'b');
|
2011-01-04 00:42:46 +00:00
|
|
|
|
2014-05-13 07:34:35 +00:00
|
|
|
if (bufname == NULL)
|
2015-08-29 09:25:00 +00:00
|
|
|
pb = paste_get_top(NULL);
|
2009-06-01 22:58:49 +00:00
|
|
|
else {
|
2014-05-13 07:34:35 +00:00
|
|
|
pb = paste_get_name(bufname);
|
2010-12-30 23:16:18 +00:00
|
|
|
if (pb == NULL) {
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_error(item, "no buffer %s", bufname);
|
2012-07-11 07:10:15 +00:00
|
|
|
return (CMD_RETURN_ERROR);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-29 09:36:46 +00:00
|
|
|
if (pb != NULL && ~wp->flags & PANE_INPUTOFF) {
|
2011-01-04 00:42:46 +00:00
|
|
|
sepstr = args_get(args, 's');
|
|
|
|
if (sepstr == NULL) {
|
|
|
|
if (args_has(args, 'r'))
|
|
|
|
sepstr = "\n";
|
|
|
|
else
|
|
|
|
sepstr = "\r";
|
|
|
|
}
|
2015-08-29 09:36:46 +00:00
|
|
|
seplen = strlen(sepstr);
|
|
|
|
|
|
|
|
if (bracket && (wp->screen->mode & MODE_BRACKETPASTE))
|
|
|
|
bufferevent_write(wp->event, "\033[200~", 6);
|
|
|
|
|
|
|
|
bufdata = paste_buffer_data(pb, &bufsize);
|
|
|
|
bufend = bufdata + bufsize;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
line = memchr(bufdata, '\n', bufend - bufdata);
|
|
|
|
if (line == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
bufferevent_write(wp->event, bufdata, line - bufdata);
|
|
|
|
bufferevent_write(wp->event, sepstr, seplen);
|
|
|
|
|
|
|
|
bufdata = line + 1;
|
|
|
|
}
|
|
|
|
if (bufdata != bufend)
|
|
|
|
bufferevent_write(wp->event, bufdata, bufend - bufdata);
|
|
|
|
|
|
|
|
if (bracket && (wp->screen->mode & MODE_BRACKETPASTE))
|
|
|
|
bufferevent_write(wp->event, "\033[201~", 6);
|
2011-01-04 00:42:46 +00:00
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2015-09-11 14:41:50 +00:00
|
|
|
if (pb != NULL && args_has(args, 'd'))
|
|
|
|
paste_free(pb);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2012-07-11 07:10:15 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|