2014-11-08 12:27:43 +00:00
|
|
|
/* $OpenBSD$ */
|
2007-11-23 17:52:54 +00:00
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2007-11-23 17:52:54 +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-06-06 00:01:36 +00:00
|
|
|
#include <stdlib.h>
|
2007-11-23 17:52:54 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#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 *);
|
2007-11-23 17:52:54 +00:00
|
|
|
|
|
|
|
const struct cmd_entry cmd_paste_buffer_entry = {
|
2015-12-13 21:53:57 +00:00
|
|
|
.name = "paste-buffer",
|
|
|
|
.alias = "pasteb",
|
|
|
|
|
|
|
|
.args = { "db:prs:t:", 0, 0 },
|
|
|
|
.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
|
2007-11-23 17:52:54 +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)
|
2007-11-23 17:52:54 +00:00
|
|
|
{
|
2020-04-13 08:26:27 +00:00
|
|
|
struct args *args = cmd_get_args(self);
|
2017-04-22 10:22:39 +00:00
|
|
|
struct window_pane *wp = item->target.wp;
|
2011-01-07 14:45:34 +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-07 14:45:34 +00:00
|
|
|
|
2014-05-13 07:34:35 +00:00
|
|
|
bufname = NULL;
|
|
|
|
if (args_has(args, 'b'))
|
|
|
|
bufname = args_get(args, 'b');
|
2011-01-07 14:45:34 +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);
|
2008-06-20 17:31:48 +00:00
|
|
|
else {
|
2014-05-13 07:34:35 +00:00
|
|
|
pb = paste_get_name(bufname);
|
2010-12-30 22:39:49 +00:00
|
|
|
if (pb == NULL) {
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_error(item, "no buffer %s", bufname);
|
2012-07-11 19:37:32 +00:00
|
|
|
return (CMD_RETURN_ERROR);
|
2009-01-19 18:23:40 +00:00
|
|
|
}
|
2008-06-20 17:31:48 +00:00
|
|
|
}
|
2008-06-20 18:58:13 +00:00
|
|
|
|
2015-08-29 09:36:46 +00:00
|
|
|
if (pb != NULL && ~wp->flags & PANE_INPUTOFF) {
|
2011-01-07 14:45:34 +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-07 14:45:34 +00:00
|
|
|
}
|
2008-06-20 17:31:48 +00:00
|
|
|
|
2015-09-11 14:41:50 +00:00
|
|
|
if (pb != NULL && args_has(args, 'd'))
|
|
|
|
paste_free(pb);
|
2007-11-23 17:52:54 +00:00
|
|
|
|
2012-07-11 19:37:32 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
2007-11-23 17:52:54 +00:00
|
|
|
}
|