applied reject no match patch
[dmenu.git] / dmenu.c
diff --git a/dmenu.c b/dmenu.c
index 3f8f689..9a965e1 100644 (file)
--- a/dmenu.c
+++ b/dmenu.c
@@ -40,6 +40,7 @@ static char *embed;
 static int bh, mw, mh;
 static int inputw = 0, promptw;
 static int lrpad; /* sum of left and right padding */
+static int reject_no_match = 0;
 static size_t cursor;
 static struct item *items = NULL;
 static struct item *matches, *matchend;
@@ -283,12 +284,26 @@ insert(const char *str, ssize_t n)
 {
        if (strlen(text) + n > sizeof text - 1)
                return;
+
+       static char last[BUFSIZ] = "";
+       if(reject_no_match) {
+               /* store last text value in case we need to revert it */
+               memcpy(last, text, BUFSIZ);
+       }
+
        /* move existing text out of the way, insert new text, and update cursor */
        memmove(&text[cursor + n], &text[cursor], sizeof text - cursor - MAX(n, 0));
        if (n > 0)
                memcpy(&text[cursor], str, n);
        cursor += n;
        match();
+
+       if(!matches && reject_no_match) {
+               /* revert to last text value if theres no match */
+               memcpy(text, last, BUFSIZ);
+               cursor -= n;
+               match();
+       }
 }
 
 static size_t
@@ -723,7 +738,7 @@ setup(void)
 static void
 usage(void)
 {
-       fputs("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
+       fputs("usage: dmenu [-bfirv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
              "             [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n", stderr);
        exit(1);
 }
@@ -783,7 +798,9 @@ main(int argc, char *argv[])
                else if (!strcmp(argv[i], "-i")) { /* case-insensitive item matching */
                        fstrncmp = strncasecmp;
                        fstrstr = cistrstr;
-               } else if (i + 1 == argc)
+               } else if (!strcmp(argv[i], "-r")) /* reject input which results in no match */
+                       reject_no_match = 1;
+               else if (i + 1 == argc)
                        usage();
                /* these options take one argument */
                else if (!strcmp(argv[i], "-l"))   /* number of lines in vertical list */