]> Sergey Matveev's repositories - nnn.git/commitdiff
Support regex/substring filter switch
authorArun Prakash Jana <engineerarun@gmail.com>
Sat, 11 Jan 2020 05:53:57 +0000 (11:23 +0530)
committerArun Prakash Jana <engineerarun@gmail.com>
Sat, 11 Jan 2020 06:08:31 +0000 (11:38 +0530)
nnn.1
src/nnn.c

diff --git a/nnn.1 b/nnn.1
index d6725a6b7157ddc7c5336fb5d368a1bc0bc53d03..5094cd3897a929c2e36ff55246fac71d5a9981b4 100644 (file)
--- a/nnn.1
+++ b/nnn.1
@@ -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.
 "@" is the "last session" file.
 .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
 Common regex use cases:
 .Pp
index 78557f42f3d919a1ef4e2874604d086f179721ae..57d5289fac7426e1c2b813988eb5dd78e56d6bdb 100644 (file)
--- a/src/nnn.c
+++ b/src/nnn.c
 #define CMD_LEN_MAX (PATH_MAX + ((NAME_MAX + 1) << 1))
 #define READLINE_MAX 128
 #define FILTER '/'
+#define RFILTER '\\'
 #define MSGWAIT '$'
 #define REGEX_MAX 48
 #define BM_MAX 10
@@ -243,7 +244,7 @@ typedef struct {
        uint useeditor  : 1;  /* Use VISUAL to open text files */
        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 regex      : 1;  /* Use regex filters */
        uint x11        : 1;  /* Copy to system clipboard and show notis */
        uint trash      : 1;  /* Move removed files to trash */
        uint mtime      : 1;  /* Use modification time (else access time) */
@@ -296,7 +297,7 @@ static settings cfg = {
        0, /* useeditor */
        0, /* runplugin */
        0, /* runctx */
-       0, /* filter_re */
+       0, /* regex */
        0, /* x11 */
        0, /* trash */
        1, /* mtime */
@@ -2027,11 +2028,11 @@ static int matches(const char *fltr)
        regex_t re;
 
        /* Search filter */
-       if (cfg.filter_re && setfilter(&re, fltr) != 0)
+       if (cfg.regex && setfilter(&re, fltr) != 0)
                return -1;
 
        ndents = fill(fltr, &re);
-       if (cfg.filter_re)
+       if (cfg.regex)
                regfree(&re);
 
        qsort(dents, ndents, sizeof(*dents), entrycmpfn);
@@ -2047,7 +2048,7 @@ static int filterentries(char *path, char *lastname)
        int r, total = ndents, len;
        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) {
                        move_cursor(dentfind(lastname, ndents), 0);
                        redraw(path);
@@ -2055,7 +2056,7 @@ static int filterentries(char *path, char *lastname)
 
                len = mbstowcs(wln, ln, REGEX_MAX);
        } else {
-               ln[0] = wln[0] = FILTER;
+               ln[0] = wln[0] = cfg.regex ? RFILTER : FILTER;
                ln[1] = wln[1] = '\0';
                len = 1;
        }
@@ -2133,6 +2134,26 @@ static int filterentries(char *path, char *lastname)
                                case '?': /* Help and config key, '?' is an invalid regex */
                                        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 */
@@ -5765,7 +5786,7 @@ int main(int argc, char *argv[])
                        cfg.waitedit = 1;
                        break;
                case 'g':
-                       cfg.filter_re = 1;
+                       cfg.regex = 1;
                        filterfn = &visible_re;
                        break;
                case 'H':