From 541b936bcfcb48f14d0a2f6de09a7990e0ffc3d0 Mon Sep 17 00:00:00 2001
From: Luuk van Baal <luukvbaal@gmail.com>
Date: Wed, 29 Sep 2021 11:33:19 +0200
Subject: [PATCH] Fix gitstatus and rebase patches

---
 patches/gitstatus/mainline.diff      | 62 +++++++++----------
 patches/gitstatus/namefirst.diff     | 64 ++++++++++----------
 patches/namefirst/mainline.diff      | 26 ++++----
 patches/restorepreview/mainline.diff | 89 +++++++++++++---------------
 4 files changed, 120 insertions(+), 121 deletions(-)

diff --git a/patches/gitstatus/mainline.diff b/patches/gitstatus/mainline.diff
index 64d39f9f..0c9eb6bb 100644
--- a/patches/gitstatus/mainline.diff
+++ b/patches/gitstatus/mainline.diff
@@ -6,10 +6,10 @@
 # Authors: Luuk van Baal, @crides
 
 diff --git a/src/nnn.c b/src/nnn.c
-index fe5d650..059c7bf 100644
+index 88263beb..597ff2b3 100644
 --- a/src/nnn.c
 +++ b/src/nnn.c
-@@ -277,6 +277,7 @@ typedef struct entry {
+@@ -285,6 +285,7 @@ typedef struct entry {
  	uid_t uid; /* 4 bytes */
  	gid_t gid; /* 4 bytes */
  #endif
@@ -17,7 +17,7 @@ index fe5d650..059c7bf 100644
  } *pEntry;
 
  /* Selection marker */
-@@ -333,6 +334,7 @@ typedef struct {
+@@ -341,6 +342,7 @@ typedef struct {
  	uint_t cliopener  : 1;  /* All-CLI app opener */
  	uint_t waitedit   : 1;  /* For ops that can't be detached, used EDITOR */
  	uint_t rollover   : 1;  /* Roll over at edges */
@@ -25,7 +25,7 @@ index fe5d650..059c7bf 100644
  } settings;
 
  /* Non-persistent program-internal states (alphabeical order) */
-@@ -382,7 +384,17 @@ typedef struct {
+@@ -390,7 +392,17 @@ typedef struct {
  } session_header_t;
  #endif
 
@@ -43,7 +43,7 @@ index fe5d650..059c7bf 100644
 
  /* Configuration, contexts */
  static settings cfg = {
-@@ -413,6 +425,7 @@ static settings cfg = {
+@@ -421,6 +433,7 @@ static settings cfg = {
  	0, /* cliopener */
  	0, /* waitedit */
  	1, /* rollover */
@@ -51,20 +51,19 @@ index fe5d650..059c7bf 100644
  };
 
  static context g_ctx[CTX_MAX] __attribute__ ((aligned));
-@@ -3781,6 +3794,39 @@ static char *get_kv_val(kv *kvarr, char *buf, int key, uchar_t max, uchar_t id)
- 	return NULL;
+@@ -3814,6 +3827,38 @@ static int get_kv_key(kv *kvarr, char *val, uchar_t max, uchar_t id)
+ 	return -1;
  }
 
 +static size_t get_git_statuses(const char *path)
 +{
 +	static char gitrev[] = "git rev-parse --show-toplevel 2>/dev/null";
-+	char workdir[PATH_MAX];
++	char workdir[PATH_MAX], *ret;
 +	FILE *fp = popen(gitrev, "r");
 +
-+	fgets(workdir, PATH_MAX, fp);
++	ret = fgets(workdir, PATH_MAX, fp);
 +	pclose(fp);
-+
-+	if (!workdir[0])
++	if (!ret)
 +		return 0;
 +
 +	static char gitstat[] = "git -c core.quotePath= status --porcelain --ignored=matching -u ";
@@ -91,7 +90,7 @@ index fe5d650..059c7bf 100644
  static void resetdircolor(int flags)
  {
  	/* Directories are always shown on top, clear the color when moving to first file */
-@@ -4118,6 +4164,10 @@ static void printent(const struct entry *ent, uint_t namecols, bool sel)
+@@ -4151,6 +4196,10 @@ static void printent(const struct entry *ent, uint_t namecols, bool sel)
 
  	uchar_t color_pair = get_color_pair_name_ind(ent, &ind, &attrs);
 
@@ -102,36 +101,39 @@ index fe5d650..059c7bf 100644
  	addch((ent->flags & FILE_SELECTED) ? '+' | A_REVERSE | A_BOLD : ' ');
 
  	if (g_state.oldcolor)
-@@ -5451,6 +5501,10 @@ static int dentfill(char *path, struct entry **ppdents)
+@@ -5548,6 +5597,11 @@ static int dentfill(char *path, struct entry **ppdents)
  		attron(COLOR_PAIR(cfg.curctx + 1));
  	}
 
 +	char linkpath[PATH_MAX];
 +	if ((git_statuses.len = get_git_statuses(path)))
-+		realpath(path, linkpath);
++		if (!realpath(path, linkpath))
++			printwarn(NULL);
 +
  #if _POSIX_C_SOURCE >= 200112L
  	posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
  #endif
-@@ -5649,6 +5703,34 @@ static int dentfill(char *path, struct entry **ppdents)
+@@ -5746,6 +5800,36 @@ static int dentfill(char *path, struct entry **ppdents)
  #endif
  		}
 
 +		if (git_statuses.len) {
-+			char dentpath[PATH_MAX];
-+			size_t pathlen = mkpath(linkpath, dentp->name, dentpath) - 1;
++			char dentpath[PATH_MAX], prefix[PATH_MAX + 1];
++			size_t pathlen = mkpath(linkpath, dentp->name, dentpath);
++			snprintf(prefix, PATH_MAX + 1, "%s/", dentpath);
 +			dentp->git_status[0] = dentp->git_status[1] = '-';
 +
 +			if (dentp->flags & DIR_OR_DIRLNK) {
 +				for (size_t i = 0; i < git_statuses.len; ++i)
-+					if (is_prefix(git_statuses.statuses[i].path, dentpath, pathlen)) {
-+						dentp->git_status[0] = git_statuses.statuses[i].status[0];
-+						dentp->git_status[1] = git_statuses.statuses[i].status[1];
-+						if (dentp->git_status[1] != '!') {
++					if (is_prefix(git_statuses.statuses[i].path, prefix, pathlen)) {
++						if ((dentp->git_status[0] == '-') && (git_statuses.statuses[i].status[0] != '-'))
++							dentp->git_status[0] = git_statuses.statuses[i].status[0];
++						if ((dentp->git_status[1] == '-') && (git_statuses.statuses[i].status[1] != '-'))
++							dentp->git_status[1] = git_statuses.statuses[i].status[1];
++						if (git_statuses.statuses[i].status[1] != '!')
 +							git_statuses.show = TRUE;
-+							if (dentp->git_status[1] == '?')
-+								break;
-+						}
++						if ((dentp->git_status[0] != '-') && (dentp->git_status[1] != '-'))
++							break;
 +					}
 +			} else {
 +				for (size_t i = 0; i < git_statuses.len; ++i)
@@ -148,7 +150,7 @@ index fe5d650..059c7bf 100644
  		++ndents;
  	} while ((dp = readdir(dirp)));
 
-@@ -6160,11 +6242,12 @@ static int adjust_cols(int n)
+@@ -6270,11 +6354,12 @@ static int adjust_cols(int n)
  #endif
  	if (cfg.showdetail) {
  		/* Fallback to light mode if less than 35 columns */
@@ -164,7 +166,7 @@ index fe5d650..059c7bf 100644
 
  	/* 2 columns for preceding space and indicator */
  	return (n - 2);
-@@ -7913,6 +7996,7 @@ static void usage(void)
+@@ -8034,6 +8119,7 @@ static void usage(void)
  		" -F val  fifo mode [0:preview 1:explore]\n"
  #endif
  		" -g      regex filters\n"
@@ -172,15 +174,15 @@ index fe5d650..059c7bf 100644
  		" -H      show hidden files\n"
  		" -J      no auto-proceed on select\n"
  		" -K      detect key collision\n"
-@@ -8051,6 +8135,7 @@ static void cleanup(void)
- 		fflush(stdout);
+@@ -8174,6 +8260,7 @@ static void cleanup(void)
+ 		free(hostname);
  	}
  #endif
 +	free(git_statuses.statuses);
  	free(selpath);
  	free(plgpath);
  	free(cfgpath);
-@@ -8095,7 +8180,7 @@ int main(int argc, char *argv[])
+@@ -8218,7 +8305,7 @@ int main(int argc, char *argv[])
 
  	while ((opt = (env_opts_id > 0
  		       ? env_opts[--env_opts_id]
@@ -189,7 +191,7 @@ index fe5d650..059c7bf 100644
  		switch (opt) {
  #ifndef NOFIFO
  		case 'a':
-@@ -8146,6 +8231,9 @@ int main(int argc, char *argv[])
+@@ -8269,6 +8356,9 @@ int main(int argc, char *argv[])
  			cfg.regex = 1;
  			filterfn = &visible_re;
  			break;
diff --git a/patches/gitstatus/namefirst.diff b/patches/gitstatus/namefirst.diff
index fe962d94..c2b13576 100644
--- a/patches/gitstatus/namefirst.diff
+++ b/patches/gitstatus/namefirst.diff
@@ -7,10 +7,10 @@
 # Authors: Luuk van Baal, @crides
 
 diff --git a/src/nnn.c b/src/nnn.c
-index 2d33716..b190177 100644
+index 55f32e73..ed771826 100644
 --- a/src/nnn.c
 +++ b/src/nnn.c
-@@ -277,6 +277,7 @@ typedef struct entry {
+@@ -285,6 +285,7 @@ typedef struct entry {
  	uid_t uid; /* 4 bytes */
  	gid_t gid; /* 4 bytes */
  #endif
@@ -18,7 +18,7 @@ index 2d33716..b190177 100644
  } *pEntry;
 
  /* Selection marker */
-@@ -333,6 +334,7 @@ typedef struct {
+@@ -341,6 +342,7 @@ typedef struct {
  	uint_t cliopener  : 1;  /* All-CLI app opener */
  	uint_t waitedit   : 1;  /* For ops that can't be detached, used EDITOR */
  	uint_t rollover   : 1;  /* Roll over at edges */
@@ -26,7 +26,7 @@ index 2d33716..b190177 100644
  } settings;
 
  /* Non-persistent program-internal states (alphabeical order) */
-@@ -386,7 +388,17 @@ static struct {
+@@ -394,7 +396,17 @@ static struct {
  	ushort_t maxnameln, maxsizeln, maxuidln, maxgidln, maxentln, uidln, gidln, printguid;
  } dtls;
 
@@ -44,7 +44,7 @@ index 2d33716..b190177 100644
 
  /* Configuration, contexts */
  static settings cfg = {
-@@ -417,6 +429,7 @@ static settings cfg = {
+@@ -425,6 +437,7 @@ static settings cfg = {
  	0, /* cliopener */
  	0, /* waitedit */
  	1, /* rollover */
@@ -52,20 +52,19 @@ index 2d33716..b190177 100644
  };
 
  static context g_ctx[CTX_MAX] __attribute__ ((aligned));
-@@ -3789,6 +3802,39 @@ static char *get_kv_val(kv *kvarr, char *buf, int key, uchar_t max, uchar_t id)
- 	return NULL;
+@@ -3822,6 +3835,38 @@ static int get_kv_key(kv *kvarr, char *val, uchar_t max, uchar_t id)
+ 	return -1;
  }
 
 +static size_t get_git_statuses(const char *path)
 +{
 +	static char gitrev[] = "git rev-parse --show-toplevel 2>/dev/null";
-+	char workdir[PATH_MAX];
++	char workdir[PATH_MAX], *ret;
 +	FILE *fp = popen(gitrev, "r");
 +
-+	fgets(workdir, PATH_MAX, fp);
++	ret = fgets(workdir, PATH_MAX, fp);
 +	pclose(fp);
-+
-+	if (!workdir[0])
++	if (!ret)
 +		return 0;
 +
 +	static char gitstat[] = "git -c core.quotePath= status --porcelain --ignored=matching -u ";
@@ -92,7 +91,7 @@ index 2d33716..b190177 100644
  static void resetdircolor(int flags)
  {
  	/* Directories are always shown on top, clear the color when moving to first file */
-@@ -4099,6 +4145,9 @@ static void printent(const struct entry *ent, uint_t namecols, bool sel)
+@@ -4132,6 +4177,9 @@ static void printent(const struct entry *ent, uint_t namecols, bool sel)
  	int attrs = 0, namelen;
  	uchar_t color_pair = get_color_pair_name_ind(ent, &ind, &attrs);
 
@@ -102,36 +101,39 @@ index 2d33716..b190177 100644
  	addch((ent->flags & FILE_SELECTED) ? '+' | A_REVERSE | A_BOLD : ' ');
 
  	if (g_state.oldcolor)
-@@ -5457,6 +5506,10 @@ static int dentfill(char *path, struct entry **ppdents)
+@@ -5554,6 +5602,11 @@ static int dentfill(char *path, struct entry **ppdents)
  		attron(COLOR_PAIR(cfg.curctx + 1));
  	}
 
 +	char linkpath[PATH_MAX];
 +	if ((git_statuses.len = get_git_statuses(path)))
-+		realpath(path, linkpath);
++		if (!realpath(path, linkpath))
++			printwarn(NULL);
 +
  #if _POSIX_C_SOURCE >= 200112L
  	posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
  #endif
-@@ -5655,6 +5708,34 @@ static int dentfill(char *path, struct entry **ppdents)
+@@ -5752,6 +5805,36 @@ static int dentfill(char *path, struct entry **ppdents)
  #endif
  		}
 
 +		if (git_statuses.len) {
-+			char dentpath[PATH_MAX];
-+			size_t pathlen = mkpath(linkpath, dentp->name, dentpath) - 1;
++			char dentpath[PATH_MAX], prefix[PATH_MAX + 1];
++			size_t pathlen = mkpath(linkpath, dentp->name, dentpath);
++			snprintf(prefix, PATH_MAX + 1, "%s/", dentpath);
 +			dentp->git_status[0] = dentp->git_status[1] = '-';
 +
 +			if (dentp->flags & DIR_OR_DIRLNK) {
 +				for (size_t i = 0; i < git_statuses.len; ++i)
-+					if (is_prefix(git_statuses.statuses[i].path, dentpath, pathlen)) {
-+						dentp->git_status[0] = git_statuses.statuses[i].status[0];
-+						dentp->git_status[1] = git_statuses.statuses[i].status[1];
-+						if (dentp->git_status[1] != '!') {
++					if (is_prefix(git_statuses.statuses[i].path, prefix, pathlen)) {
++						if ((dentp->git_status[0] == '-') && (git_statuses.statuses[i].status[0] != '-'))
++							dentp->git_status[0] = git_statuses.statuses[i].status[0];
++						if ((dentp->git_status[1] == '-') && (git_statuses.statuses[i].status[1] != '-'))
++							dentp->git_status[1] = git_statuses.statuses[i].status[1];
++						if (git_statuses.statuses[i].status[1] != '!')
 +							git_statuses.show = TRUE;
-+							if (dentp->git_status[1] == '?')
-+								break;
-+						}
++						if ((dentp->git_status[0] != '-') && (dentp->git_status[1] != '-'))
++							break;
 +					}
 +			} else {
 +				for (size_t i = 0; i < git_statuses.len; ++i)
@@ -148,7 +150,7 @@ index 2d33716..b190177 100644
  		++ndents;
  	} while ((dp = readdir(dirp)));
 
-@@ -6157,7 +6238,8 @@ static int adjust_cols(int n)
+@@ -6267,7 +6350,8 @@ static int adjust_cols(int n)
  			cfg.showdetail ^= 1;
  		else /* 2 more accounted for below */
  			n -= (dtls.maxentln - 2 - dtls.maxnameln);
@@ -158,7 +160,7 @@ index 2d33716..b190177 100644
 
  	/* 2 columns for preceding space and indicator */
  	return (n - 2);
-@@ -6312,7 +6394,7 @@ static void redraw(char *path)
+@@ -6422,7 +6506,7 @@ static void redraw(char *path)
  			}
  #endif
  		}
@@ -167,7 +169,7 @@ index 2d33716..b190177 100644
  	}
 
  	ncols = adjust_cols(ncols);
-@@ -7919,6 +8001,7 @@ static void usage(void)
+@@ -8040,6 +8124,7 @@ static void usage(void)
  		" -F val  fifo mode [0:preview 1:explore]\n"
  #endif
  		" -g      regex filters\n"
@@ -175,15 +177,15 @@ index 2d33716..b190177 100644
  		" -H      show hidden files\n"
  		" -J      no auto-proceed on select\n"
  		" -K      detect key collision\n"
-@@ -8057,6 +8140,7 @@ static void cleanup(void)
- 		fflush(stdout);
+@@ -8180,6 +8265,7 @@ static void cleanup(void)
+ 		free(hostname);
  	}
  #endif
 +	free(git_statuses.statuses);
  	free(selpath);
  	free(plgpath);
  	free(cfgpath);
-@@ -8101,7 +8185,7 @@ int main(int argc, char *argv[])
+@@ -8224,7 +8310,7 @@ int main(int argc, char *argv[])
 
  	while ((opt = (env_opts_id > 0
  		       ? env_opts[--env_opts_id]
@@ -192,7 +194,7 @@ index 2d33716..b190177 100644
  		switch (opt) {
  #ifndef NOFIFO
  		case 'a':
-@@ -8152,6 +8236,9 @@ int main(int argc, char *argv[])
+@@ -8275,6 +8361,9 @@ int main(int argc, char *argv[])
  			cfg.regex = 1;
  			filterfn = &visible_re;
  			break;
diff --git a/patches/namefirst/mainline.diff b/patches/namefirst/mainline.diff
index 17f472c2..e8b0614e 100644
--- a/patches/namefirst/mainline.diff
+++ b/patches/namefirst/mainline.diff
@@ -4,10 +4,10 @@
 # Author: Luuk van Baal
 
 diff --git a/src/nnn.c b/src/nnn.c
-index fe5d650..2d33716 100644
+index 88263beb..55f32e73 100644
 --- a/src/nnn.c
 +++ b/src/nnn.c
-@@ -382,6 +382,10 @@ typedef struct {
+@@ -390,6 +390,10 @@ typedef struct {
  } session_header_t;
  #endif
 
@@ -18,7 +18,7 @@ index fe5d650..2d33716 100644
  /* GLOBALS */
 
  /* Configuration, contexts */
-@@ -1065,10 +1069,12 @@ static char *getpwname(uid_t uid)
+@@ -1083,10 +1087,12 @@ static char *getpwname(uid_t uid)
  	static char *namecache;
 
  	if (uidcache != uid) {
@@ -31,7 +31,7 @@ index fe5d650..2d33716 100644
  	}
 
  	return namecache ? namecache : xitoa(uid);
-@@ -1080,10 +1086,12 @@ static char *getgrname(gid_t gid)
+@@ -1098,10 +1104,12 @@ static char *getgrname(gid_t gid)
  	static char *grpcache;
 
  	if (gidcache != gid) {
@@ -44,7 +44,7 @@ index fe5d650..2d33716 100644
  	}
 
  	return grpcache ? grpcache : xitoa(gid);
-@@ -3796,14 +3804,13 @@ static void resetdircolor(int flags)
+@@ -3829,14 +3837,13 @@ static void resetdircolor(int flags)
   * Max supported str length: NAME_MAX;
   */
  #ifdef NOLC
@@ -62,7 +62,7 @@ index fe5d650..2d33716 100644
  {
  	wchar_t * const wbuf = (wchar_t *)g_buf;
  	wchar_t *buf = wbuf;
-@@ -3828,7 +3835,7 @@ static wchar_t *unescape(const char *str, uint_t maxcols)
+@@ -3861,7 +3868,7 @@ static wchar_t *unescape(const char *str, uint_t maxcols)
  		++buf;
  	}
 
@@ -71,7 +71,7 @@ index fe5d650..2d33716 100644
  }
 
  static off_t get_size(off_t size, off_t *pval, int comp)
-@@ -4089,33 +4096,7 @@ static uchar_t get_color_pair_name_ind(const struct entry *ent, char *pind, int
+@@ -4122,33 +4129,7 @@ static uchar_t get_color_pair_name_ind(const struct entry *ent, char *pind, int
  static void printent(const struct entry *ent, uint_t namecols, bool sel)
  {
  	char ind = '\0';
@@ -106,7 +106,7 @@ index fe5d650..2d33716 100644
  	uchar_t color_pair = get_color_pair_name_ind(ent, &ind, &attrs);
 
  	addch((ent->flags & FILE_SELECTED) ? '+' | A_REVERSE | A_BOLD : ' ');
-@@ -4140,15 +4121,40 @@ static void printent(const struct entry *ent, uint_t namecols, bool sel)
+@@ -4173,15 +4154,40 @@ static void printent(const struct entry *ent, uint_t namecols, bool sel)
  		++namecols;
 
  #ifndef NOLC
@@ -150,7 +150,7 @@ index fe5d650..2d33716 100644
  }
 
  static void savecurctx(char *path, char *curname, int nextctx)
-@@ -6140,18 +6146,6 @@ static void statusbar(char *path)
+@@ -6250,18 +6256,6 @@ static void statusbar(char *path)
  		tocursor();
  }
 
@@ -169,7 +169,7 @@ index fe5d650..2d33716 100644
  static int adjust_cols(int n)
  {
  	/* Calculate the number of cols available to print entry name */
-@@ -6159,11 +6153,10 @@ static int adjust_cols(int n)
+@@ -6269,11 +6263,10 @@ static int adjust_cols(int n)
  	n -= (g_state.oldcolor ? 0 : 1 + xstrlen(ICON_PADDING_LEFT) + xstrlen(ICON_PADDING_RIGHT));
  #endif
  	if (cfg.showdetail) {
@@ -183,7 +183,7 @@ index fe5d650..2d33716 100644
  	}
 
  	/* 2 columns for preceding space and indicator */
-@@ -6200,8 +6193,6 @@ static void draw_line(int ncols)
+@@ -6310,8 +6303,6 @@ static void draw_line(int ncols)
  	/* Must reset e.g. no files in dir */
  	if (dir)
  		attroff(COLOR_PAIR(cfg.curctx + 1) | A_BOLD);
@@ -192,7 +192,7 @@ index fe5d650..2d33716 100644
  }
 
  static void redraw(char *path)
-@@ -6309,6 +6300,21 @@ static void redraw(char *path)
+@@ -6419,6 +6410,21 @@ static void redraw(char *path)
 
  	onscreen = MIN(onscreen + curscroll, ndents);
 
@@ -214,7 +214,7 @@ index fe5d650..2d33716 100644
  	ncols = adjust_cols(ncols);
 
  	int len = scanselforpath(path, FALSE);
-@@ -6339,7 +6345,7 @@ static void redraw(char *path)
+@@ -6449,7 +6455,7 @@ static void redraw(char *path)
  #endif
  	}
 
diff --git a/patches/restorepreview/mainline.diff b/patches/restorepreview/mainline.diff
index 0e35e6f2..65ed9890 100644
--- a/patches/restorepreview/mainline.diff
+++ b/patches/restorepreview/mainline.diff
@@ -4,7 +4,7 @@
 # Authors: Luuk van Baal
 
 diff --git a/src/nnn.c b/src/nnn.c
-index 44f297f4..f15b3bc1 100644
+index 88263beb..8aa24712 100644
 --- a/src/nnn.c
 +++ b/src/nnn.c
 @@ -367,7 +367,8 @@ typedef struct {
@@ -15,19 +15,19 @@ index 44f297f4..f15b3bc1 100644
 +	uint_t previewer  : 1;  /* Run state of previewer */
 +	uint_t reserved   : 6;  /* Adjust when adding/removing a field */
  } runstate;
- 
+
  /* Contexts or workspaces */
-@@ -513,6 +514,9 @@ static char g_tmpfpath[TMP_LEN_MAX] __attribute__ ((aligned));
+@@ -515,6 +516,9 @@ static char g_tmpfpath[TMP_LEN_MAX] __attribute__ ((aligned));
  /* Buffer to store plugins control pipe location */
  static char g_pipepath[TMP_LEN_MAX] __attribute__ ((aligned));
- 
+
 +/* Buffer to store preview plugins control pipe location */
 +static char g_ppipepath[TMP_LEN_MAX] __attribute__ ((aligned));
 +
  /* Non-persistent runtime states */
  static runstate g_state;
- 
-@@ -687,12 +691,13 @@ static const char * const messages[] = {
+
+@@ -689,12 +693,13 @@ static const char * const messages[] = {
  #define NNN_FCOLORS 5
  #define NNNLVL      6
  #define NNN_PIPE    7
@@ -44,10 +44,10 @@ index 44f297f4..f15b3bc1 100644
 +#define NNN_ORDER   12
 +#define NNN_HELP    13 /* strings end here */
 +#define NNN_TRASH   14 /* flags begin here */
- 
+
  static const char * const env_cfg[] = {
  	"NNN_OPTS",
-@@ -703,6 +708,7 @@ static const char * const env_cfg[] = {
+@@ -705,6 +710,7 @@ static const char * const env_cfg[] = {
  	"NNN_FCOLORS",
  	"NNNLVL",
  	"NNN_PIPE",
@@ -55,16 +55,16 @@ index 44f297f4..f15b3bc1 100644
  	"NNN_MCLICK",
  	"NNN_SEL",
  	"NNN_ARCHIVE",
-@@ -846,7 +852,7 @@ static char *load_input(int fd, const char *path);
+@@ -848,7 +854,7 @@ static char *load_input(int fd, const char *path);
  static int set_sort_flags(int r);
  static void statusbar(char *path);
  #ifndef NOFIFO
 -static void notify_fifo(bool force);
 +static void notify_fifo(bool force, bool closepreview);
  #endif
- 
+
  /* Functions */
-@@ -3045,7 +3051,7 @@ try_quit:
+@@ -3047,7 +3053,7 @@ try_quit:
  			} else {
  #ifndef NOFIFO
  				if (!g_state.fifomode)
@@ -73,14 +73,14 @@ index 44f297f4..f15b3bc1 100644
  #endif
  				escaped = TRUE;
  				settimeout();
-@@ -5131,15 +5137,20 @@ static bool run_cmd_as_plugin(const char *file, uchar_t flags)
- 
+@@ -5139,15 +5145,20 @@ static bool run_cmd_as_plugin(const char *file, char *runfile, uchar_t flags)
+
  static bool plctrl_init(void)
  {
 -	size_t len;
 +	size_t len, lenbuf;
 +	pid_t pid = getpid();
- 
+
  	/* g_tmpfpath is used to generate tmp file names */
  	g_tmpfpath[tmpfplen - 1] = '\0';
 -	len = xstrsncpy(g_pipepath, g_tmpfpath, TMP_LEN_MAX);
@@ -95,13 +95,13 @@ index 44f297f4..f15b3bc1 100644
 +	xstrsncpy(g_ppipepath + len - 1, xitoa(pid), TMP_LEN_MAX - len);
  	setenv(env_cfg[NNN_PIPE], g_pipepath, TRUE);
 +	setenv(env_cfg[NNN_PPIPE], g_ppipepath, TRUE);
- 
+
  	return EXIT_SUCCESS;
  }
-@@ -5168,6 +5179,21 @@ static ssize_t read_nointr(int fd, void *buf, size_t count)
+@@ -5176,6 +5187,21 @@ static ssize_t read_nointr(int fd, void *buf, size_t count)
  	return len;
  }
- 
+
 +void *previewpipe(void *arg __attribute__ ((unused)))
 +{
 +	int fd, buf;
@@ -120,19 +120,19 @@ index 44f297f4..f15b3bc1 100644
  static char *readpipe(int fd, char *ctxnum, char **path)
  {
  	char ctx, *nextpath = NULL;
-@@ -5787,7 +5813,7 @@ static void populate(char *path, char *lastname)
+@@ -5795,7 +5821,7 @@ static void populate(char *path, char *lastname)
  }
- 
+
  #ifndef NOFIFO
 -static void notify_fifo(bool force)
 +static void notify_fifo(bool force, bool closepreview)
  {
  	if (!fifopath)
  		return;
-@@ -5803,6 +5829,12 @@ static void notify_fifo(bool force)
+@@ -5811,6 +5837,12 @@ static void notify_fifo(bool force)
  		}
  	}
- 
+
 +	if (closepreview) {
 +		if (write(fifofd, "close\n", 6) != 6)
 +			xerror();
@@ -140,26 +140,27 @@ index 44f297f4..f15b3bc1 100644
 +	}
 +
  	static struct entry lastentry;
- 
+
  	if (!force && !memcmp(&lastentry, &pdents[cur], sizeof(struct entry)))
-@@ -5852,7 +5884,7 @@ static void move_cursor(int target, int ignore_scrolloff)
- 
+@@ -5860,7 +5892,7 @@ static void move_cursor(int target, int ignore_scrolloff)
+
  #ifndef NOFIFO
  	if (!g_state.fifomode)
 -		notify_fifo(FALSE); /* Send hovered path to NNN_FIFO */
 +		notify_fifo(FALSE, FALSE); /* Send hovered path to NNN_FIFO */
  #endif
  }
- 
-@@ -6473,6 +6505,7 @@ static bool browse(char *ipath, const char *session, int pkey)
+
+@@ -6477,7 +6509,7 @@ static bool browse(char *ipath, const char *session, int pkey)
+ 	pEntry pent;
+ 	enum action sel;
+ 	struct stat sb;
+-	int r = -1, presel, selstartid = 0, selendid = 0;
++	int r = -1, presel, selstartid = 0, selendid = 0, previewkey = 0;
  	const uchar_t opener_flags = (cfg.cliopener ? F_CLI : (F_NOTRACE | F_NOSTDIN | F_NOWAIT));
  	bool watch = FALSE;
  	ino_t inode = 0;
-+	static int previewkey;
- 
- #ifndef NOMOUSE
- 	MEVENT event = {0};
-@@ -6735,7 +6768,7 @@ nochange:
+@@ -6743,7 +6775,7 @@ nochange:
  					move_cursor(r, 1);
  #ifndef NOFIFO
  				else if ((event.bstate == BUTTON1_PRESSED) && !g_state.fifomode)
@@ -168,7 +169,7 @@ index 44f297f4..f15b3bc1 100644
  #endif
  				/* Handle right click selection */
  				if (event.bstate == BUTTON3_PRESSED) {
-@@ -6808,7 +6841,7 @@ nochange:
+@@ -6816,7 +6848,7 @@ nochange:
                          }
  #ifndef NOFIFO
  			if (g_state.fifomode && (sel == SEL_OPEN)) {
@@ -177,16 +178,13 @@ index 44f297f4..f15b3bc1 100644
  				goto nochange;
  			}
  #endif
-@@ -6890,7 +6923,17 @@ nochange:
+@@ -6898,7 +6930,14 @@ nochange:
  			    && strstr(g_buf, "text")
  #endif
  			) {
-+
 +				if (g_state.previewer)
 +					notify_fifo(FALSE, TRUE);
-+
  				spawn(editor, newpath, NULL, NULL, F_CLI);
-+
 +				if (g_state.previewer) {
 +					pkey = previewkey;
 +					goto run_plugin;
@@ -195,16 +193,13 @@ index 44f297f4..f15b3bc1 100644
  				if (cfg.filtermode) {
  					presel = FILTER;
  					clearfilter();
-@@ -7198,7 +7241,17 @@ nochange:
+@@ -7206,7 +7245,14 @@ nochange:
  				copycurname();
  				goto nochange;
  			case SEL_EDIT:
-+
 +				if (g_state.previewer)
 +					notify_fifo(FALSE, TRUE);
-+
  				spawn(editor, newpath, NULL, NULL, F_CLI);
-+
 +				if (g_state.previewer) {
 +					pkey = previewkey;
 +					goto run_plugin;
@@ -213,18 +208,18 @@ index 44f297f4..f15b3bc1 100644
  				continue;
  			default: /* SEL_LOCK */
  				lock_terminal();
-@@ -7558,6 +7611,7 @@ nochange:
- 
+@@ -7566,6 +7612,7 @@ nochange:
+
  			goto begin;
  		}
 +run_plugin:
  		case SEL_PLUGIN:
  			/* Check if directory is accessible */
  			if (!xdiraccess(plgpath)) {
-@@ -7583,6 +7637,12 @@ nochange:
+@@ -7591,6 +7638,12 @@ nochange:
  					goto nochange;
  				}
- 
+
 +				if (xstrcmp(tmp, "preview-tui") == 0) {
 +					previewkey = r;
 +					pthread_t tid;
@@ -234,7 +229,7 @@ index 44f297f4..f15b3bc1 100644
  				if (tmp[0] == '-' && tmp[1]) {
  					++tmp;
  					r = FALSE; /* Do not refresh dir after completion */
-@@ -8181,8 +8241,10 @@ static void cleanup(void)
+@@ -8189,8 +8242,10 @@ static void cleanup(void)
  	if (g_state.autofifo)
  		unlink(fifopath);
  #endif
@@ -246,8 +241,8 @@ index 44f297f4..f15b3bc1 100644
  #ifdef DEBUG
  	disabledbg();
  #endif
-@@ -8681,7 +8743,7 @@ int main(int argc, char *argv[])
- 
+@@ -8689,7 +8744,7 @@ int main(int argc, char *argv[])
+
  #ifndef NOFIFO
  	if (!g_state.fifomode)
 -		notify_fifo(FALSE);
-- 
2.51.0