mirror of
				https://github.com/tmux/tmux.git
				synced 2025-11-04 09:07:38 +00:00 
			
		
		
		
	Move editor stuff to common code in popup.c.
This commit is contained in:
		
							
								
								
									
										98
									
								
								popup.c
									
									
									
									
									
								
							
							
						
						
									
										98
									
								
								popup.c
									
									
									
									
									
								
							@@ -19,9 +19,11 @@
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <sys/wait.h>
 | 
			
		||||
 | 
			
		||||
#include <paths.h>
 | 
			
		||||
#include <signal.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
 | 
			
		||||
#include "tmux.h"
 | 
			
		||||
 | 
			
		||||
@@ -57,6 +59,12 @@ struct popup_data {
 | 
			
		||||
	u_int			  lb;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct popup_editor {
 | 
			
		||||
	char			*path;
 | 
			
		||||
	popup_finish_edit_cb	 cb;
 | 
			
		||||
	void			*arg;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
popup_redraw_cb(const struct tty_ctx *ttyctx)
 | 
			
		||||
{
 | 
			
		||||
@@ -519,3 +527,93 @@ popup_display(int flags, struct cmdq_item *item, u_int px, u_int py, u_int sx,
 | 
			
		||||
	    popup_draw_cb, popup_key_cb, popup_free_cb, pd);
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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 (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_WRITEKEYS|POPUP_CLOSEEXIT, NULL, px, py, sx, sy,
 | 
			
		||||
	    0, NULL, cmd, NULL, _PATH_TMP, c, NULL, popup_editor_close_cb,
 | 
			
		||||
	    pe) != 0) {
 | 
			
		||||
		popup_editor_free(pe);
 | 
			
		||||
		free(cmd);
 | 
			
		||||
		return (-1);
 | 
			
		||||
	}
 | 
			
		||||
	free(cmd);
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										3
									
								
								tmux.h
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								tmux.h
									
									
									
									
									
								
							@@ -2869,6 +2869,7 @@ int		 menu_display(struct menu *, int, struct cmdq_item *, u_int,
 | 
			
		||||
#define POPUP_CLOSEEXIT 0x2
 | 
			
		||||
#define POPUP_CLOSEEXITZERO 0x4
 | 
			
		||||
typedef void (*popup_close_cb)(int, void *);
 | 
			
		||||
typedef void (*popup_finish_edit_cb)(char *, size_t, void *);
 | 
			
		||||
u_int		 popup_width(struct cmdq_item *, u_int, const char **,
 | 
			
		||||
		    struct client *, struct cmd_find_state *);
 | 
			
		||||
u_int		 popup_height(u_int, const char **);
 | 
			
		||||
@@ -2876,6 +2877,8 @@ int		 popup_display(int, struct cmdq_item *, u_int, u_int, u_int,
 | 
			
		||||
		    u_int, u_int, const char **, const char *, const char *,
 | 
			
		||||
		    const char *, struct client *, struct cmd_find_state *,
 | 
			
		||||
		    popup_close_cb, void *);
 | 
			
		||||
int		 popup_editor(struct client *, const char *, size_t,
 | 
			
		||||
		    popup_finish_edit_cb, void *);
 | 
			
		||||
 | 
			
		||||
/* style.c */
 | 
			
		||||
int		 style_parse(struct style *,const struct grid_cell *,
 | 
			
		||||
 
 | 
			
		||||
@@ -18,8 +18,6 @@
 | 
			
		||||
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
 | 
			
		||||
#include <paths.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <time.h>
 | 
			
		||||
@@ -100,7 +98,6 @@ struct window_buffer_modedata {
 | 
			
		||||
 | 
			
		||||
struct window_buffer_editdata {
 | 
			
		||||
	u_int			 wp_id;
 | 
			
		||||
	char			*path;
 | 
			
		||||
	char			*name;
 | 
			
		||||
	struct paste_buffer	*pb;
 | 
			
		||||
};
 | 
			
		||||
@@ -366,19 +363,14 @@ window_buffer_do_paste(void *modedata, void *itemdata, struct client *c,
 | 
			
		||||
static void
 | 
			
		||||
window_buffer_finish_edit(struct window_buffer_editdata *ed)
 | 
			
		||||
{
 | 
			
		||||
	unlink(ed->path);
 | 
			
		||||
	free(ed->path);
 | 
			
		||||
	free(ed->name);
 | 
			
		||||
	free(ed);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
window_buffer_edit_close_cb(int status, void *arg)
 | 
			
		||||
window_buffer_edit_close_cb(char *buf, size_t len, void *arg)
 | 
			
		||||
{
 | 
			
		||||
	struct window_buffer_editdata	*ed = arg;
 | 
			
		||||
	FILE				*f;
 | 
			
		||||
	off_t				 len;
 | 
			
		||||
	char				*buf;
 | 
			
		||||
	size_t				 oldlen;
 | 
			
		||||
	const char			*oldbuf;
 | 
			
		||||
	struct paste_buffer		*pb;
 | 
			
		||||
@@ -386,7 +378,7 @@ window_buffer_edit_close_cb(int status, void *arg)
 | 
			
		||||
	struct window_buffer_modedata	*data;
 | 
			
		||||
	struct window_mode_entry	*wme;
 | 
			
		||||
 | 
			
		||||
	if (status != 0) {
 | 
			
		||||
	if (buf == NULL || len == 0) {
 | 
			
		||||
		window_buffer_finish_edit(ed);
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
@@ -397,16 +389,6 @@ window_buffer_edit_close_cb(int status, void *arg)
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	f = fopen(ed->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) {
 | 
			
		||||
	oldbuf = paste_buffer_data(pb, &oldlen);
 | 
			
		||||
	if (oldlen != '\0' &&
 | 
			
		||||
	    oldbuf[oldlen - 1] != '\n' &&
 | 
			
		||||
@@ -414,9 +396,6 @@ window_buffer_edit_close_cb(int status, void *arg)
 | 
			
		||||
		len--;
 | 
			
		||||
	if (len != 0)
 | 
			
		||||
		paste_replace(pb, buf, len);
 | 
			
		||||
		}
 | 
			
		||||
		fclose(f);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	wp = window_pane_find_by_id(ed->wp_id);
 | 
			
		||||
	if (wp != NULL) {
 | 
			
		||||
@@ -436,51 +415,21 @@ window_buffer_start_edit(struct window_buffer_modedata *data,
 | 
			
		||||
    struct window_buffer_itemdata *item, struct client *c)
 | 
			
		||||
{
 | 
			
		||||
	struct paste_buffer		*pb;
 | 
			
		||||
	int				 fd;
 | 
			
		||||
	FILE				*f;
 | 
			
		||||
	const char			*buf;
 | 
			
		||||
	size_t				 len;
 | 
			
		||||
	struct window_buffer_editdata	*ed;
 | 
			
		||||
	char				*cmd;
 | 
			
		||||
	char				 path[] = _PATH_TMP "tmux.XXXXXXXX";
 | 
			
		||||
	const char			*editor;
 | 
			
		||||
	u_int				 px, py, sx, sy;
 | 
			
		||||
 | 
			
		||||
	if ((pb = paste_get_name(item->name)) == NULL)
 | 
			
		||||
		return;
 | 
			
		||||
	buf = paste_buffer_data(pb, &len);
 | 
			
		||||
 | 
			
		||||
	editor = options_get_string(global_options, "editor");
 | 
			
		||||
	if (*editor == '\0')
 | 
			
		||||
		return;
 | 
			
		||||
 | 
			
		||||
	fd = mkstemp(path);
 | 
			
		||||
	if (fd == -1)
 | 
			
		||||
		return;
 | 
			
		||||
	f = fdopen(fd, "w");
 | 
			
		||||
	if (fwrite(buf, len, 1, f) != 1) {
 | 
			
		||||
		fclose(f);
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
	fclose(f);
 | 
			
		||||
 | 
			
		||||
	ed = xcalloc(1, sizeof *ed);
 | 
			
		||||
	ed->wp_id = data->wp->id;
 | 
			
		||||
	ed->path = xstrdup(path);
 | 
			
		||||
	ed->name = xstrdup(paste_buffer_name(pb));
 | 
			
		||||
	ed->pb = pb;
 | 
			
		||||
 | 
			
		||||
	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_WRITEKEYS|POPUP_CLOSEEXIT, NULL, px, py, sx, sy,
 | 
			
		||||
	    0, NULL, cmd, NULL, _PATH_TMP, c, NULL, window_buffer_edit_close_cb,
 | 
			
		||||
	    ed) != 0)
 | 
			
		||||
	if (popup_editor(c, buf, len, window_buffer_edit_close_cb, ed) != 0)
 | 
			
		||||
		window_buffer_finish_edit(ed);
 | 
			
		||||
	free(cmd);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user