]> Sergey Matveev's repositories - nnn.git/commitdiff
Add -a option to generate a temporary NNN_FIFO (#588)
authorlvgx <l@vgx.fr>
Sat, 23 May 2020 12:03:37 +0000 (14:03 +0200)
committerArun Prakash Jana <engineerarun@gmail.com>
Sat, 23 May 2020 12:10:51 +0000 (17:40 +0530)
* Add -a option to generate a temporary NNN_FIFO

* Add documentation for -a option

* plugins/README.md: promote the use of -a

This obsoletes the global FIFO unlink trick, so I remove mentions of it.
@jarun update:

Polish -a

misc/auto-completion/bash/nnn-completion.bash
misc/auto-completion/fish/nnn.fish
misc/auto-completion/zsh/_nnn
nnn.1
plugins/README.md
src/nnn.c

index e8f2548c6642a5e9ed866eb7b195af9c6bd1db56..820e1c7c1d4fde5cc4afc7ed35bbc17095bce272 100644 (file)
@@ -12,6 +12,7 @@ _nnn ()
     local cur=$2 prev=$3
     local -a opts
     opts=(
+        -a
         -A
         -b
         -c
index 1b6455b2aece8818e6586ad88111628434e3f958..ac6e462c45a48e55fe66fdd8a70807875eed2068 100644 (file)
@@ -11,6 +11,7 @@ else
     set sessions_dir $HOME/.config/nnn/sessions
 end
 
+complete -c nnn -s a    -d 'auto-setup NNN_FIFO'
 complete -c nnn -s A    -d 'disable dir auto-select'
 complete -c nnn -s b -r -d 'bookmark key to open' -x -a '(echo $NNN_BMS | awk -F: -v RS=\; \'{print $1"\t"$2}\')'
 complete -c nnn -s c    -d 'cli-only opener'
index 441589acba4185d2eb6bcd08e14e2bdda98dfb5d..d130b162eba1df7c73767ca29fafbbcf7551f8cf 100644 (file)
@@ -9,6 +9,7 @@
 setopt localoptions noshwordsplit noksharrays
 local -a args
 args=(
+    '(-a)-a[auto-setup NNN_FIFO]'
     '(-A)-A[disable dir auto-select]'
     '(-b)-b[bookmark key to open]:key char'
     '(-c)-c[cli-only opener]'
diff --git a/nnn.1 b/nnn.1
index 7a34f3b8cc7982d1efe6a46201e78706502ffd6a..86a4d8755b10b9175878ed9a356922317126ea3c 100644 (file)
--- a/nnn.1
+++ b/nnn.1
@@ -6,6 +6,7 @@
 .Nd the missing terminal file manager for X
 .Sh SYNOPSIS
 .Nm
+.Op Ar -a
 .Op Ar -A
 .Op Ar -b key
 .Op Ar -c
@@ -54,6 +55,9 @@ to see the list of keybinds.
 .Nm
 supports the following options:
 .Pp
+.Fl a
+        auto-setup temporary NNN_FIFO (described in ENVIRONMENT section)
+.Pp
 .Fl A
         disable directory auto-select in type-to-nav mode
 .Pp
@@ -423,7 +427,9 @@ separated by \fI;\fR:
 .Bd -literal
     export NNN_FIFO='/tmp/nnn.fifo'
 
-    NOTE: If the FIFO file doesn't exist it will be created, but it will never be removed.
+    NOTES:
+    1. Overriden by a temporary path with -a option.
+    2. If the FIFO file doesn't exist it will be created, but not removed (unless it is generated by -a option).
 .Ed
 .Pp
 \fBNNN_LOCKER:\fR terminal locker program.
index 59ae28126b7db08aedba129be6164b1db5a4231b..27448229d07fc5cff8ea125c65744e792c88ef8a 100644 (file)
@@ -198,9 +198,9 @@ Usage examples can be found in the Examples section below.
 
 If `NNN_FIFO` is set, `nnn` will open it and write every hovered files. This can be used in plugins and external scripts, e.g. to implement file previews.
 
-If a `NNN_FIFO` is set globally, each `nnn` instance will write to it, and a process reading from the pipe will get hovered path from every instance, interleaved.
+The easiest way to set `NNN_FIFO` is to start `nnn` with the `-a` option, to automatically setup a temporary FIFO file for this `nnn` instance.
 
-If you want to prevent this and be sure to have a private pipe to one `nnn` instance, you can unlink (remove) the FIFO file. If you had opened the FIFO before and you have read from it (so that `nnn` have it opened too), you can still read from it while you don't close it. But new `nnn` instances will recreate a new FIFO not linked to the previous one.
+If a `NNN_FIFO` environment variable is set globally (and `-a` is not passed to `nnn`), each `nnn` instance will write to the same FIFO, and a process reading from the pipe will get hovered path from every instance, interleaved.
 
 Don't forget to fork in the background to avoid blocking `nnn`.
 
@@ -246,13 +246,6 @@ There are many plugins provided by `nnn` which can be used as examples. Here are
     fi
 
     while read FILE ; do
-        if [ -n "$NNN_FIFO" ] ; then
-            # If you want to remove the FIFO,
-            # don't do it before first read,
-            # nnn won't have it opened yet
-            rm "$NNN_FIFO"
-            NNN_FIFO=
-        fi
         printf "%s" "$FILE" | xsel
     done < "$NNN_FIFO" &
     disown
index 89c4a48a349e3f02421001ca9ff96e92d6b10498..b45eeb2302dfc8784091d6d3e295f46b685d41dd 100644 (file)
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -400,6 +400,7 @@ static char g_pipepath[TMP_LEN_MAX] __attribute__ ((aligned));
 #define STATE_FORTUNE 0x20
 #define STATE_TRASH 0x40
 #define STATE_FORCEQUIT 0x80
+#define STATE_AUTOFIFO 0x100
 
 static uint g_states;
 
@@ -6726,6 +6727,9 @@ static void usage(void)
                "positional args:\n"
                "  PATH   start dir [default: .]\n\n"
                "optional args:\n"
+#ifndef NOFIFO
+               " -a      auto NNN_FIFO\n"
+#endif
                " -A      no dir auto-select\n"
                " -b key  open bookmark key (trumps -s/S)\n"
                " -c      cli-only NNN_OPENER (trumps -e)\n"
@@ -6880,6 +6884,11 @@ static void cleanup(void)
        free(ihashbmp);
        free(bookmark);
        free(plug);
+#ifndef NOFIFO
+       if (g_states & STATE_AUTOFIFO)
+               unlink(fifopath);
+#endif
+
 #ifdef DBGMODE
        disabledbg();
 #endif
@@ -6906,8 +6915,13 @@ int main(int argc, char *argv[])
 
        while ((opt = (env_opts_id > 0
                       ? env_opts[--env_opts_id]
-                      : getopt(argc, argv, "Ab:cdeEfFgHKl:nop:P:QrRs:St:T:Vxh"))) != -1) {
+                      : getopt(argc, argv, "aAb:cdeEfFgHKl:nop:P:QrRs:St:T:Vxh"))) != -1) {
                switch (opt) {
+#ifndef NOFIFO
+               case 'a':
+                       g_states |= STATE_AUTOFIFO;
+                       break;
+#endif
                case 'A':
                        cfg.autoselect = 0;
                        break;
@@ -7154,6 +7168,12 @@ int main(int argc, char *argv[])
 
 #ifndef NOFIFO
        /* Create fifo */
+       if (g_states & STATE_AUTOFIFO) {
+               g_tmpfpath[tmpfplen - 1] = '\0';
+               snprintf(g_buf, CMD_LEN_MAX, "%s/nnn-fifo.%d", g_tmpfpath, getpid());
+               setenv("NNN_FIFO", g_buf, TRUE);
+       }
+
        fifopath = xgetenv("NNN_FIFO", NULL);
        if (fifopath) {
                if (mkfifo(fifopath, 0600) != 0 && !(errno == EEXIST && access(fifopath, W_OK) == 0)) {