]> Sergey Matveev's repositories - nnn.git/commitdiff
Context code '+' to create context smartly
authorArun Prakash Jana <engineerarun@gmail.com>
Sun, 3 May 2020 10:46:14 +0000 (16:16 +0530)
committerArun Prakash Jana <engineerarun@gmail.com>
Sun, 3 May 2020 10:55:59 +0000 (16:25 +0530)
plugins/README.md
plugins/mimelist
src/nnn.c

index b16ef9ee1892be1c44a4e46ab9f82d776cfa4255..306723604024e4bd494cb000e7b2c39b8823435d 100644 (file)
@@ -161,17 +161,25 @@ Plugins can be written in any scripting language. However, POSIX-compliant shell
 
 Drop the plugin in `${XDG_CONFIG_HOME:-$HOME/.config}/nnn/plugins` and make it executable. Optionally add a hotkey in `$NNN_PLUG` for frequent usage.
 
-#### Controlling `nnn`'s active directory
-`nnn` provides a mechanism for plugins to control its active directory.
+#### Send data to `nnn`
+`nnn` provides a mechanism for plugins to send data to `nnn` to control its active directory or invoke the list mode.
 The way to do so is by writing to the pipe pointed by the environment variable `NNN_PIPE`.
-The plugin should write a single string in the format `<context number><char><path>` without a newline at the end. For example, `1c/etc`.
-The context number indicates the context to change the active directory of (0 is used to indicate the current context).
-The `<char>` indicates the operation type.
+The plugin should write a single string in the format `<ctxcode><opcode><data>` without a newline at the end. For example, `1c/etc`.
 
-: Char : Operation :
+The `ctxcode` indicates the context to change the active directory of.
+
+| Context code | Meaning |
+|:---:| --- |
+| `1`-`4` | context number |
+| `0` | current context |
+| `+` | next inactive context or current (if all active) |
+
+The `opcode` indicates the operation type.
+
+| Opcode | Operation |
 |:---:| --- |
-| c | cd |
-| l | list files in list mode |
+| `c` | change directory |
+| `l` | list files in list mode |
 
 For convenience, we provided a helper script named `.nnn-plugin-helper` and a function named `nnn_cd` to ease this process. `nnn_cd` receives the path to change to as the first argument, and the context as an optional second argument.
 If a context is not provided, it is asked for explicitly. To skip this and choose the current context, set the `CUR_CTX` variable in `.nnn-plugin-helper` to `1`.
index 0ec7c0f35c3966433da3422a5ec822ecaf7b22b2..5635afda3b279e57c1887815e2407592a7f67238 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/env sh
 
-# Description: Run fd/find in subtree and list files by mime type in current context
+# Description: Run fd/find in subtree and list files by mime type in smart context
 # Requires: fd/find
 #
 # Shell: POSIX compliant
@@ -17,5 +17,5 @@ fi
 printf "mime: "
 read -r mime
 
-printf "%s" "0l" > "$NNN_PIPE"
+printf "%s" "+l" > "$NNN_PIPE"
 $fd | file -if- | grep "$mime" | awk -F: '{printf "%s\0", $1}' > "$NNN_PIPE"
index d9a20dfc3a5583786dc3baefc5cd7ec3d8e19aa3..5e215d7469f23096f21261135f95b3223692add4 100644 (file)
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -4234,16 +4234,24 @@ static void rmlistpath()
 
 static void readpipe(int fd, char **path, char **lastname, char **lastdir)
 {
-       char *nextpath = NULL;
+       int r;
+       char ctx, *nextpath = NULL;
        ssize_t len = read(fd, g_buf, 1);
 
        if (len != 1)
                return;
 
-       char ctx = g_buf[0] - '0';
-
-       if (ctx > CTX_MAX)
-               return;
+       if (g_buf[0] == '+') {
+               r = cfg.curctx;
+               do
+                       r = (r + 1) & ~CTX_MAX;
+               while (g_ctx[r].c_cfg.ctxactive && (r != cfg.curctx));
+               ctx = r + 1;
+       } else {
+               ctx = g_buf[0] - '0';
+               if (ctx > CTX_MAX)
+                       return;
+       }
 
        len = read(fd, g_buf, 1);
        if (len != 1)
@@ -4269,7 +4277,7 @@ static void readpipe(int fd, char **path, char **lastname, char **lastdir)
                        xstrsncpy(*lastdir, *path, PATH_MAX);
                        xstrsncpy(*path, nextpath, PATH_MAX);
                } else {
-                       int r = ctx - 1;
+                       r = ctx - 1;
 
                        g_ctx[r].c_cfg.ctxactive = 0;
                        savecurctx(&cfg, nextpath, dents[cur].name, r);