diff --git a/popup.c b/popup.c index 229a1d16..91e2f68e 100644 --- a/popup.c +++ b/popup.c @@ -76,12 +76,6 @@ struct popup_data { u_int lb; }; -struct popup_editor { - char *path; - popup_finish_edit_cb cb; - void *arg; -}; - static const struct menu_item popup_menu_items[] = { { "Close", 'q', NULL }, { "#{?buffer_name,Paste #[underscore]#{buffer_name},}", 'p', NULL }, @@ -95,15 +89,6 @@ static const struct menu_item popup_menu_items[] = { { NULL, KEYC_NONE, NULL } }; -static const struct menu_item popup_internal_menu_items[] = { - { "Close", 'q', NULL }, - { "", KEYC_NONE, NULL }, - { "Fill Space", 'F', NULL }, - { "Centre", 'C', NULL }, - - { NULL, KEYC_NONE, NULL } -}; - static void popup_free(struct popup_data *pd) { @@ -651,11 +636,7 @@ popup_key_cb(struct client *c, void *data, struct key_event *event) menu: pd->menu = menu_create(""); - if (pd->flags & POPUP_INTERNAL) { - menu_add_items(pd->menu, popup_internal_menu_items, NULL, c, - NULL); - } else - menu_add_items(pd->menu, popup_menu_items, NULL, c, NULL); + menu_add_items(pd->menu, popup_menu_items, NULL, c, NULL); if (m->x >= (pd->menu->width + 4) / 2) x = m->x - (pd->menu->width + 4) / 2; else @@ -862,127 +843,16 @@ popup_display(int flags, enum box_lines lines, struct cmdq_item *item, u_int px, pd->psx = sx; pd->psy = sy; - if (flags & POPUP_NOJOB) - pd->ictx = input_init(NULL, NULL, &pd->palette, NULL); - else { - pd->job = job_run(shellcmd, argc, argv, env, s, cwd, - popup_job_update_cb, popup_job_complete_cb, NULL, pd, - JOB_NOWAIT|JOB_PTY|JOB_KEEPWRITE|JOB_DEFAULTSHELL, jx, jy); - if (pd->job == NULL) { - popup_free(pd); - return (-1); - } - pd->ictx = input_init(NULL, job_get_event(pd->job), - &pd->palette, c); + pd->job = job_run(shellcmd, argc, argv, env, s, cwd, + popup_job_update_cb, popup_job_complete_cb, NULL, pd, + JOB_NOWAIT|JOB_PTY|JOB_KEEPWRITE|JOB_DEFAULTSHELL, jx, jy); + if (pd->job == NULL) { + popup_free(pd); + return (-1); } + pd->ictx = input_init(NULL, job_get_event(pd->job), &pd->palette, c); server_client_set_overlay(c, 0, popup_check_cb, popup_mode_cb, popup_draw_cb, popup_key_cb, popup_free_cb, popup_resize_cb, pd); return (0); } - -void -popup_write(struct client *c, const char *data, size_t size) -{ - struct popup_data *pd = c->overlay_data; - - if (!popup_present(c)) - return; - c->overlay_check = NULL; - c->overlay_data = NULL; - input_parse_screen(pd->ictx, &pd->s, popup_init_ctx_cb, pd, data, size); - c->overlay_check = popup_check_cb; - c->overlay_data = pd; -} - -static void -popup_editor_free(struct popup_editor *pe) -{ - unlink(pe->path); - free(pe->path); - free(pe); -} - -static void -popup_editor_close_cb(int status, void *arg) -{ - struct popup_editor *pe = arg; - FILE *f; - char *buf = NULL; - off_t len = 0; - - if (status != 0) { - pe->cb(NULL, 0, pe->arg); - popup_editor_free(pe); - return; - } - - f = fopen(pe->path, "r"); - if (f != NULL) { - fseeko(f, 0, SEEK_END); - len = ftello(f); - fseeko(f, 0, SEEK_SET); - - if (len == 0 || - (uintmax_t)len > (uintmax_t)SIZE_MAX || - (buf = malloc(len)) == NULL || - fread(buf, len, 1, f) != 1) { - free(buf); - buf = NULL; - len = 0; - } - fclose(f); - } - pe->cb(buf, len, pe->arg); /* callback now owns buffer */ - popup_editor_free(pe); -} - -int -popup_editor(struct client *c, const char *buf, size_t len, - popup_finish_edit_cb cb, void *arg) -{ - struct popup_editor *pe; - int fd; - FILE *f; - char *cmd; - char path[] = _PATH_TMP "tmux.XXXXXXXX"; - const char *editor; - u_int px, py, sx, sy; - - editor = options_get_string(global_options, "editor"); - if (*editor == '\0') - return (-1); - - fd = mkstemp(path); - if (fd == -1) - return (-1); - f = fdopen(fd, "w"); - if (f == NULL) - return (-1); - if (fwrite(buf, len, 1, f) != 1) { - fclose(f); - return (-1); - } - fclose(f); - - pe = xcalloc(1, sizeof *pe); - pe->path = xstrdup(path); - pe->cb = cb; - pe->arg = arg; - - sx = c->tty.sx * 9 / 10; - sy = c->tty.sy * 9 / 10; - px = (c->tty.sx / 2) - (sx / 2); - py = (c->tty.sy / 2) - (sy / 2); - - xasprintf(&cmd, "%s %s", editor, path); - if (popup_display(POPUP_INTERNAL|POPUP_CLOSEEXIT, BOX_LINES_DEFAULT, - NULL, px, py, sx, sy, NULL, cmd, 0, NULL, _PATH_TMP, NULL, c, NULL, - NULL, NULL, popup_editor_close_cb, pe) != 0) { - popup_editor_free(pe); - free(cmd); - return (-1); - } - free(cmd); - return (0); -} diff --git a/tmux.h b/tmux.h index 7e7db3d1..f361d4b2 100644 --- a/tmux.h +++ b/tmux.h @@ -1275,6 +1275,7 @@ struct window_pane { int status; struct timeval dead_time; struct cmdq_item *wait_item; /* new-pane -W: waiting for pane exit */ + struct spawn_editor_state *editor; int fd; struct bufferevent *event; @@ -3790,19 +3791,13 @@ int menu_key_cb(struct client *, void *, struct key_event *); /* popup.c */ #define POPUP_CLOSEEXIT 0x1 #define POPUP_CLOSEEXITZERO 0x2 -#define POPUP_INTERNAL 0x4 -#define POPUP_CLOSEANYKEY 0x8 -#define POPUP_NOJOB 0x10 +#define POPUP_CLOSEANYKEY 0x4 typedef void (*popup_close_cb)(int, void *); -typedef void (*popup_finish_edit_cb)(char *, size_t, void *); int popup_display(int, enum box_lines, struct cmdq_item *, u_int, u_int, u_int, u_int, struct environ *, const char *, int, char **, const char *, const char *, struct client *, struct session *, const char *, const char *, popup_close_cb, void *); -void popup_write(struct client *, const char *, size_t); -int popup_editor(struct client *, const char *, size_t, - popup_finish_edit_cb, void *); int popup_present(struct client *); int popup_modify(struct client *, const char *, const char *, const char *, enum box_lines, int); @@ -3826,6 +3821,12 @@ struct style_range *style_ranges_get_range(struct style_ranges *, u_int); /* spawn.c */ struct winlink *spawn_window(struct spawn_context *, char **); struct window_pane *spawn_pane(struct spawn_context *, char **); +typedef void (*spawn_finish_edit_cb)(char *, size_t, void *); +struct spawn_editor_state *spawn_editor(struct client *, const char *, size_t, + spawn_finish_edit_cb, void *); +void spawn_cancel_editor(struct spawn_editor_state *); +pid_t spawn_get_editor_pid(struct spawn_editor_state *); +void spawn_editor_finish(struct window_pane *); /* regsub.c */ char *regsub(const char *, const char *, const char *, int);