xref: /openbsd-src/usr.bin/tmux/popup.c (revision 4e1ee0786f11cc571bd0be17d38e46f635c719fc)
1 /* $OpenBSD: popup.c,v 1.40 2021/10/20 09:52:27 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2020 Nicholas Marriott <nicholas.marriott@gmail.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 #include <sys/wait.h>
21 
22 #include <paths.h>
23 #include <signal.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 
28 #include "tmux.h"
29 
30 struct popup_data {
31 	struct client		 *c;
32 	struct cmdq_item	 *item;
33 	int			  flags;
34 	enum box_lines		  lines;
35 	char			 *title;
36 
37 	struct screen		  s;
38 	struct colour_palette	  palette;
39 	struct job		 *job;
40 	struct input_ctx	 *ictx;
41 	int			  status;
42 	popup_close_cb		  cb;
43 	void			 *arg;
44 
45 	struct menu		 *menu;
46 	struct menu_data	 *md;
47 	int			  close;
48 
49 	/* Current position and size. */
50 	u_int			  px;
51 	u_int			  py;
52 	u_int			  sx;
53 	u_int			  sy;
54 
55 	/* Preferred position and size. */
56 	u_int			  ppx;
57 	u_int			  ppy;
58 	u_int			  psx;
59 	u_int			  psy;
60 
61 	enum { OFF, MOVE, SIZE }  dragging;
62 	u_int			  dx;
63 	u_int			  dy;
64 
65 	u_int			  lx;
66 	u_int			  ly;
67 	u_int			  lb;
68 };
69 
70 struct popup_editor {
71 	char			*path;
72 	popup_finish_edit_cb	 cb;
73 	void			*arg;
74 };
75 
76 static const struct menu_item popup_menu_items[] = {
77 	{ "Close", 'q', NULL },
78 	{ "#{?buffer_name,Paste #[underscore]#{buffer_name},}", 'p', NULL },
79 	{ "", KEYC_NONE, NULL },
80 	{ "Fill Space", 'F', NULL },
81 	{ "Centre", 'C', NULL },
82 	{ "", KEYC_NONE, NULL },
83 	{ "To Horizontal Pane", 'h', NULL },
84 	{ "To Vertical Pane", 'v', NULL },
85 
86 	{ NULL, KEYC_NONE, NULL }
87 };
88 
89 static const struct menu_item popup_internal_menu_items[] = {
90 	{ "Close", 'q', NULL },
91 	{ "", KEYC_NONE, NULL },
92 	{ "Fill Space", 'F', NULL },
93 	{ "Centre", 'C', NULL },
94 
95 	{ NULL, KEYC_NONE, NULL }
96 };
97 
98 static void
99 popup_redraw_cb(const struct tty_ctx *ttyctx)
100 {
101 	struct popup_data	*pd = ttyctx->arg;
102 
103 	pd->c->flags |= CLIENT_REDRAWOVERLAY;
104 }
105 
106 static int
107 popup_set_client_cb(struct tty_ctx *ttyctx, struct client *c)
108 {
109 	struct popup_data	*pd = ttyctx->arg;
110 
111 	if (c != pd->c)
112 		return (0);
113 	if (pd->c->flags & CLIENT_REDRAWOVERLAY)
114 		return (0);
115 
116 	ttyctx->bigger = 0;
117 	ttyctx->wox = 0;
118 	ttyctx->woy = 0;
119 	ttyctx->wsx = c->tty.sx;
120 	ttyctx->wsy = c->tty.sy;
121 
122 	if (pd->lines == BOX_LINES_NONE) {
123 		ttyctx->xoff = ttyctx->rxoff = pd->px;
124 		ttyctx->yoff = ttyctx->ryoff = pd->py;
125 	} else {
126 		ttyctx->xoff = ttyctx->rxoff = pd->px + 1;
127 		ttyctx->yoff = ttyctx->ryoff = pd->py + 1;
128 	}
129 
130 	return (1);
131 }
132 
133 static void
134 popup_init_ctx_cb(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx)
135 {
136 	struct popup_data	*pd = ctx->arg;
137 
138 	ttyctx->palette = &pd->palette;
139 	ttyctx->redraw_cb = popup_redraw_cb;
140 	ttyctx->set_client_cb = popup_set_client_cb;
141 	ttyctx->arg = pd;
142 }
143 
144 static struct screen *
145 popup_mode_cb(__unused struct client *c, void *data, u_int *cx, u_int *cy)
146 {
147 	struct popup_data	*pd = data;
148 
149 	if (pd->md != NULL)
150 		return (menu_mode_cb(c, pd->md, cx, cy));
151 
152 	if (pd->lines == BOX_LINES_NONE) {
153 		*cx = pd->px + pd->s.cx;
154 		*cy = pd->py + pd->s.cy;
155 	} else {
156 		*cx = pd->px + 1 + pd->s.cx;
157 		*cy = pd->py + 1 + pd->s.cy;
158 	}
159 	return (&pd->s);
160 }
161 
162 /* Return parts of the input range which are not obstructed by the popup. */
163 static void
164 popup_check_cb(struct client* c, void *data, u_int px, u_int py, u_int nx,
165     struct overlay_ranges *r)
166 {
167 	struct popup_data	*pd = data;
168 	struct overlay_ranges	 or[2];
169 	u_int			 i, j, k = 0;
170 
171 	if (pd->md != NULL) {
172 		/* Check each returned range for the menu against the popup. */
173 		menu_check_cb(c, pd->md, px, py, nx, r);
174 		for (i = 0; i < 2; i++) {
175 			server_client_overlay_range(pd->px, pd->py, pd->sx,
176 			    pd->sy, r->px[i], py, r->nx[i], &or[i]);
177 		}
178 
179 		/*
180 		 * or has up to OVERLAY_MAX_RANGES non-overlapping ranges,
181 		 * ordered from left to right. Collect them in the output.
182 		 */
183 		for (i = 0; i < 2; i++) {
184 			/* Each or[i] only has 2 ranges. */
185 			for (j = 0; j < 2; j++) {
186 				if (or[i].nx[j] > 0) {
187 					r->px[k] = or[i].px[j];
188 					r->nx[k] = or[i].nx[j];
189 					k++;
190 				}
191 			}
192 		}
193 
194 		/* Zero remaining ranges if any. */
195 		for (i = k; i < OVERLAY_MAX_RANGES; i++) {
196 			r->px[i] = 0;
197 			r->nx[i] = 0;
198 		}
199 
200 		return;
201 	}
202 
203 	server_client_overlay_range(pd->px, pd->py, pd->sx, pd->sy, px, py, nx,
204 	    r);
205 }
206 
207 static void
208 popup_draw_cb(struct client *c, void *data, struct screen_redraw_ctx *rctx)
209 {
210 	struct popup_data	*pd = data;
211 	struct tty		*tty = &c->tty;
212 	struct screen		 s;
213 	struct screen_write_ctx	 ctx;
214 	u_int			 i, px = pd->px, py = pd->py;
215 	struct colour_palette	*palette = &pd->palette;
216 	struct grid_cell	 gc;
217 	struct grid_cell	 bgc;
218 	struct options          *o = c->session->curw->window->options;
219 
220 	screen_init(&s, pd->sx, pd->sy, 0);
221 	screen_write_start(&ctx, &s);
222 	screen_write_clearscreen(&ctx, 8);
223 
224 	memcpy(&bgc, &grid_default_cell, sizeof bgc);
225 	bgc.attr = 0;
226 	style_apply(&bgc, o, "popup-border-style", NULL);
227 	bgc.attr = 0;
228 
229 	if (pd->lines == BOX_LINES_NONE) {
230 		screen_write_cursormove(&ctx, 0, 0, 0);
231 		screen_write_fast_copy(&ctx, &pd->s, 0, 0, pd->sx, pd->sy);
232 	} else if (pd->sx > 2 && pd->sy > 2) {
233 		screen_write_box(&ctx, pd->sx, pd->sy, pd->lines, &bgc,
234 		    pd->title);
235 		screen_write_cursormove(&ctx, 1, 1, 0);
236 		screen_write_fast_copy(&ctx, &pd->s, 0, 0, pd->sx - 2,
237 		    pd->sy - 2);
238 	}
239 	screen_write_stop(&ctx);
240 
241 	memcpy(&gc, &grid_default_cell, sizeof gc);
242 	style_apply(&gc, o, "popup-style", NULL);
243 	gc.attr = 0;
244 	palette->fg = gc.fg;
245 	palette->bg = gc.bg;
246 
247 	if (pd->md != NULL) {
248 		c->overlay_check = menu_check_cb;
249 		c->overlay_data = pd->md;
250 	} else {
251 		c->overlay_check = NULL;
252 		c->overlay_data = NULL;
253 	}
254 	for (i = 0; i < pd->sy; i++)
255 		tty_draw_line(tty, &s, 0, i, pd->sx, px, py + i, &gc, palette);
256 	if (pd->md != NULL) {
257 		c->overlay_check = NULL;
258 		c->overlay_data = NULL;
259 		menu_draw_cb(c, pd->md, rctx);
260 	}
261 	c->overlay_check = popup_check_cb;
262 	c->overlay_data = pd;
263 }
264 
265 static void
266 popup_free_cb(struct client *c, void *data)
267 {
268 	struct popup_data	*pd = data;
269 	struct cmdq_item	*item = pd->item;
270 
271 	if (pd->md != NULL)
272 		menu_free_cb(c, pd->md);
273 
274 	if (pd->cb != NULL)
275 		pd->cb(pd->status, pd->arg);
276 
277 	if (item != NULL) {
278 		if (cmdq_get_client(item) != NULL &&
279 		    cmdq_get_client(item)->session == NULL)
280 			cmdq_get_client(item)->retval = pd->status;
281 		cmdq_continue(item);
282 	}
283 	server_client_unref(pd->c);
284 
285 	if (pd->job != NULL)
286 		job_free(pd->job);
287 	input_free(pd->ictx);
288 
289 	screen_free(&pd->s);
290 	colour_palette_free(&pd->palette);
291 
292 	free(pd->title);
293 	free(pd);
294 }
295 
296 static void
297 popup_resize_cb(__unused struct client *c, void *data)
298 {
299 	struct popup_data	*pd = data;
300 	struct tty		*tty = &c->tty;
301 
302 	if (pd == NULL)
303 		return;
304 	if (pd->md != NULL)
305 		menu_free_cb(c, pd->md);
306 
307 	/* Adjust position and size. */
308 	if (pd->psy > tty->sy)
309 		pd->sy = tty->sy;
310 	else
311 		pd->sy = pd->psy;
312 	if (pd->psx > tty->sx)
313 		pd->sx = tty->sx;
314 	else
315 		pd->sx = pd->psx;
316 	if (pd->ppy + pd->sy > tty->sy)
317 		pd->py = tty->sy - pd->sy;
318 	else
319 		pd->py = pd->ppy;
320 	if (pd->ppx + pd->sx > tty->sx)
321 		pd->px = tty->sx - pd->sx;
322 	else
323 		pd->px = pd->ppx;
324 
325 	/* Avoid zero size screens. */
326 	if (pd->lines == BOX_LINES_NONE) {
327 		screen_resize(&pd->s, pd->sx, pd->sy, 0);
328 		if (pd->job != NULL)
329 			job_resize(pd->job, pd->sx, pd->sy );
330 	} else if (pd->sx > 2 && pd->sy > 2) {
331 		screen_resize(&pd->s, pd->sx - 2, pd->sy - 2, 0);
332 		if (pd->job != NULL)
333 			job_resize(pd->job, pd->sx - 2, pd->sy - 2);
334 	}
335 }
336 
337 static void
338 popup_make_pane(struct popup_data *pd, enum layout_type type)
339 {
340 	struct client		*c = pd->c;
341 	struct session		*s = c->session;
342 	struct window		*w = s->curw->window;
343 	struct layout_cell	*lc;
344 	struct window_pane	*wp = w->active, *new_wp;
345 	u_int			 hlimit;
346 	const char		*shell;
347 
348 	window_unzoom(w);
349 
350 	lc = layout_split_pane(wp, type, -1, 0);
351 	hlimit = options_get_number(s->options, "history-limit");
352 	new_wp = window_add_pane(wp->window, NULL, hlimit, 0);
353 	layout_assign_pane(lc, new_wp, 0);
354 
355 	new_wp->fd = job_transfer(pd->job, &new_wp->pid, new_wp->tty,
356 	    sizeof new_wp->tty);
357 	pd->job = NULL;
358 
359 	screen_set_title(&pd->s, new_wp->base.title);
360 	screen_free(&new_wp->base);
361 	memcpy(&new_wp->base, &pd->s, sizeof wp->base);
362 	screen_resize(&new_wp->base, new_wp->sx, new_wp->sy, 1);
363 	screen_init(&pd->s, 1, 1, 0);
364 
365 	shell = options_get_string(s->options, "default-shell");
366 	if (!checkshell(shell))
367 		shell = _PATH_BSHELL;
368 	new_wp->shell = xstrdup(shell);
369 
370 	window_pane_set_event(new_wp);
371 	window_set_active_pane(w, new_wp, 1);
372 	new_wp->flags |= PANE_CHANGED;
373 
374 	pd->close = 1;
375 }
376 
377 static void
378 popup_menu_done(__unused struct menu *menu, __unused u_int choice,
379     key_code key, void *data)
380 {
381 	struct popup_data	*pd = data;
382 	struct client		*c = pd->c;
383 	struct paste_buffer	*pb;
384 	const char		*buf;
385 	size_t			 len;
386 
387 	pd->md = NULL;
388 	pd->menu = NULL;
389 	server_redraw_client(pd->c);
390 
391 	switch (key) {
392 	case 'p':
393 		pb = paste_get_top(NULL);
394 		if (pb != NULL) {
395 			buf = paste_buffer_data(pb, &len);
396 			bufferevent_write(job_get_event(pd->job), buf, len);
397 		}
398 		break;
399 	case 'F':
400 		pd->sx = c->tty.sx;
401 		pd->sy = c->tty.sy;
402 		pd->px = 0;
403 		pd->py = 0;
404 		server_redraw_client(c);
405 		break;
406 	case 'C':
407 		pd->px = c->tty.sx / 2 - pd->sx / 2;
408 		pd->py = c->tty.sy / 2 - pd->sy / 2;
409 		server_redraw_client(c);
410 		break;
411 	case 'h':
412 		popup_make_pane(pd, LAYOUT_LEFTRIGHT);
413 		break;
414 	case 'v':
415 		popup_make_pane(pd, LAYOUT_TOPBOTTOM);
416 		break;
417 	case 'q':
418 		pd->close = 1;
419 		break;
420 	}
421 }
422 
423 static void
424 popup_handle_drag(struct client *c, struct popup_data *pd,
425     struct mouse_event *m)
426 {
427 	u_int	px, py;
428 
429 	if (!MOUSE_DRAG(m->b))
430 		pd->dragging = OFF;
431 	else if (pd->dragging == MOVE) {
432 		if (m->x < pd->dx)
433 			px = 0;
434 		else if (m->x - pd->dx + pd->sx > c->tty.sx)
435 			px = c->tty.sx - pd->sx;
436 		else
437 			px = m->x - pd->dx;
438 		if (m->y < pd->dy)
439 			py = 0;
440 		else if (m->y - pd->dy + pd->sy > c->tty.sy)
441 			py = c->tty.sy - pd->sy;
442 		else
443 			py = m->y - pd->dy;
444 		pd->px = px;
445 		pd->py = py;
446 		pd->dx = m->x - pd->px;
447 		pd->dy = m->y - pd->py;
448 		pd->ppx = px;
449 		pd->ppy = py;
450 		server_redraw_client(c);
451 	} else if (pd->dragging == SIZE) {
452 		if (pd->lines == BOX_LINES_NONE) {
453 			if (m->x < pd->px + 1)
454 				return;
455 			if (m->y < pd->py + 1)
456 				return;
457 		} else {
458 			if (m->x < pd->px + 3)
459 				return;
460 			if (m->y < pd->py + 3)
461 				return;
462 		}
463 		pd->sx = m->x - pd->px;
464 		pd->sy = m->y - pd->py;
465 		pd->psx = pd->sx;
466 		pd->psy = pd->sy;
467 
468 		if (pd->lines == BOX_LINES_NONE) {
469 			screen_resize(&pd->s, pd->sx, pd->sy, 0);
470 			if (pd->job != NULL)
471 				job_resize(pd->job, pd->sx, pd->sy);
472 		} else {
473 			screen_resize(&pd->s, pd->sx - 2, pd->sy - 2, 0);
474 			if (pd->job != NULL)
475 				job_resize(pd->job, pd->sx - 2, pd->sy - 2);
476 		}
477 		server_redraw_client(c);
478 	}
479 }
480 
481 static int
482 popup_key_cb(struct client *c, void *data, struct key_event *event)
483 {
484 	struct popup_data	*pd = data;
485 	struct mouse_event	*m = &event->m;
486 	const char		*buf;
487 	size_t			 len;
488 	u_int			 px, py, x;
489 	enum { NONE, LEFT, RIGHT, TOP, BOTTOM } border = NONE;
490 
491 	if (pd->md != NULL) {
492 		if (menu_key_cb(c, pd->md, event) == 1) {
493 			pd->md = NULL;
494 			pd->menu = NULL;
495 			if (pd->close)
496 				server_client_clear_overlay(c);
497 			else
498 				server_redraw_client(c);
499 		}
500 		return (0);
501 	}
502 
503 	if (KEYC_IS_MOUSE(event->key)) {
504 		if (pd->dragging != OFF) {
505 			popup_handle_drag(c, pd, m);
506 			goto out;
507 		}
508 		if (m->x < pd->px ||
509 		    m->x > pd->px + pd->sx - 1 ||
510 		    m->y < pd->py ||
511 		    m->y > pd->py + pd->sy - 1) {
512 			if (MOUSE_BUTTONS(m->b) == 2)
513 				goto menu;
514 			return (0);
515 		}
516 		if (pd->lines != BOX_LINES_NONE) {
517 			if (m->x == pd->px)
518 				border = LEFT;
519 			else if (m->x == pd->px + pd->sx - 1)
520 				border = RIGHT;
521 			else if (m->y == pd->py)
522 				border = TOP;
523 			else if (m->y == pd->py + pd->sy - 1)
524 				border = BOTTOM;
525 		}
526 		if ((m->b & MOUSE_MASK_MODIFIERS) == 0 &&
527 		    MOUSE_BUTTONS(m->b) == 2 &&
528 		    (border == LEFT || border == TOP))
529 		    goto menu;
530 		if (((m->b & MOUSE_MASK_MODIFIERS) == MOUSE_MASK_META) ||
531 		    border != NONE) {
532 			if (!MOUSE_DRAG(m->b))
533 				goto out;
534 			if (MOUSE_BUTTONS(m->lb) == 0)
535 				pd->dragging = MOVE;
536 			else if (MOUSE_BUTTONS(m->lb) == 2)
537 				pd->dragging = SIZE;
538 			pd->dx = m->lx - pd->px;
539 			pd->dy = m->ly - pd->py;
540 			goto out;
541 		}
542 	}
543 	if ((((pd->flags & (POPUP_CLOSEEXIT|POPUP_CLOSEEXITZERO)) == 0) ||
544 	    pd->job == NULL) &&
545 	    (event->key == '\033' || event->key == '\003'))
546 		return (1);
547 	if (pd->job != NULL) {
548 		if (KEYC_IS_MOUSE(event->key)) {
549 			/* Must be inside, checked already. */
550 			if (pd->lines == BOX_LINES_NONE) {
551 				px = m->x - pd->px;
552 				py = m->y - pd->py;
553 			} else {
554 				px = m->x - pd->px - 1;
555 				py = m->y - pd->py - 1;
556 			}
557 			if (!input_key_get_mouse(&pd->s, m, px, py, &buf, &len))
558 				return (0);
559 			bufferevent_write(job_get_event(pd->job), buf, len);
560 			return (0);
561 		}
562 		input_key(&pd->s, job_get_event(pd->job), event->key);
563 	}
564 	return (0);
565 
566 menu:
567 	pd->menu = menu_create("");
568 	if (pd->flags & POPUP_INTERNAL) {
569 		menu_add_items(pd->menu, popup_internal_menu_items, NULL, NULL,
570 		    NULL);
571 	} else
572 		menu_add_items(pd->menu, popup_menu_items, NULL, NULL, NULL);
573 	if (m->x >= (pd->menu->width + 4) / 2)
574 		x = m->x - (pd->menu->width + 4) / 2;
575 	else
576 		x = 0;
577 	pd->md = menu_prepare(pd->menu, 0, NULL, x, m->y, c, NULL,
578 	    popup_menu_done, pd);
579 	c->flags |= CLIENT_REDRAWOVERLAY;
580 
581 out:
582 	pd->lx = m->x;
583 	pd->ly = m->y;
584 	pd->lb = m->b;
585 	return (0);
586 }
587 
588 static void
589 popup_job_update_cb(struct job *job)
590 {
591 	struct popup_data	*pd = job_get_data(job);
592 	struct evbuffer		*evb = job_get_event(job)->input;
593 	struct client		*c = pd->c;
594 	struct screen		*s = &pd->s;
595 	void			*data = EVBUFFER_DATA(evb);
596 	size_t			 size = EVBUFFER_LENGTH(evb);
597 
598 	if (size == 0)
599 		return;
600 
601 	if (pd->md != NULL) {
602 		c->overlay_check = menu_check_cb;
603 		c->overlay_data = pd->md;
604 	} else {
605 		c->overlay_check = NULL;
606 		c->overlay_data = NULL;
607 	}
608 	input_parse_screen(pd->ictx, s, popup_init_ctx_cb, pd, data, size);
609 	c->overlay_check = popup_check_cb;
610 	c->overlay_data = pd;
611 
612 	evbuffer_drain(evb, size);
613 }
614 
615 static void
616 popup_job_complete_cb(struct job *job)
617 {
618 	struct popup_data	*pd = job_get_data(job);
619 	int			 status;
620 
621 	status = job_get_status(pd->job);
622 	if (WIFEXITED(status))
623 		pd->status = WEXITSTATUS(status);
624 	else if (WIFSIGNALED(status))
625 		pd->status = WTERMSIG(status);
626 	else
627 		pd->status = 0;
628 	pd->job = NULL;
629 
630 	if ((pd->flags & POPUP_CLOSEEXIT) ||
631 	    ((pd->flags & POPUP_CLOSEEXITZERO) && pd->status == 0))
632 		server_client_clear_overlay(pd->c);
633 }
634 
635 int
636 popup_display(int flags, enum box_lines lines, struct cmdq_item *item, u_int px,
637     u_int py, u_int sx, u_int sy, struct environ *env, const char *shellcmd,
638     int argc, char **argv, const char *cwd, const char *title, struct client *c,
639     struct session *s, popup_close_cb cb, void *arg)
640 {
641 	struct popup_data	*pd;
642 	u_int			 jx, jy;
643 	struct options		*o;
644 
645 	if (lines == BOX_LINES_DEFAULT) {
646 		if (s != NULL)
647 			o = s->curw->window->options;
648 		else
649 			o = c->session->curw->window->options;
650 		lines = options_get_number(o, "popup-border-lines");
651 	}
652 	if (lines == BOX_LINES_NONE) {
653 		if (sx < 1 || sy < 1)
654 			return (-1);
655 		jx = sx;
656 		jy = sy;
657 	} else {
658 		if (sx < 3 || sy < 3)
659 			return (-1);
660 		jx = sx - 2;
661 		jy = sy - 2;
662 	}
663 	if (c->tty.sx < sx || c->tty.sy < sy)
664 		return (-1);
665 
666 	pd = xcalloc(1, sizeof *pd);
667 	pd->item = item;
668 	pd->flags = flags;
669 	pd->lines = lines;
670 	pd->title = xstrdup(title);
671 
672 	pd->c = c;
673 	pd->c->references++;
674 
675 	pd->cb = cb;
676 	pd->arg = arg;
677 	pd->status = 128 + SIGHUP;
678 
679 	screen_init(&pd->s, sx - 2, sy - 2, 0);
680 	colour_palette_init(&pd->palette);
681 	colour_palette_from_option(&pd->palette, global_w_options);
682 
683 	pd->px = px;
684 	pd->py = py;
685 	pd->sx = sx;
686 	pd->sy = sy;
687 
688 	pd->ppx = px;
689 	pd->ppy = py;
690 	pd->psx = sx;
691 	pd->psy = sy;
692 
693 	pd->job = job_run(shellcmd, argc, argv, env, s, cwd,
694 	    popup_job_update_cb, popup_job_complete_cb, NULL, pd,
695 	    JOB_NOWAIT|JOB_PTY|JOB_KEEPWRITE, jx, jy);
696 	pd->ictx = input_init(NULL, job_get_event(pd->job), &pd->palette);
697 
698 	server_client_set_overlay(c, 0, popup_check_cb, popup_mode_cb,
699 	    popup_draw_cb, popup_key_cb, popup_free_cb, popup_resize_cb, pd);
700 	return (0);
701 }
702 
703 static void
704 popup_editor_free(struct popup_editor *pe)
705 {
706 	unlink(pe->path);
707 	free(pe->path);
708 	free(pe);
709 }
710 
711 static void
712 popup_editor_close_cb(int status, void *arg)
713 {
714 	struct popup_editor	*pe = arg;
715 	FILE			*f;
716 	char			*buf = NULL;
717 	off_t			 len = 0;
718 
719 	if (status != 0) {
720 		pe->cb(NULL, 0, pe->arg);
721 		popup_editor_free(pe);
722 		return;
723 	}
724 
725 	f = fopen(pe->path, "r");
726 	if (f != NULL) {
727 		fseeko(f, 0, SEEK_END);
728 		len = ftello(f);
729 		fseeko(f, 0, SEEK_SET);
730 
731 		if (len == 0 ||
732 		    (uintmax_t)len > (uintmax_t)SIZE_MAX ||
733 		    (buf = malloc(len)) == NULL ||
734 		    fread(buf, len, 1, f) != 1) {
735 			free(buf);
736 			buf = NULL;
737 			len = 0;
738 		}
739 		fclose(f);
740 	}
741 	pe->cb(buf, len, pe->arg); /* callback now owns buffer */
742 	popup_editor_free(pe);
743 }
744 
745 int
746 popup_editor(struct client *c, const char *buf, size_t len,
747     popup_finish_edit_cb cb, void *arg)
748 {
749 	struct popup_editor	*pe;
750 	int			 fd;
751 	FILE			*f;
752 	char			*cmd;
753 	char			 path[] = _PATH_TMP "tmux.XXXXXXXX";
754 	const char		*editor;
755 	u_int			 px, py, sx, sy;
756 
757 	editor = options_get_string(global_options, "editor");
758 	if (*editor == '\0')
759 		return (-1);
760 
761 	fd = mkstemp(path);
762 	if (fd == -1)
763 		return (-1);
764 	f = fdopen(fd, "w");
765 	if (fwrite(buf, len, 1, f) != 1) {
766 		fclose(f);
767 		return (-1);
768 	}
769 	fclose(f);
770 
771 	pe = xcalloc(1, sizeof *pe);
772 	pe->path = xstrdup(path);
773 	pe->cb = cb;
774 	pe->arg = arg;
775 
776 	sx = c->tty.sx * 9 / 10;
777 	sy = c->tty.sy * 9 / 10;
778 	px = (c->tty.sx / 2) - (sx / 2);
779 	py = (c->tty.sy / 2) - (sy / 2);
780 
781 	xasprintf(&cmd, "%s %s", editor, path);
782 	if (popup_display(POPUP_INTERNAL|POPUP_CLOSEEXIT, BOX_LINES_DEFAULT,
783 	    NULL, px, py, sx, sy, NULL, cmd, 0, NULL, _PATH_TMP, NULL, c, NULL,
784 	    popup_editor_close_cb, pe) != 0) {
785 		popup_editor_free(pe);
786 		free(cmd);
787 		return (-1);
788 	}
789 	free(cmd);
790 	return (0);
791 }
792