Add a -w flag to set- and load-buffer to send to clipboard using OSC 52.

GitHub issue 2363.
This commit is contained in:
nicm 2020-09-02 13:46:35 +00:00
parent 60860aced8
commit 37b1600d9c
5 changed files with 57 additions and 15 deletions

View File

@ -37,14 +37,15 @@ const struct cmd_entry cmd_load_buffer_entry = {
.name = "load-buffer", .name = "load-buffer",
.alias = "loadb", .alias = "loadb",
.args = { "b:", 1, 1 }, .args = { "b:t:w", 1, 1 },
.usage = CMD_BUFFER_USAGE " path", .usage = CMD_BUFFER_USAGE " " CMD_TARGET_CLIENT_USAGE " path",
.flags = CMD_AFTERHOOK, .flags = CMD_AFTERHOOK|CMD_CLIENT_TFLAG|CMD_CLIENT_CANFAIL,
.exec = cmd_load_buffer_exec .exec = cmd_load_buffer_exec
}; };
struct cmd_load_buffer_data { struct cmd_load_buffer_data {
struct client *client;
struct cmdq_item *item; struct cmdq_item *item;
char *name; char *name;
}; };
@ -54,6 +55,7 @@ cmd_load_buffer_done(__unused struct client *c, const char *path, int error,
int closed, struct evbuffer *buffer, void *data) int closed, struct evbuffer *buffer, void *data)
{ {
struct cmd_load_buffer_data *cdata = data; struct cmd_load_buffer_data *cdata = data;
struct client *tc = cdata->client;
struct cmdq_item *item = cdata->item; struct cmdq_item *item = cdata->item;
void *bdata = EVBUFFER_DATA(buffer); void *bdata = EVBUFFER_DATA(buffer);
size_t bsize = EVBUFFER_LENGTH(buffer); size_t bsize = EVBUFFER_LENGTH(buffer);
@ -72,7 +74,12 @@ cmd_load_buffer_done(__unused struct client *c, const char *path, int error,
cmdq_error(item, "%s", cause); cmdq_error(item, "%s", cause);
free(cause); free(cause);
free(copy); free(copy);
} } else if (tc != NULL &&
tc->session != NULL &&
(~tc->flags & CLIENT_DEAD))
tty_set_selection(&tc->tty, copy, bsize);
if (tc != NULL)
server_client_unref(tc);
} }
cmdq_continue(item); cmdq_continue(item);
@ -84,6 +91,7 @@ static enum cmd_retval
cmd_load_buffer_exec(struct cmd *self, struct cmdq_item *item) cmd_load_buffer_exec(struct cmd *self, struct cmdq_item *item)
{ {
struct args *args = cmd_get_args(self); struct args *args = cmd_get_args(self);
struct client *tc = cmdq_get_target_client(item);
struct cmd_load_buffer_data *cdata; struct cmd_load_buffer_data *cdata;
const char *bufname = args_get(args, 'b'); const char *bufname = args_get(args, 'b');
char *path; char *path;
@ -94,6 +102,10 @@ cmd_load_buffer_exec(struct cmd *self, struct cmdq_item *item)
cdata->name = xstrdup(bufname); cdata->name = xstrdup(bufname);
else else
cdata->name = NULL; cdata->name = NULL;
if (args_has(args, 'w') && tc != NULL) {
cdata->client = tc;
cdata->client->references++;
}
path = format_single_from_target(item, args->argv[0]); path = format_single_from_target(item, args->argv[0]);
file_read(cmdq_get_client(item), path, cmd_load_buffer_done, cdata); file_read(cmdq_get_client(item), path, cmd_load_buffer_done, cdata);

View File

@ -33,10 +33,11 @@ const struct cmd_entry cmd_set_buffer_entry = {
.name = "set-buffer", .name = "set-buffer",
.alias = "setb", .alias = "setb",
.args = { "ab:n:", 0, 1 }, .args = { "ab:t:n:w", 0, 1 },
.usage = "[-a] " CMD_BUFFER_USAGE " [-n new-buffer-name] data", .usage = "[-aw] " CMD_BUFFER_USAGE " [-n new-buffer-name] "
CMD_TARGET_CLIENT_USAGE " data",
.flags = CMD_AFTERHOOK, .flags = CMD_AFTERHOOK|CMD_CLIENT_TFLAG|CMD_CLIENT_CANFAIL,
.exec = cmd_set_buffer_exec .exec = cmd_set_buffer_exec
}; };
@ -55,6 +56,7 @@ static enum cmd_retval
cmd_set_buffer_exec(struct cmd *self, struct cmdq_item *item) cmd_set_buffer_exec(struct cmd *self, struct cmdq_item *item)
{ {
struct args *args = cmd_get_args(self); struct args *args = cmd_get_args(self);
struct client *tc = cmdq_get_target_client(item);
struct paste_buffer *pb; struct paste_buffer *pb;
char *bufdata, *cause; char *bufdata, *cause;
const char *bufname, *olddata; const char *bufname, *olddata;
@ -118,6 +120,8 @@ cmd_set_buffer_exec(struct cmd *self, struct cmdq_item *item)
free(cause); free(cause);
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
} }
if (args_has(args, 'w') && tc != NULL)
tty_set_selection(&tc->tty, bufdata, bufsize);
return (CMD_RETURN_NORMAL); return (CMD_RETURN_NORMAL);
} }

