Run a command at filter prompt

This is a conveninece for nav-as-you-type mode.
With this program option, on pressing ^P, the filter is  executed as cmd.
This commit is contained in:
Arun Prakash Jana 2019-09-11 12:18:58 +05:30
parent ccf20f830b
commit bb5ef221b3
No known key found for this signature in database
GPG key ID: A75979F35C080412
3 changed files with 28 additions and 20 deletions

View file

@ -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

9
nnn.1
View file

@ -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

View file

@ -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;