]> Sergey Matveev's repositories - nnn.git/commitdiff
Run a command at filter prompt
authorArun Prakash Jana <engineerarun@gmail.com>
Wed, 11 Sep 2019 06:48:58 +0000 (12:18 +0530)
committerArun Prakash Jana <engineerarun@gmail.com>
Wed, 11 Sep 2019 09:24:23 +0000 (14:54 +0530)
This is a conveninece for nav-as-you-type mode.
With this program option, on pressing ^P, the filter is  executed as cmd.

README.md
nnn.1
src/nnn.c

index 8f1abae1579e4e6f510ccc630b41ad6851b05c54..fbd36a05adc2f1795871ffafc5ee8d6c38d69c27 100644 (file)
--- a/README.md
+++ b/README.md
@@ -202,7 +202,7 @@ Option completion scripts for Bash, Fish and Zsh can be found in respective subd
 #### Cmdline options
 
 ```
-usage: nnn [-a] [-b key] [-d] [-H] [-i] [-n] [-o]
+usage: nnn [-a] [-b key] [-d] [-f] [-H] [-i] [-n] [-o]
            [-p file] [-r] [-s] [-S] [-t] [-v] [-h] [PATH]
 
 The missing terminal file manager for X.
@@ -214,6 +214,7 @@ optional args:
  -a      use access time
  -b key  open bookmark key
  -d      detail mode
+ -f      run filter as cmd on ^P
  -H      show hidden files
  -i      nav-as-you-type mode
  -n      version sort
diff --git a/nnn.1 b/nnn.1
index 851648c5468b699fc905df1a8a44236905b8d3e0..acc8493144d8f76a25d5eeac0fd4947178794566 100644 (file)
--- a/nnn.1
+++ b/nnn.1
@@ -9,6 +9,7 @@
 .Op Ar -a
 .Op Ar -b key
 .Op Ar -d
+.Op Ar -f
 .Op Ar -H
 .Op Ar -i
 .Op Ar -n
@@ -46,6 +47,9 @@ supports the following options:
 .Fl d
         detail mode
 .Pp
+.Fl f
+        run filter as command when ^P is pressed
+.Pp
 .Fl H
         show hidden files
 .Pp
@@ -116,6 +120,8 @@ with a '^' (caret) symbol.
 .Pp
 There is a program option to filter entries by substring match instead of regex.
 .Pp
+There is a program option to execute the current filter as a command when \fI^P\fR is pressed.
+.Pp
 In the \fInavigate-as-you-type\fR mode directories are opened in filter mode,
 allowing continuous navigation. Works best with the \fBarrow keys\fR.
 .br
@@ -157,8 +163,7 @@ when dealing with the !, e and p commands respectively. A single combination to
     NOTE: To run a plugin directly, press 'x' followed by the plugin key.
 .Ed
 .Pp
-\fBNNN_USE_EDITOR:\fR use VISUAL (else EDITOR, preferably CLI, fallback vi) to handle text
-files.
+\fBNNN_USE_EDITOR:\fR use VISUAL (else EDITOR, preferably CLI, fallback vi) to handle text files.
 .Bd -literal
     export NNN_USE_EDITOR=1
 .Ed
index 68ed476e46567e9a740be36f8f460591c22c52b4..0ad0a691547bf0f22e37fe5735c777be3cd966f8 100644 (file)
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -217,7 +217,7 @@ typedef struct {
        uint selmode    : 1;  /* Set when selecting files */
        uint showdetail : 1;  /* Clear to show fewer file info */
        uint ctxactive  : 1;  /* Context active or not */
-       uint reserved   : 7;
+       uint reserved   : 6;
        /* The following settings are global */
        uint curctx     : 2;  /* Current context number */
        uint dircolor   : 1;  /* Current status of dir color */
@@ -230,6 +230,7 @@ typedef struct {
        uint runplugin  : 1;  /* Choose plugin mode */
        uint runctx     : 2;  /* The context in which plugin is to be run */
        uint filter_re  : 1;  /* Use regex filters */
+       uint filtercmd  : 1;  /* Run filter as command on no match */
        uint trash      : 1;  /* Move removed files to trash */
        uint mtime      : 1;  /* Use modification time (else access time) */
 } settings;
@@ -270,6 +271,7 @@ static settings cfg = {
        0, /* runplugin */
        0, /* runctx */
        1, /* filter_re */
+       0, /* filtercmd */
        0, /* trash */
        1, /* mtime */
 };
@@ -1644,12 +1646,10 @@ static int matches(const char *fltr)
        ndents = fill(fltr, &re);
        if (cfg.filter_re)
                regfree(&re);
-       if (!ndents)
-               return 0;
 
        qsort(dents, ndents, sizeof(*dents), entrycmp);
 
-       return 0;
+       return ndents;
 }
 
 static int filterentries(char *path)
@@ -1728,23 +1728,21 @@ static int filterentries(char *path)
                if (r == OK) {
                        /* Handle all control chars in main loop */
                        if (*ch < ASCII_MAX && keyname(*ch)[0] == '^' && *ch != '^') {
+                               DPRINTF_D(*ch);
+                               DPRINTF_S(keyname(*ch));
+
+                               /* If there's a filter, try a command on ^P */
+                               if (cfg.filtercmd && *ch == CONTROL('P') && len > 1) {
+                                       spawn(shell, "-c", pln, path, F_CLI | F_CMD);
+                                       *ch = CONTROL('L');
+                               }
+
                                if (len == 1)
                                        cur = oldcur;
                                goto end;
                        }
 
                        switch (*ch) {
-                       case '\r':  /* with nonl(), this is ENTER key value */
-                               if (len == 1) {
-                                       cur = oldcur;
-                                       goto end;
-                               }
-
-                               if (matches(pln) == -1)
-                                       goto end;
-
-                               redraw(path);
-                               goto end;
                        case '/': /* works as Leader key in filter mode */
                                *ch = CONTROL('_'); // fallthrough
                                if (len == 1)
@@ -4528,7 +4526,7 @@ nochange:
 static void usage(void)
 {
        fprintf(stdout,
-               "%s: nnn [-a] [-b key] [-d] [-H] [-i] [-n] [-o]\n"
+               "%s: nnn [-a] [-b key] [-d] [-f] [-H] [-i] [-n] [-o]\n"
                "           [-p file] [-r] [-s] [-S] [-t] [-v] [-h] [PATH]\n\n"
                "The missing terminal file manager for X.\n\n"
                "positional args:\n"
@@ -4537,6 +4535,7 @@ static void usage(void)
                " -a      use access time\n"
                " -b key  open bookmark key\n"
                " -d      detail mode\n"
+               " -f      run filter as cmd on ^P\n"
                " -H      show hidden files\n"
                " -i      nav-as-you-type mode\n"
                " -n      version sort\n"
@@ -4681,7 +4680,7 @@ int main(int argc, char *argv[])
        bool progress = FALSE;
 #endif
 
-       while ((opt = getopt(argc, argv, "HSiab:dnop:rstvh")) != -1) {
+       while ((opt = getopt(argc, argv, "HSiab:dfnop:rstvh")) != -1) {
                switch (opt) {
                case 'S':
                        cfg.blkorder = 1;
@@ -4700,6 +4699,9 @@ int main(int argc, char *argv[])
                case 'b':
                        arg = optarg;
                        break;
+               case 'f':
+                       cfg.filtercmd = 1;
+                       break;
                case 'H':
                        cfg.showhidden = 1;
                        break;