Support regex/substring filter switch

This commit is contained in:
Arun Prakash Jana 2020-01-11 11:23:57 +05:30
parent 0e203288b4
commit 8588b3f0bd
No known key found for this signature in database
GPG key ID: A75979F35C080412
2 changed files with 35 additions and 8 deletions

8
nnn.1
View file

@ -138,7 +138,13 @@ When a session is loaded dynamically, the last working session is saved automati
All the session files are located in the \fB${XDG_CONFIG_HOME:-$HOME/.config}/nnn/sessions\fR directory by the session name. All the session files are located in the \fB${XDG_CONFIG_HOME:-$HOME/.config}/nnn/sessions\fR directory by the session name.
"@" is the "last session" file. "@" is the "last session" file.
.Sh FILTERS .Sh FILTERS
Filters are substrings to find matching entries in the current directory instantly (search-as-you-type). There is a program option to use regex filters. Filters are substrings to find matching entries in the current directory instantly (\fIsearch-as-you-type\fR). There is a program option to switch to regex filters.
.Pp
To switch the filter type at runtime:
.br
- string to regex: press '\\' at empty filter prompt
.br
- regex to string: press '/' at empty filter prompt
.Pp .Pp
Common regex use cases: Common regex use cases:
.Pp .Pp

View file

@ -128,6 +128,7 @@
#define CMD_LEN_MAX (PATH_MAX + ((NAME_MAX + 1) << 1)) #define CMD_LEN_MAX (PATH_MAX + ((NAME_MAX + 1) << 1))
#define READLINE_MAX 128 #define READLINE_MAX 128
#define FILTER '/' #define FILTER '/'
#define RFILTER '\\'
#define MSGWAIT '$' #define MSGWAIT '$'
#define REGEX_MAX 48 #define REGEX_MAX 48
#define BM_MAX 10 #define BM_MAX 10
@ -243,7 +244,7 @@ typedef struct {
uint useeditor : 1; /* Use VISUAL to open text files */ uint useeditor : 1; /* Use VISUAL to open text files */
uint runplugin : 1; /* Choose plugin mode */ uint runplugin : 1; /* Choose plugin mode */
uint runctx : 2; /* The context in which plugin is to be run */ uint runctx : 2; /* The context in which plugin is to be run */
uint filter_re : 1; /* Use regex filters */ uint regex : 1; /* Use regex filters */
uint x11 : 1; /* Copy to system clipboard and show notis */ uint x11 : 1; /* Copy to system clipboard and show notis */
uint trash : 1; /* Move removed files to trash */ uint trash : 1; /* Move removed files to trash */
uint mtime : 1; /* Use modification time (else access time) */ uint mtime : 1; /* Use modification time (else access time) */
@ -296,7 +297,7 @@ static settings cfg = {
0, /* useeditor */ 0, /* useeditor */
0, /* runplugin */ 0, /* runplugin */
0, /* runctx */ 0, /* runctx */
0, /* filter_re */ 0, /* regex */
0, /* x11 */ 0, /* x11 */
0, /* trash */ 0, /* trash */
1, /* mtime */ 1, /* mtime */
@ -2027,11 +2028,11 @@ static int matches(const char *fltr)
regex_t re; regex_t re;
/* Search filter */ /* Search filter */
if (cfg.filter_re && setfilter(&re, fltr) != 0) if (cfg.regex && setfilter(&re, fltr) != 0)
return -1; return -1;
ndents = fill(fltr, &re); ndents = fill(fltr, &re);
if (cfg.filter_re) if (cfg.regex)
regfree(&re); regfree(&re);
qsort(dents, ndents, sizeof(*dents), entrycmpfn); qsort(dents, ndents, sizeof(*dents), entrycmpfn);
@ -2047,7 +2048,7 @@ static int filterentries(char *path, char *lastname)
int r, total = ndents, len; int r, total = ndents, len;
char *pln = g_ctx[cfg.curctx].c_fltr + 1; char *pln = g_ctx[cfg.curctx].c_fltr + 1;
if (ndents && ln[0] == FILTER && *pln) { if (ndents && (ln[0] == FILTER || ln[0] == RFILTER) && *pln) {
if (matches(pln) != -1) { if (matches(pln) != -1) {
move_cursor(dentfind(lastname, ndents), 0); move_cursor(dentfind(lastname, ndents), 0);
redraw(path); redraw(path);
@ -2055,7 +2056,7 @@ static int filterentries(char *path, char *lastname)
len = mbstowcs(wln, ln, REGEX_MAX); len = mbstowcs(wln, ln, REGEX_MAX);
} else { } else {
ln[0] = wln[0] = FILTER; ln[0] = wln[0] = cfg.regex ? RFILTER : FILTER;
ln[1] = wln[1] = '\0'; ln[1] = wln[1] = '\0';
len = 1; len = 1;
} }
@ -2133,6 +2134,26 @@ static int filterentries(char *path, char *lastname)
case '?': /* Help and config key, '?' is an invalid regex */ case '?': /* Help and config key, '?' is an invalid regex */
goto end; goto end;
} }
DPRINTF_S(ln);
if (*ch == RFILTER && ln[0] == FILTER) {
DPRINTF_S("set back");
wln[0] = ln[0] = RFILTER;
cfg.regex = TRUE;
filterfn = &visible_re;
printprompt(ln);
continue;
}
if (*ch == FILTER && ln[0] == RFILTER) {
DPRINTF_S("set forward");
wln[0] = ln[0] = FILTER;
cfg.regex = FALSE;
filterfn = &visible_str;
printprompt(ln);
continue;
}
} }
/* Reset cur in case it's a repeat search */ /* Reset cur in case it's a repeat search */
@ -5765,7 +5786,7 @@ int main(int argc, char *argv[])
cfg.waitedit = 1; cfg.waitedit = 1;
break; break;
case 'g': case 'g':
cfg.filter_re = 1; cfg.regex = 1;
filterfn = &visible_re; filterfn = &visible_re;
break; break;
case 'H': case 'H':