Skip to content

Commit 57f3314

Browse files
nicmnicm
nicm
authored andcommitted
Add argument to refresh-client -l to forward clipboard to a pane. GitHub
issue 3068.
1 parent 8aed444 commit 57f3314

File tree

7 files changed

+195
-118
lines changed

7 files changed

+195
-118
lines changed

cmd-refresh-client.c

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const struct cmd_entry cmd_refresh_client_entry = {
3434
.name = "refresh-client",
3535
.alias = "refresh",
3636

37-
.args = { "A:B:cC:Df:F:lLRSt:U", 0, 1, NULL },
37+
.args = { "A:B:cC:Df:F:l::LRSt:U", 0, 1, NULL },
3838
.usage = "[-cDlLRSU] [-A pane:state] [-B name:what:format] "
3939
"[-C XxY] [-f flags] " CMD_TARGET_CLIENT_USAGE " [adjustment]",
4040

@@ -162,6 +162,37 @@ cmd_refresh_client_update_offset(struct client *tc, const char *value)
162162
free(copy);
163163
}
164164

165+
static enum cmd_retval
166+
cmd_refresh_client_clipboard(struct cmd *self, struct cmdq_item *item)
167+
{
168+
struct args *args = cmd_get_args(self);
169+
struct client *tc = cmdq_get_target_client(item);
170+
const char *p;
171+
u_int i;
172+
struct cmd_find_state fs;
173+
174+
p = args_get(args, 'l');
175+
if (p == NULL) {
176+
if (tc->flags & CLIENT_CLIPBOARDBUFFER)
177+
return (CMD_RETURN_NORMAL);
178+
tc->flags |= CLIENT_CLIPBOARDBUFFER;
179+
} else {
180+
if (cmd_find_target(&fs, item, p, CMD_FIND_PANE, 0) != 0)
181+
return (CMD_RETURN_ERROR);
182+
for (i = 0; i < tc->clipboard_npanes; i++) {
183+
if (tc->clipboard_panes[i] == fs.wp->id)
184+
break;
185+
}
186+
if (i != tc->clipboard_npanes)
187+
return (CMD_RETURN_NORMAL);
188+
tc->clipboard_panes = xreallocarray (tc->clipboard_panes,
189+
tc->clipboard_npanes + 1, sizeof *tc->clipboard_panes);
190+
tc->clipboard_panes[tc->clipboard_npanes++] = fs.wp->id;
191+
}
192+
tty_clipboard_query(&tc->tty);
193+
return (CMD_RETURN_NORMAL);
194+
}
195+
165196
static enum cmd_retval
166197
cmd_refresh_client_exec(struct cmd *self, struct cmdq_item *item)
167198
{
@@ -224,10 +255,8 @@ cmd_refresh_client_exec(struct cmd *self, struct cmdq_item *item)
224255
return (CMD_RETURN_NORMAL);
225256
}
226257

227-
if (args_has(args, 'l')) {
228-
tty_send_osc52_query(&tc->tty);
229-
return (CMD_RETURN_NORMAL);
230-
}
258+
if (args_has(args, 'l'))
259+
return (cmd_refresh_client_clipboard(self, item));
231260

232261
if (args_has(args, 'F')) /* -F is an alias for -f */
233262
server_client_set_flags(tc, args_get(args, 'F'));

input.c

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2682,8 +2682,8 @@ input_osc_52(struct input_ctx *ictx, const char *p)
26822682
{
26832683
struct window_pane *wp = ictx->wp;
26842684
char *end;
2685-
const char *buf;
2686-
size_t len;
2685+
const char *buf = NULL;
2686+
size_t len = 0;
26872687
u_char *out;
26882688
int outlen, state;
26892689
struct screen_write_ctx ctx;
@@ -2703,26 +2703,12 @@ input_osc_52(struct input_ctx *ictx, const char *p)
27032703
log_debug("%s: %s", __func__, end);
27042704

27052705
if (strcmp(end, "?") == 0) {
2706-
if ((pb = paste_get_top(NULL)) != NULL) {
2706+
if ((pb = paste_get_top(NULL)) != NULL)
27072707
buf = paste_buffer_data(pb, &len);
2708-
outlen = 4 * ((len + 2) / 3) + 1;
2709-
out = xmalloc(outlen);
2710-
if ((outlen = b64_ntop(buf, len, out, outlen)) == -1) {
2711-
free(out);
2712-
return;
2713-
}
2714-
} else {
2715-
outlen = 0;
2716-
out = NULL;
2717-
}
2718-
bufferevent_write(ictx->event, "\033]52;;", 6);
2719-
if (outlen != 0)
2720-
bufferevent_write(ictx->event, out, outlen);
27212708
if (ictx->input_end == INPUT_END_BEL)
2722-
bufferevent_write(ictx->event, "\007", 1);
2709+
input_reply_clipboard(ictx->event, buf, len, "\007");
27232710
else
2724-
bufferevent_write(ictx->event, "\033\\", 2);
2725-
free(out);
2711+
input_reply_clipboard(ictx->event, buf, len, "\033\\");
27262712
return;
27272713
}
27282714

@@ -2780,3 +2766,26 @@ input_osc_104(struct input_ctx *ictx, const char *p)
27802766
screen_write_fullredraw(&ictx->ctx);
27812767
free(copy);
27822768
}
2769+
2770+
void
2771+
input_reply_clipboard(struct bufferevent *bev, const char *buf, size_t len,
2772+
const char *end)
2773+
{
2774+
char *out = NULL;
2775+
size_t outlen = 0;
2776+
2777+
if (buf != NULL && len != 0) {
2778+
outlen = 4 * ((len + 2) / 3) + 1;
2779+
out = xmalloc(outlen);
2780+
if ((outlen = b64_ntop(buf, len, out, outlen)) == -1) {
2781+
free(out);
2782+
return;
2783+
}
2784+
}
2785+
2786+
bufferevent_write(bev, "\033]52;;", 6);
2787+
if (outlen != 0)
2788+
bufferevent_write(bev, out, outlen);
2789+
bufferevent_write(bev, end, strlen(end));
2790+
free(out);
2791+
}

