]> Sergey Matveev's repositories - nnn.git/blob - patches/restorepreview/mainline.diff
4384d65801c7139931ed4ea666800310ec423add
[nnn.git] / patches / restorepreview / mainline.diff
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 b10143a4..76d6bc5b 100644
11 --- a/src/nnn.c
12 +++ b/src/nnn.c
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 */
19  } runstate;
20  
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));
24  
25 +/* Buffer to store preview plugins control pipe location */
26 +static char g_ppipepath[TMP_LEN_MAX] __attribute__ ((aligned));
27 +
28  /* Non-persistent runtime states */
29  static runstate g_state;
30  
31 @@ -676,12 +680,13 @@ static const char * const messages[] = {
32  #define NNN_FCOLORS 5
33  #define NNNLVL      6
34  #define NNN_PIPE    7
35 -#define NNN_MCLICK  8
36 -#define NNN_SEL     9
37 -#define NNN_ARCHIVE 10
38 -#define NNN_ORDER   11
39 -#define NNN_HELP    12 /* strings end here */
40 -#define NNN_TRASH   13 /* flags begin here */
41 +#define NNN_PPIPE   8
42 +#define NNN_MCLICK  9
43 +#define NNN_SEL     10
44 +#define NNN_ARCHIVE 11
45 +#define NNN_ORDER   12
46 +#define NNN_HELP    13 /* strings end here */
47 +#define NNN_TRASH   14 /* flags begin here */
48  
49  static const char * const env_cfg[] = {
50         "NNN_OPTS",
51 @@ -692,6 +697,7 @@ static const char * const env_cfg[] = {
52         "NNN_FCOLORS",
53         "NNNLVL",
54         "NNN_PIPE",
55 +       "NNN_PPIPE",
56         "NNN_MCLICK",
57         "NNN_SEL",
58         "NNN_ARCHIVE",
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);
62  #ifndef NOFIFO
63 -static void notify_fifo(bool force);
64 +static void notify_fifo(bool force, bool closepreview);
65  #endif
66  
67  /* Functions */
68 @@ -3022,7 +3028,7 @@ try_quit:
69                         } else {
70  #ifndef NOFIFO
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 */
74  #endif
75                                 escaped = TRUE;
76                                 settimeout();
77 @@ -5120,15 +5126,20 @@ static void run_cmd_as_plugin(const char *file, char *runfile, uchar_t flags)
78  
79  static bool plctrl_init(void)
80  {
81 -       size_t len;
82 +       size_t len, lenbuf;
83 +       pid_t pid = getpid();
84  
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);
99  
100         return EXIT_SUCCESS;
101  }
102 @@ -5157,6 +5168,21 @@ static ssize_t read_nointr(int fd, void *buf, size_t count)
103         return len;
104  }
105  
106 +void *previewpipe(void *arg __attribute__ ((unused)))
107 +{
108 +       int fd, buf;
109 +
110 +       mkfifo(g_ppipepath, 0600);
111 +       fd = open(g_ppipepath, O_RDONLY);
112 +
113 +       if (read(fd, &buf, 1) == 1)
114 +               g_state.previewer = buf;
115 +
116 +       close(fd);
117 +       unlink(g_ppipepath);
118 +       return NULL;
119 +}
120 +
121  static char *readpipe(int fd, char *ctxnum, char **path)
122  {
123         char ctx, *nextpath = NULL;
124 @@ -5841,7 +5867,7 @@ static void populate(char *path, char *lastname)
125  }
126  
127  #ifndef NOFIFO
128 -static void notify_fifo(bool force)
129 +static void notify_fifo(bool force, bool closepreview)
130  {
131         if (!fifopath)
132                 return;
133 @@ -5857,6 +5883,12 @@ static void notify_fifo(bool force)
134                 }
135         }
136  
137 +       if (closepreview) {
138 +               if (write(fifofd, "close\n", 6) != 6)
139 +                       xerror();
140 +               return;
141 +       }
142 +
143         static struct entry lastentry;
144  
145         if (!force && !memcmp(&lastentry, &pdents[cur], sizeof(struct entry))) // NOLINT
146 @@ -5889,7 +5921,7 @@ static void send_to_explorer(int *presel)
147                 if (fd > 1)
148                         close(fd);
149         } else
150 -               notify_fifo(TRUE); /* Send opened path to NNN_FIFO */
151 +               notify_fifo(TRUE, FALSE); /* Send opened path to NNN_FIFO */
152  }
153  #endif
154  
155 @@ -5922,7 +5954,7 @@ static void move_cursor(int target, int ignore_scrolloff)
156  
157  #ifndef NOFIFO
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 */
161  #endif
162  }
163  
164 @@ -6539,7 +6571,7 @@ static bool browse(char *ipath, const char *session, int pkey)
165         pEntry pent;
166         enum action sel;
167         struct stat sb;
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;
172         ino_t inode = 0;
173 @@ -6797,7 +6829,7 @@ nochange:
174                                         move_cursor(r, 1);
175  #ifndef NOFIFO
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 */
179  #endif
180                                 /* Handle right click selection */
181                                 if (event.bstate == BUTTON3_PRESSED) {
182 @@ -6959,7 +6991,14 @@ nochange:
183                             && strstr(g_buf, "text")
184  #endif
185                         ) {
186 +                               if (g_state.previewer)
187 +                                       notify_fifo(FALSE, TRUE);
188                                 spawn(editor, newpath, NULL, NULL, F_CLI);
189 +                               if (g_state.previewer) {
190 +                                       pkey = previewkey;
191 +                                       goto run_plugin;
192 +                               }
193 +
194                                 if (cfg.filtermode) {
195                                         presel = FILTER;
196                                         clearfilter();
197 @@ -7272,8 +7311,14 @@ nochange:
198                                 copycurname();
199                                 goto nochange;
200                         case SEL_EDIT:
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) {
206 +                                       pkey = previewkey;
207 +                                       goto run_plugin;
208 +                               }
209                                 continue;
210                         default: /* SEL_LOCK */
211                                 lock_terminal();
212 @@ -7642,6 +7687,7 @@ nochange:
213                         cd = FALSE;
214                         goto begin;
215                 }
216 +run_plugin:
217                 case SEL_PLUGIN:
218                         /* Check if directory is accessible */
219                         if (!xdiraccess(plgpath)) {
220 @@ -7667,6 +7713,12 @@ nochange:
221                                         goto nochange;
222                                 }
223  
224 +                               if (xstrcmp(tmp, "preview-tui") == 0) {
225 +                                       previewkey = r;
226 +                                       pthread_t tid;
227 +                                       pthread_create(&tid, NULL, previewpipe, NULL);
228 +                               }
229 +
230                                 if (tmp[0] == '-' && tmp[1]) {
231                                         ++tmp;
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
236                 case SEL_PROMPT:
237 +                       if (g_state.previewer)
238 +                               notify_fifo(FALSE, TRUE);
239                         r = handle_cmd(sel, newpath);
240 +                       if (g_state.previewer) {
241 +                               pkey = previewkey;
242 +                               goto run_plugin;
243 +                       }
244  
245                         /* Continue in type-to-nav mode, if enabled */
246                         if (cfg.filtermode)
247 @@ -8263,8 +8321,10 @@ static void cleanup(void)
248         if (g_state.autofifo)
249                 unlink(fifopath);
250  #endif
251 -       if (g_state.pluginit)
252 +       if (g_state.pluginit){
253                 unlink(g_pipepath);
254 +               unlink(g_ppipepath);
255 +       }
256  #ifdef DEBUG
257         disabledbg();
258  #endif
259 @@ -8769,7 +8829,7 @@ int main(int argc, char *argv[])
260  
261  #ifndef NOFIFO
262         if (!g_state.fifomode)
263 -               notify_fifo(FALSE);
264 +               notify_fifo(FALSE, TRUE);
265         if (fifofd != -1)
266                 close(fifofd);
267  #endif