summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzachir <zachir@librem.one>2023-02-21 14:12:54 -0600
committerzachir <zachir@librem.one>2023-02-21 14:12:54 -0600
commitf5e21c71896573cccd82fdb1a29cfb9a76818a87 (patch)
treeb2628e21444faa81ffe30998e5643e2165c43889
parentefffd811301f69ee1e91183c2356ccc8f5115c02 (diff)
add rejectnomatch patch
-rw-r--r--dmenu.15
-rw-r--r--dmenu.c21
-rw-r--r--patches/dmenu-rejectnomatch-4.7.diff82
3 files changed, 105 insertions, 3 deletions
diff --git a/dmenu.1 b/dmenu.1
index 3e72fee..ec120c8 100644
--- a/dmenu.1
+++ b/dmenu.1
@@ -3,7 +3,7 @@
dmenu \- dynamic menu
.SH SYNOPSIS
.B dmenu
-.RB [ \-bfivP ]
+.RB [ \-bfirvP ]
.RB [ \-l
.IR lines ]
.RB [ \-h
@@ -67,6 +67,9 @@ dmenu matches menu items case insensitively.
.B \-P
dmenu will not directly display the keyboard input, but instead replace it with dots. All data from stdin will be ignored.
.TP
+.B \-r
+dmenu will reject any input which would result in no matching option left.
+.TP
.BI \-l " lines"
dmenu lists items vertically, with the given number of lines.
.TP
diff --git a/dmenu.c b/dmenu.c
index f698d44..442ca3d 100644
--- a/dmenu.c
+++ b/dmenu.c
@@ -46,6 +46,7 @@ static char * (*sepchr)(const char *, int);
static int bh, mw, mh;
static int inputw = 0, promptw, passwd = 0;
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;
@@ -433,12 +434,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
@@ -1010,7 +1025,7 @@ setup(void)
static void
usage(void)
{
- die("usage: dmenu [-bfivP] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
+ die("usage: dmenu [-bfirvP] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
" [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n"
" [-nhb color] [-nhf color] [-shb color] [-shf color]\n"
" [-d separator] [-D separator] [-h height]");
@@ -1038,8 +1053,10 @@ main(int argc, char *argv[])
else if (!strcmp(argv[i], "-i")) { /* case-insensitive item matching */
fstrncmp = strncasecmp;
fstrstr = cistrstr;
- } else if (!strcmp(argv[i], "-P")) /* is the input a password */
+ } else if (!strcmp(argv[i], "-P")) /* is the input a password */
passwd = 1;
+ 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 */
diff --git a/patches/dmenu-rejectnomatch-4.7.diff b/patches/dmenu-rejectnomatch-4.7.diff
new file mode 100644
index 0000000..329ab1d
--- /dev/null
+++ b/patches/dmenu-rejectnomatch-4.7.diff
@@ -0,0 +1,82 @@
+diff --git a/dmenu.1 b/dmenu.1
+index 9eab758..61084ab 100644
+--- a/dmenu.1
++++ b/dmenu.1
+@@ -3,7 +3,7 @@
+ dmenu \- dynamic menu
+ .SH SYNOPSIS
+ .B dmenu
+-.RB [ \-bfiv ]
++.RB [ \-bfirv ]
+ .RB [ \-l
+ .IR lines ]
+ .RB [ \-m
+@@ -47,6 +47,9 @@ X until stdin reaches end\-of\-file.
+ .B \-i
+ dmenu matches menu items case insensitively.
+ .TP
++.B \-r
++dmenu will reject any input which would result in no matching option left.
++.TP
+ .BI \-l " lines"
+ dmenu lists items vertically, with the given number of lines.
+ .TP
+diff --git a/dmenu.c b/dmenu.c
+index d605ab4..7505278 100644
+--- a/dmenu.c
++++ b/dmenu.c
+@@ -38,6 +38,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;
+@@ -268,12 +269,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
+@@ -636,7 +651,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);
+ }
+@@ -659,7 +674,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 */