server-client.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ server_client_lost(struct client *c)
422422
if (c->flags & CLIENT_TERMINAL)
423423
tty_free(&c->tty);
424424
free(c->ttyname);
425+
free(c->clipboard_panes);
425426

426427
free(c->term_name);
427428
free(c->term_type);

tmux.1

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,11 +1341,12 @@ and sets an environment variable for the newly created session; it may be
13411341
specified multiple times.
13421342
.Tg refresh
13431343
.It Xo Ic refresh-client
1344-
.Op Fl cDlLRSU
1344+
.Op Fl cDLRSU
13451345
.Op Fl A Ar pane:state
13461346
.Op Fl B Ar name:what:format
13471347
.Op Fl C Ar size
13481348
.Op Fl f Ar flags
1349+
.Op Fl l Op Ar target-pane
13491350
.Op Fl t Ar target-client
13501351
.Op Ar adjustment
13511352
.Xc
@@ -1459,7 +1460,11 @@ sets a comma-separated list of client flags, see
14591460
.Fl l
14601461
requests the clipboard from the client using the
14611462
.Xr xterm 1
1462-
escape sequence and stores it in a new paste buffer.
1463+
escape sequence.
1464+
If
1465+
Ar target-pane
1466+
is given, the clipboard is sent (in encoded form), otherwise it is stored in a
1467+
new paste buffer.
14631468
.Pp
14641469
.Fl L ,
14651470
.Fl R ,
@@ -5057,6 +5062,8 @@ The following variables are available, where appropriate:
50575062
.It Li "client_termname" Ta "" Ta "Terminal name of client"
50585063
.It Li "client_termtype" Ta "" Ta "Terminal type of client, if available"
50595064
.It Li "client_tty" Ta "" Ta "Pseudo terminal of client"
5065+
.It Li "client_uid" Ta "" Ta "UID of client process"
5066+
.It Li "client_user" Ta "" Ta "User of client process"
50605067
.It Li "client_utf8" Ta "" Ta "1 if client supports UTF-8"
50615068
.It Li "client_width" Ta "" Ta "Width of client"
50625069
.It Li "client_written" Ta "" Ta "Bytes written to client"
@@ -5174,6 +5181,8 @@ The following variables are available, where appropriate:
51745181
.It Li "session_windows" Ta "" Ta "Number of windows in session"
51755182
.It Li "socket_path" Ta "" Ta "Server socket path"
51765183
.It Li "start_time" Ta "" Ta "Server start time"
5184+
.It Li "uid" Ta "" Ta "Server UID"
5185+
.It Li "user" Ta "" Ta "Server user"
51775186
.It Li "version" Ta "" Ta "Server version"
51785187
.It Li "window_active" Ta "" Ta "1 if window active"
51795188
.It Li "window_active_clients" Ta "" Ta "Number of clients viewing this window"
@@ -5188,7 +5197,6 @@ The following variables are available, where appropriate:
51885197
.It Li "window_cell_width" Ta "" Ta "Width of each cell in pixels"
51895198
.It Li "window_end_flag" Ta "" Ta "1 if window has the highest index"
51905199
.It Li "window_flags" Ta "#F" Ta "Window flags with # escaped as ##"
5191-
.It Li "window_raw_flags" Ta "" Ta "Window flags with nothing escaped"
51925200
.It Li "window_format" Ta "" Ta "1 if format is for a window"
51935201
.It Li "window_height" Ta "" Ta "Height of window"
51945202
.It Li "window_id" Ta "" Ta "Unique window ID"
@@ -5203,6 +5211,7 @@ The following variables are available, where appropriate:
52035211
.It Li "window_offset_x" Ta "" Ta "X offset into window if larger than client"
52045212
.It Li "window_offset_y" Ta "" Ta "Y offset into window if larger than client"
52055213
.It Li "window_panes" Ta "" Ta "Number of panes in window"
5214+
.It Li "window_raw_flags" Ta "" Ta "Window flags with nothing escaped"
52065215
.It Li "window_silence_flag" Ta "" Ta "1 if window has silence alert"
52075216
.It Li "window_stack_index" Ta "" Ta "Index in session most recent stack"
52085217
.It Li "window_start_flag" Ta "" Ta "1 if window has the lowest index"

0 commit comments

Comments
 (0)