19
tmux.1
View File

@ -5651,12 +5651,21 @@ See the
.Sx FORMATS .Sx FORMATS
section. section.
.It Xo Ic load-buffer .It Xo Ic load-buffer
.Op Fl w
.Op Fl b Ar buffer-name .Op Fl b Ar buffer-name
.Op Fl t Ar target-client
.Ar path .Ar path
.Xc .Xc
.D1 (alias: Ic loadb ) .D1 (alias: Ic loadb )
Load the contents of the specified paste buffer from Load the contents of the specified paste buffer from
.Ar path . .Ar path .
If
.Fl w
is given, the buffer is also sent to the clipboard for
.Ar target-client
using the
.Xr xterm 1
escape sequence, if possible.
.It Xo Ic paste-buffer .It Xo Ic paste-buffer
.Op Fl dpr .Op Fl dpr
.Op Fl b Ar buffer-name .Op Fl b Ar buffer-name
@ -5693,14 +5702,22 @@ The
.Fl a .Fl a
option appends to rather than overwriting the file. option appends to rather than overwriting the file.
.It Xo Ic set-buffer .It Xo Ic set-buffer
.Op Fl a .Op Fl aw
.Op Fl b Ar buffer-name .Op Fl b Ar buffer-name
.Op Fl t Ar target-client
.Op Fl n Ar new-buffer-name .Op Fl n Ar new-buffer-name
.Ar data .Ar data
.Xc .Xc
.D1 (alias: Ic setb ) .D1 (alias: Ic setb )
Set the contents of the specified buffer to Set the contents of the specified buffer to
.Ar data . .Ar data .
If
.Fl w
is given, the buffer is also sent to the clipboard for
.Ar target-client
using the
.Xr xterm 1
escape sequence, if possible.
The The
.Fl a .Fl a
option appends to rather than overwriting the buffer. option appends to rather than overwriting the buffer.

1
tmux.h
View File

@ -2126,6 +2126,7 @@ int tty_open(struct tty *, char **);
void tty_close(struct tty *); void tty_close(struct tty *);
void tty_free(struct tty *); void tty_free(struct tty *);
void tty_update_features(struct tty *); void tty_update_features(struct tty *);
void tty_set_selection(struct tty *, const char *, size_t);
void tty_write(void (*)(struct tty *, const struct tty_ctx *), void tty_write(void (*)(struct tty *, const struct tty_ctx *),
struct tty_ctx *); struct tty_ctx *);
void tty_cmd_alignmenttest(struct tty *, const struct tty_ctx *); void tty_cmd_alignmenttest(struct tty *, const struct tty_ctx *);

22
tty.c
View File

@ -1896,19 +1896,27 @@ tty_cmd_cells(struct tty *tty, const struct tty_ctx *ctx)
void void
tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx) tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx)
{ {
char *buf; tty_set_selection(tty, ctx->ptr, ctx->num);
size_t off; }
void
tty_set_selection(struct tty *tty, const char *buf, size_t len)
{
char *encoded;
size_t size;
if (!tty_term_has(tty->term, TTYC_MS)) if (!tty_term_has(tty->term, TTYC_MS))
return; return;
if (~tty->flags & TTY_STARTED)
return;
off = 4 * ((ctx->num + 2) / 3) + 1; /* storage for base64 */ size = 4 * ((len + 2) / 3) + 1; /* storage for base64 */
buf = xmalloc(off); encoded = xmalloc(size);
b64_ntop(ctx->ptr, ctx->num, buf, off); b64_ntop(buf, len, encoded, size);
tty_putcode_ptr2(tty, TTYC_MS, "", buf); tty_putcode_ptr2(tty, TTYC_MS, "", encoded);
free(buf); free(encoded);
} }
void void