restrict return patch

This commit is contained in:
Jakub 2023-01-04 21:26:44 -05:00
parent 2db2a8113f
commit 77b329af23
4 changed files with 81 additions and 2 deletions

View File

@ -2,6 +2,7 @@
/* Default settings; can be overriden by command line. */ /* Default settings; can be overriden by command line. */
static int topbar = 1; /* -b option; if 0, dmenu appears at bottom */ static int topbar = 1; /* -b option; if 0, dmenu appears at bottom */
static int restrict_return = 0; /* -r option; if 1, disables shift-return and ctrl-return */
static int fuzzy = 1; /* -F option; if 0, dmenu doesn't use fuzzy matching */ static int fuzzy = 1; /* -F option; if 0, dmenu doesn't use fuzzy matching */
/* -fn option overrides fonts[0]; default X11 font or font set */ /* -fn option overrides fonts[0]; default X11 font or font set */
static int centered = 0; /* -c option; centers dmenu on screen */ static int centered = 0; /* -c option; centers dmenu on screen */

View File

@ -3,7 +3,7 @@
dmenu \- dynamic menu dmenu \- dynamic menu
.SH SYNOPSIS .SH SYNOPSIS
.B dmenu .B dmenu
.RB [ \-bfsrv ] .RB [ \-bfsrRv ]
.RB [ \-l .RB [ \-l
.IR lines ] .IR lines ]
.RB [ \-h .RB [ \-h
@ -55,6 +55,10 @@ dmenu matches menu items case sensitively.
.B \-r .B \-r
dmenu will reject any input which would result in no matching option left. dmenu will reject any input which would result in no matching option left.
.TP .TP
.B \-R
disables ctr-return and shift-return. this guarantees that dmenu prints to
stdout only once, and it only prints an item read from stdin.
.TP
.BI \-l " lines" .BI \-l " lines"
dmenu lists items vertically, with the given number of lines. dmenu lists items vertically, with the given number of lines.
.TP .TP

11
dmenu.c
View File

@ -611,6 +611,13 @@ insert:
break; break;
case XK_Return: case XK_Return:
case XK_KP_Enter: case XK_KP_Enter:
if (restrict_return) {
if (!sel || ev->state & (ShiftMask | ControlMask))
break;
puts(sel->text);
cleanup();
exit(0);
}
puts((sel && !(ev->state & ShiftMask)) ? sel->text : text); puts((sel && !(ev->state & ShiftMask)) ? sel->text : text);
if (!(ev->state & ControlMask)) { if (!(ev->state & ControlMask)) {
cleanup(); cleanup();
@ -848,7 +855,7 @@ setup(void)
static void static void
usage(void) usage(void)
{ {
die("usage: dmenu [-bfirv] [-l lines] [-h height] [-p prompt] [-fn font] [-m monitor]\n" die("usage: dmenu [-bfirRv] [-l lines] [-h height] [-p prompt] [-fn font] [-m monitor]\n"
" [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]"); " [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]");
} }
@ -876,6 +883,8 @@ main(int argc, char *argv[])
fstrstr = strstr; fstrstr = strstr;
} else if (!strcmp(argv[i], "-r")) /* reject input which results in no match */ } else if (!strcmp(argv[i], "-r")) /* reject input which results in no match */
reject_no_match = 1; reject_no_match = 1;
else if (!strcmp(argv[i], "-R"))
restrict_return = 1;
else if (i + 1 == argc) else if (i + 1 == argc)
usage(); usage();
/* these options take one argument */ /* these options take one argument */

View File

@ -0,0 +1,65 @@
diff --git a/config.def.h b/config.def.h
index 1edb647..47c2f76 100644
--- a/config.def.h
+++ b/config.def.h
@@ -2,6 +2,7 @@
/* Default settings; can be overriden by command line. */
static int topbar = 1; /* -b option; if 0, dmenu appears at bottom */
+static int restrict_return = 0; /* -r option; if 1, disables shift-return and ctrl-return */
/* -fn option overrides fonts[0]; default X11 font or font set */
static const char *fonts[] = {
"monospace:size=10"
diff --git a/dmenu.1 b/dmenu.1
index 323f93c..0dcafce 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,10 @@ is faster, but will lock up X until stdin reaches end\-of\-file.
.B \-i
dmenu matches menu items case insensitively.
.TP
+.B \-r
+disables ctr-return and shift-return. this guarantees that dmenu prints to
+stdout only once, and it only prints an item read from stdin.
+.TP
.BI \-l " lines"
dmenu lists items vertically, with the given number of lines.
.TP
diff --git a/dmenu.c b/dmenu.c
index 65f25ce..a278680 100644
--- a/dmenu.c
+++ b/dmenu.c
@@ -464,6 +464,13 @@ insert:
break;
case XK_Return:
case XK_KP_Enter:
+ if (restrict_return) {
+ if (!sel || ev->state & (ShiftMask | ControlMask))
+ break;
+ puts(sel->text);
+ cleanup();
+ exit(0);
+ }
puts((sel && !(ev->state & ShiftMask)) ? sel->text : text);
if (!(ev->state & ControlMask)) {
cleanup();
@@ -712,7 +719,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"))
+ restrict_return = 1;
+ else if (i + 1 == argc)
usage();
/* these options take one argument */
else if (!strcmp(argv[i], "-l")) /* number of lines in vertical list */