2009-06-01 22:58:49 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2013-03-24 09:54:10 +00:00
|
|
|
enum cmd_retval cmd_paste_buffer_exec(struct cmd *, struct cmd_q *);
|
2011-01-04 00:42:46 +00:00
|
|
|
|
2012-03-03 09:43:22 +00:00
|
|
|
void cmd_paste_buffer_filter(struct window_pane *,
|
2012-11-27 20:22:12 +00:00
|
|
|
const char *, size_t, const char *, int);
|
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",
|
|
|
|
|
|
|
|
.args = { "db:prs:t:", 0, 0 },
|
|
|
|
.usage = "[-dpr] [-s separator] " CMD_BUFFER_USAGE " "
|
|
|
|
CMD_TARGET_PANE_USAGE,
|
|
|
|
|
2015-12-14 00:31:54 +00:00
|
|
|
.tflag = CMD_PANE,
|
|
|
|
|
|
|
|
.flags = 0,
|
2015-12-13 21:53:57 +00:00
|
|
|
.exec = cmd_paste_buffer_exec
|
2009-06-01 22:58:49 +00:00
|
|
|
};
|
|
|
|
|
2012-07-11 07:10:15 +00:00
|
|
|
enum cmd_retval
|
2013-03-24 09:54:10 +00:00
|
|
|
cmd_paste_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2011-01-04 00:42:46 +00:00
|
|
|
struct args *args = self->args;
|
2015-12-13 14:32:38 +00:00
|
|
|
struct window_pane *wp = cmdq->state.tflag.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
|
|
|
|
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) {
|
2014-05-13 07:34:35 +00:00
|
|
|
cmdq_error(cmdq, "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
|
|
|
}
|