"Fossies" - the Fresh Open Source Software Archive

Member "nnn-4.4/patches/restorepreview/mainline.diff" (23 Nov 2021, 7358 Bytes) of package /linux/misc/nnn-v4.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Diff source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "mainline.diff": v4.3_vs_v4.4.

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