mainline.diff (7955B)
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" 6 # 7 # Authors: Luuk van Baal 8 9 diff --git a/src/nnn.c b/src/nnn.c 10 index 4243f2cc..b893f573 100644 11 --- a/src/nnn.c 12 +++ b/src/nnn.c 13 @@ -370,7 +370,8 @@ typedef struct { 14 uint_t stayonsel : 1; /* Disable auto-proceed on select */ 15 uint_t trash : 2; /* Trash method 0: rm -rf, 1: trash-cli, 2: gio trash */ 16 uint_t uidgid : 1; /* Show owner and group info */ 17 - uint_t reserved : 7; /* Adjust when adding/removing a field */ 18 + uint_t previewer : 1; /* Run state of previewer */ 19 + uint_t reserved : 6; /* Adjust when adding/removing a field */ 20 } runstate; 21 22 /* Contexts or workspaces */ 23 @@ -518,6 +519,9 @@ static char g_tmpfpath[TMP_LEN_MAX] __attribute__ ((aligned)); 24 /* Buffer to store plugins control pipe location */ 25 static char g_pipepath[TMP_LEN_MAX] __attribute__ ((aligned)); 26 27 +/* Buffer to store preview plugins control pipe location */ 28 +static char g_ppipepath[TMP_LEN_MAX] __attribute__ ((aligned)); 29 + 30 /* Non-persistent runtime states */ 31 static runstate g_state; 32 33 @@ -692,12 +696,13 @@ static const char * const messages[] = { 34 #define NNN_FCOLORS 5 35 #define NNNLVL 6 36 #define NNN_PIPE 7 37 -#define NNN_MCLICK 8 38 -#define NNN_SEL 9 39 -#define NNN_ARCHIVE 10 40 -#define NNN_ORDER 11 41 -#define NNN_HELP 12 /* strings end here */ 42 -#define NNN_TRASH 13 /* flags begin here */ 43 +#define NNN_PPIPE 8 44 +#define NNN_MCLICK 9 45 +#define NNN_SEL 10 46 +#define NNN_ARCHIVE 11 47 +#define NNN_ORDER 12 48 +#define NNN_HELP 13 /* strings end here */ 49 +#define NNN_TRASH 14 /* flags begin here */ 50 51 static const char * const env_cfg[] = { 52 "NNN_OPTS", 53 @@ -708,6 +713,7 @@ static const char * const env_cfg[] = { 54 "NNN_FCOLORS", 55 "NNNLVL", 56 "NNN_PIPE", 57 + "NNN_PPIPE", 58 "NNN_MCLICK", 59 "NNN_SEL", 60 "NNN_ARCHIVE", 61 @@ -854,7 +860,7 @@ static int set_sort_flags(int r); 62 static void statusbar(char *path); 63 static bool get_output(char *file, char *arg1, char *arg2, int fdout, bool multi, bool page); 64 #ifndef NOFIFO 65 -static void notify_fifo(bool force); 66 +static void notify_fifo(bool force, bool closepreview); 67 #endif 68 69 /* Functions */ 70 @@ -3061,7 +3067,7 @@ try_quit: 71 } else { 72 #ifndef NOFIFO 73 if (!g_state.fifomode) 74 - notify_fifo(TRUE); /* Send hovered path to NNN_FIFO */ 75 + notify_fifo(TRUE, FALSE); /* Send hovered path to NNN_FIFO */ 76 #endif 77 escaped = TRUE; 78 settimeout(); 79 @@ -5160,15 +5166,20 @@ static bool run_cmd_as_plugin(const char *file, char *runfile, uchar_t flags) 80 81 static bool plctrl_init(void) 82 { 83 - size_t len; 84 + size_t len, lenbuf; 85 + pid_t pid = getpid(); 86 87 /* g_tmpfpath is used to generate tmp file names */ 88 g_tmpfpath[tmpfplen - 1] = '\0'; 89 - len = xstrsncpy(g_pipepath, g_tmpfpath, TMP_LEN_MAX); 90 + len = lenbuf = xstrsncpy(g_pipepath, g_tmpfpath, TMP_LEN_MAX); 91 g_pipepath[len - 1] = '/'; 92 - len = xstrsncpy(g_pipepath + len, "nnn-pipe.", TMP_LEN_MAX - len) + len; 93 - xstrsncpy(g_pipepath + len - 1, xitoa(getpid()), TMP_LEN_MAX - len); 94 + xstrsncpy(g_ppipepath, g_pipepath, TMP_LEN_MAX); 95 + len += xstrsncpy(g_pipepath + len, "nnn-pipe.", TMP_LEN_MAX - len); 96 + xstrsncpy(g_pipepath + len - 1, xitoa(pid), TMP_LEN_MAX - len); 97 + len = xstrsncpy(g_ppipepath + lenbuf, "nnn-ppipe.", TMP_LEN_MAX - lenbuf) + lenbuf; 98 + xstrsncpy(g_ppipepath + len - 1, xitoa(pid), TMP_LEN_MAX - len); 99 setenv(env_cfg[NNN_PIPE], g_pipepath, TRUE); 100 + setenv(env_cfg[NNN_PPIPE], g_ppipepath, TRUE); 101 102 return EXIT_SUCCESS; 103 } 104 @@ -5197,6 +5208,21 @@ static ssize_t read_nointr(int fd, void *buf, size_t count) 105 return len; 106 } 107 108 +void *previewpipe(void *arg __attribute__ ((unused))) 109 +{ 110 + int fd, buf; 111 + 112 + mkfifo(g_ppipepath, 0600); 113 + fd = open(g_ppipepath, O_RDONLY); 114 + 115 + if (read(fd, &buf, 1) == 1) 116 + g_state.previewer = buf; 117 + 118 + close(fd); 119 + unlink(g_ppipepath); 120 + return NULL; 121 +} 122 + 123 static char *readpipe(int fd, char *ctxnum, char **path) 124 { 125 char ctx, *nextpath = NULL; 126 @@ -5862,7 +5888,7 @@ static void populate(char *path, char *lastname) 127 } 128 129 #ifndef NOFIFO 130 -static void notify_fifo(bool force) 131 +static void notify_fifo(bool force, bool closepreview) 132 { 133 if (!fifopath) 134 return; 135 @@ -5878,6 +5904,12 @@ static void notify_fifo(bool force) 136 } 137 } 138 139 + if (closepreview) { 140 + if (write(fifofd, "close\n", 6) != 6) 141 + xerror(); 142 + return; 143 + } 144 + 145 static struct entry lastentry; 146 147 if (!force && !memcmp(&lastentry, &pdents[cur], sizeof(struct entry))) 148 @@ -5910,7 +5942,7 @@ static void send_to_explorer(int *presel) 149 if (fd > 1) 150 close(fd); 151 } else 152 - notify_fifo(TRUE); /* Send opened path to NNN_FIFO */ 153 + notify_fifo(TRUE, FALSE); /* Send opened path to NNN_FIFO */ 154 } 155 #endif 156 157 @@ -5943,7 +5975,7 @@ static void move_cursor(int target, int ignore_scrolloff) 158 159 #ifndef NOFIFO 160 if (!g_state.fifomode) 161 - notify_fifo(FALSE); /* Send hovered path to NNN_FIFO */ 162 + notify_fifo(FALSE, FALSE); /* Send hovered path to NNN_FIFO */ 163 #endif 164 } 165 166 @@ -6565,7 +6597,7 @@ static bool browse(char *ipath, const char *session, int pkey) 167 pEntry pent; 168 enum action sel; 169 struct stat sb; 170 - int r = -1, presel, selstartid = 0, selendid = 0; 171 + int r = -1, presel, selstartid = 0, selendid = 0, previewkey = 0; 172 const uchar_t opener_flags = (cfg.cliopener ? F_CLI : (F_NOTRACE | F_NOSTDIN | F_NOWAIT)); 173 bool watch = FALSE, cd = TRUE; 174 ino_t inode = 0; 175 @@ -6819,7 +6851,7 @@ nochange: 176 move_cursor(r, 1); 177 #ifndef NOFIFO 178 else if ((event.bstate == BUTTON1_PRESSED) && !g_state.fifomode) 179 - notify_fifo(TRUE); /* Send clicked path to NNN_FIFO */ 180 + notify_fifo(TRUE, FALSE); /* Send clicked path to NNN_FIFO */ 181 #endif 182 /* Handle right click selection */ 183 if (event.bstate == BUTTON3_PRESSED) { 184 @@ -6979,7 +7011,14 @@ nochange: 185 && strstr(g_buf, "text") 186 #endif 187 ) { 188 + if (g_state.previewer) 189 + notify_fifo(FALSE, TRUE); 190 spawn(editor, newpath, NULL, NULL, F_CLI); 191 + if (g_state.previewer) { 192 + pkey = previewkey; 193 + goto run_plugin; 194 + } 195 + 196 if (cfg.filtermode) { 197 presel = FILTER; 198 clearfilter(); 199 @@ -7291,8 +7330,14 @@ nochange: 200 copycurname(); 201 goto nochange; 202 case SEL_EDIT: 203 + if (g_state.previewer) 204 + notify_fifo(FALSE, TRUE); 205 if (!g_state.picker) 206 spawn(editor, newpath, NULL, NULL, F_CLI); 207 + if (g_state.previewer) { 208 + pkey = previewkey; 209 + goto run_plugin; 210 + } 211 continue; 212 default: /* SEL_LOCK */ 213 lock_terminal(); 214 @@ -7661,6 +7706,7 @@ nochange: 215 cd = FALSE; 216 goto begin; 217 } 218 +run_plugin: 219 case SEL_PLUGIN: 220 /* Check if directory is accessible */ 221 if (!xdiraccess(plgpath)) { 222 @@ -7686,6 +7732,12 @@ nochange: 223 goto nochange; 224 } 225 226 + if (xstrcmp(tmp, "preview-tui") == 0) { 227 + previewkey = r; 228 + pthread_t tid; 229 + pthread_create(&tid, NULL, previewpipe, NULL); 230 + } 231 + 232 if (tmp[0] == '-' && tmp[1]) { 233 ++tmp; 234 r = FALSE; /* Do not refresh dir after completion */ 235 @@ -7740,7 +7792,13 @@ nochange: 236 case SEL_SHELL: // fallthrough 237 case SEL_LAUNCH: // fallthrough 238 case SEL_PROMPT: 239 + if (g_state.previewer) 240 + notify_fifo(FALSE, TRUE); 241 r = handle_cmd(sel, newpath); 242 + if (g_state.previewer) { 243 + pkey = previewkey; 244 + goto run_plugin; 245 + } 246 247 /* Continue in type-to-nav mode, if enabled */ 248 if (cfg.filtermode) 249 @@ -8282,8 +8340,10 @@ static void cleanup(void) 250 if (g_state.autofifo) 251 unlink(fifopath); 252 #endif 253 - if (g_state.pluginit) 254 + if (g_state.pluginit){ 255 unlink(g_pipepath); 256 + unlink(g_ppipepath); 257 + } 258 #ifdef DEBUG 259 disabledbg(); 260 #endif 261 @@ -8787,7 +8847,7 @@ int main(int argc, char *argv[]) 262 263 #ifndef NOFIFO 264 if (!g_state.fifomode) 265 - notify_fifo(FALSE); 266 + notify_fifo(FALSE, TRUE); 267 if (fifofd != -1) 268 close(fifofd); 269 #endif