1 # Description: Adds preview pipe to enable closing and re-opening the preview
2 # pane when running an undetached editor. If you are using vim
3 # you might experience incorrectly resized window. Consider adding
4 # the following to your vimrc:
5 # autocmd VimEnter * :silent exec "!kill -s WINCH $PPID"
7 # Authors: Luuk van Baal
9 diff --git a/src/nnn.c b/src/nnn.c
10 index b10143a4..76d6bc5b 100644
13 @@ -374,6 +374,7 @@ typedef struct {
14 uint_t trash : 2; /* Trash method 0: rm -rf, 1: trash-cli, 2: gio trash */
15 uint_t uidgid : 1; /* Show owner and group info */
16 uint_t usebsdtar : 1; /* Use bsdtar as default archive utility */
17 + uint_t previewer : 1; /* Run state of previewer */
18 uint_t reserved : 5; /* Adjust when adding/removing a field */
21 @@ -500,6 +501,9 @@ static char g_tmpfpath[TMP_LEN_MAX] __attribute__ ((aligned));
22 /* Buffer to store plugins control pipe location */
23 static char g_pipepath[TMP_LEN_MAX] __attribute__ ((aligned));
25 +/* Buffer to store preview plugins control pipe location */
26 +static char g_ppipepath[TMP_LEN_MAX] __attribute__ ((aligned));
28 /* Non-persistent runtime states */
29 static runstate g_state;
31 @@ -676,12 +680,13 @@ static const char * const messages[] = {
37 -#define NNN_ARCHIVE 10
39 -#define NNN_HELP 12 /* strings end here */
40 -#define NNN_TRASH 13 /* flags begin here */
44 +#define NNN_ARCHIVE 11
46 +#define NNN_HELP 13 /* strings end here */
47 +#define NNN_TRASH 14 /* flags begin here */
49 static const char * const env_cfg[] = {
51 @@ -692,6 +697,7 @@ static const char * const env_cfg[] = {
59 @@ -833,7 +839,7 @@ static int set_sort_flags(int r);
60 static void statusbar(char *path);
61 static bool get_output(char *file, char *arg1, char *arg2, int fdout, bool multi, bool page);
63 -static void notify_fifo(bool force);
64 +static void notify_fifo(bool force, bool closepreview);
68 @@ -3022,7 +3028,7 @@ try_quit:
71 if (!g_state.fifomode)
72 - notify_fifo(TRUE); /* Send hovered path to NNN_FIFO */
73 + notify_fifo(TRUE, FALSE); /* Send hovered path to NNN_FIFO */
77 @@ -5120,15 +5126,20 @@ static void run_cmd_as_plugin(const char *file, char *runfile, uchar_t flags)
79 static bool plctrl_init(void)
83 + pid_t pid = getpid();
85 /* g_tmpfpath is used to generate tmp file names */
86 g_tmpfpath[tmpfplen - 1] = '\0';
87 - len = xstrsncpy(g_pipepath, g_tmpfpath, TMP_LEN_MAX);
88 + len = lenbuf = xstrsncpy(g_pipepath, g_tmpfpath, TMP_LEN_MAX);
89 g_pipepath[len - 1] = '/';
90 - len = xstrsncpy(g_pipepath + len, "nnn-pipe.", TMP_LEN_MAX - len) + len;
91 - xstrsncpy(g_pipepath + len - 1, xitoa(getpid()), TMP_LEN_MAX - len);
92 + xstrsncpy(g_ppipepath, g_pipepath, TMP_LEN_MAX);
93 + len += xstrsncpy(g_pipepath + len, "nnn-pipe.", TMP_LEN_MAX - len);
94 + xstrsncpy(g_pipepath + len - 1, xitoa(pid), TMP_LEN_MAX - len);
95 + len = xstrsncpy(g_ppipepath + lenbuf, "nnn-ppipe.", TMP_LEN_MAX - lenbuf) + lenbuf;
96 + xstrsncpy(g_ppipepath + len - 1, xitoa(pid), TMP_LEN_MAX - len);
97 setenv(env_cfg[NNN_PIPE], g_pipepath, TRUE);
98 + setenv(env_cfg[NNN_PPIPE], g_ppipepath, TRUE);
102 @@ -5157,6 +5168,21 @@ static ssize_t read_nointr(int fd, void *buf, size_t count)
106 +void *previewpipe(void *arg __attribute__ ((unused)))
110 + mkfifo(g_ppipepath, 0600);
111 + fd = open(g_ppipepath, O_RDONLY);
113 + if (read(fd, &buf, 1) == 1)
114 + g_state.previewer = buf;
117 + unlink(g_ppipepath);
121 static char *readpipe(int fd, char *ctxnum, char **path)
123 char ctx, *nextpath = NULL;
124 @@ -5841,7 +5867,7 @@ static void populate(char *path, char *lastname)
128 -static void notify_fifo(bool force)
129 +static void notify_fifo(bool force, bool closepreview)
133 @@ -5857,6 +5883,12 @@ static void notify_fifo(bool force)
137 + if (closepreview) {
138 + if (write(fifofd, "close\n", 6) != 6)
143 static struct entry lastentry;
145 if (!force && !memcmp(&lastentry, &pdents[cur], sizeof(struct entry))) // NOLINT
146 @@ -5889,7 +5921,7 @@ static void send_to_explorer(int *presel)
150 - notify_fifo(TRUE); /* Send opened path to NNN_FIFO */
151 + notify_fifo(TRUE, FALSE); /* Send opened path to NNN_FIFO */
155 @@ -5922,7 +5954,7 @@ static void move_cursor(int target, int ignore_scrolloff)
158 if (!g_state.fifomode)
159 - notify_fifo(FALSE); /* Send hovered path to NNN_FIFO */
160 + notify_fifo(FALSE, FALSE); /* Send hovered path to NNN_FIFO */
164 @@ -6539,7 +6571,7 @@ static bool browse(char *ipath, const char *session, int pkey)
168 - int r = -1, presel, selstartid = 0, selendid = 0;
169 + int r = -1, presel, selstartid = 0, selendid = 0, previewkey = 0;
170 const uchar_t opener_flags = (cfg.cliopener ? F_CLI : (F_NOTRACE | F_NOSTDIN | F_NOWAIT));
171 bool watch = FALSE, cd = TRUE;
173 @@ -6797,7 +6829,7 @@ nochange:
176 else if ((event.bstate == BUTTON1_PRESSED) && !g_state.fifomode)
177 - notify_fifo(TRUE); /* Send clicked path to NNN_FIFO */
178 + notify_fifo(TRUE, FALSE); /* Send clicked path to NNN_FIFO */
180 /* Handle right click selection */
181 if (event.bstate == BUTTON3_PRESSED) {
182 @@ -6959,7 +6991,14 @@ nochange:
183 && strstr(g_buf, "text")
186 + if (g_state.previewer)
187 + notify_fifo(FALSE, TRUE);
188 spawn(editor, newpath, NULL, NULL, F_CLI);
189 + if (g_state.previewer) {
194 if (cfg.filtermode) {
197 @@ -7272,8 +7311,14 @@ nochange:
201 + if (g_state.previewer)
202 + notify_fifo(FALSE, TRUE);
203 if (!(g_state.picker || g_state.fifomode))
204 spawn(editor, newpath, NULL, NULL, F_CLI);
205 + if (g_state.previewer) {
210 default: /* SEL_LOCK */
212 @@ -7642,6 +7687,7 @@ nochange:
218 /* Check if directory is accessible */
219 if (!xdiraccess(plgpath)) {
220 @@ -7667,6 +7713,12 @@ nochange:
224 + if (xstrcmp(tmp, "preview-tui") == 0) {
227 + pthread_create(&tid, NULL, previewpipe, NULL);
230 if (tmp[0] == '-' && tmp[1]) {
232 r = FALSE; /* Do not refresh dir after completion */
233 @@ -7722,7 +7774,13 @@ nochange:
234 case SEL_SHELL: // fallthrough
235 case SEL_LAUNCH: // fallthrough
237 + if (g_state.previewer)
238 + notify_fifo(FALSE, TRUE);
239 r = handle_cmd(sel, newpath);
240 + if (g_state.previewer) {
245 /* Continue in type-to-nav mode, if enabled */
247 @@ -8263,8 +8321,10 @@ static void cleanup(void)
248 if (g_state.autofifo)
251 - if (g_state.pluginit)
252 + if (g_state.pluginit){
254 + unlink(g_ppipepath);
259 @@ -8769,7 +8829,7 @@ int main(int argc, char *argv[])
262 if (!g_state.fifomode)
263 - notify_fifo(FALSE);
264 + notify_fifo(FALSE, TRUE);