[PATCH] Add flag to automatically print unambiguous query

From: Eric Pruitt <eric.pruitt_AT_gmail.com>
Date: Sun, 12 Apr 2015 11:41:42 -0700

- The "-a" flag has been added which causes dmenu to immediately print
  and exit if only one entry matches the current query.
---
 dmenu.1 |    4 ++++
 dmenu.c |    8 +++++++-
 2 files changed, 11 insertions(+), 1 deletion(-)
diff --git a/dmenu.1 b/dmenu.1
index 2897ab1..3b527c1 100644
--- a/dmenu.1
+++ b/dmenu.1
_AT_@ -41,6 +41,10 @@ which lists programs in the user's $PATH and runs the result in their $SHELL.
 .B \-b
 dmenu appears at the bottom of the screen.
 .TP
+.B \-a
+When only one entry matches the current query, print the matching value to
+stdout, and exit immediately.
+.TP
 .B \-f
 dmenu grabs the keyboard before reading stdin.  This is faster, but will lock up
 X until stdin reaches end\-of\-file.
diff --git a/dmenu.c b/dmenu.c
index 94c70de..89fd130 100644
--- a/dmenu.c
+++ b/dmenu.c
_AT_@ -55,6 +55,7 @@ static Item *prev, *curr, *next, *sel;
 static Window win;
 static XIC xic;
 static int mon = -1;
+static Bool print_if_unambiguous = False;
 
 #include "config.h"
 
_AT_@ -72,6 +73,8 @@ main(int argc, char *argv[]) {
 			puts("dmenu-"VERSION", © 2006-2014 dmenu engineers, see LICENSE for details");
 			exit(EXIT_SUCCESS);
 		}
+		else if(!strcmp(argv[i], "-a"))   /* print selection if query is unambiguous */
+			print_if_unambiguous = True;
 		else if(!strcmp(argv[i], "-b"))   /* appears at the bottom of the screen */
 			topbar = False;
 		else if(!strcmp(argv[i], "-f"))   /* grabs keyboard before reading stdin */
_AT_@ -302,6 +305,8 @@ keypress(XKeyEvent *ev) {
 	default:
 		if(!iscntrl(*buf))
 			insert(buf, len);
+		if(print_if_unambiguous && matches && !matches->right)
+			goto print_selection;
 		break;
 	case XK_Delete:
 		if(text[cursor] == '\0')
_AT_@ -367,6 +372,7 @@ keypress(XKeyEvent *ev) {
 		break;
 	case XK_Return:
 	case XK_KP_Enter:
+print_selection:
 		puts((sel && !(ev->state & ShiftMask)) ? sel->text : text);
 		if(!(ev->state & ControlMask))
 			exit(EXIT_SUCCESS);
_AT_@ -621,6 +627,6 @@ setup(void) {
 void
 usage(void) {
 	fputs("usage: dmenu [-b] [-f] [-i] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
-	      "             [-nb color] [-nf color] [-sb color] [-sf color] [-v]\n", stderr);
+	      "             [-nb color] [-nf color] [-sb color] [-sf color] [-v] [-a]\n", stderr);
 	exit(EXIT_FAILURE);
 }
-- 
1.7.10.4
--vtzGhvizbBRQ85DL--
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Sun Apr 12 2015 - 21:00:04 CEST