[PATCH] Make it possible to toggle multi-selection

From: Eric Pruitt <eric.pruitt_AT_gmail.com>
Date: Sun, 12 Apr 2015 15:15:28 -0700

- This patch adds the flags -t and -T that can be used to toggle
  multi-selection via Ctrl+Enter.
---
 dmenu.1 |    8 ++++++++
 dmenu.c |   17 ++++++++++++++---
 2 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/dmenu.1 b/dmenu.1
index 3b527c1..f98d0ff 100644
--- a/dmenu.1
+++ b/dmenu.1
_AT_@ -52,6 +52,14 @@ X until stdin reaches end\-of\-file.
 .B \-i
 dmenu matches menu items case insensitively.
 .TP
+.B \-t
+Allow the use of Ctrl+Enter to select multiple entries. When using Ctrl+Enter
+to select multiple entries, pressing Ctrl+C or Escape will not cause dmenu to
+exit with a non-zero status if at least one item was selected.
+.TP
+.B \-T
+Same as as -t but does not reset the user input after a selection.
+.TP
 .BI \-l " lines"
 dmenu lists items vertically, with the given number of lines.
 .TP
diff --git a/dmenu.c b/dmenu.c
index ef844b1..296d291 100644
--- a/dmenu.c
+++ b/dmenu.c
_AT_@ -17,6 +17,8 @@
                              * MAX(0, MIN((y)+(h),(r).y_org+(r).height) - MAX((y),(r).y_org)))
 #define MIN(a,b)              ((a) < (b) ? (a) : (b))
 #define MAX(a,b)              ((a) > (b) ? (a) : (b))
+#define MULTI_SELECT          1
+#define MULTI_SELECT_NO_RESET 2
 
 typedef struct Item Item;
 struct Item {
_AT_@ -56,6 +58,7 @@ static Window win;
 static XIC xic;
 static int mon = -1;
 static Bool print_if_unambiguous = False;
+static int multi_select_mode = 0;
 
 #include "config.h"
 
_AT_@ -83,6 +86,10 @@ main(int argc, char *argv[]) {
 			fstrncmp = strncasecmp;
 			fstrstr = cistrstr;
 		}
+		else if(!strcmp(argv[i], "-t"))   /* allow arbitrary number of selections */
+			multi_select_mode = MULTI_SELECT;
+		else if(!strcmp(argv[i], "-T"))   /* Same as -t but do not clear prompt */
+			multi_select_mode = MULTI_SELECT_NO_RESET;
 		else if(i+1 == argc)
 			usage();
 		/* these options take one argument */
_AT_@ -335,7 +342,7 @@ keypress(XKeyEvent *ev) {
 		sel = matchend;
 		break;
 	case XK_Escape:
-		exit(EXIT_FAILURE);
+		exit(sel->out ? EXIT_SUCCESS : EXIT_FAILURE);
 	case XK_Home:
 		if(sel == matches) {
 			cursor = 0;
_AT_@ -374,8 +381,12 @@ keypress(XKeyEvent *ev) {
 	case XK_KP_Enter:
 print_selection:
 		puts((sel && !(ev->state & ShiftMask)) ? sel->text : text);
-		if(!(ev->state & ControlMask))
+		if(!(ev->state & ControlMask) || !multi_select_mode)
 			exit(EXIT_SUCCESS);
+		if(multi_select_mode != MULTI_SELECT_NO_RESET) {
+			text[(cursor = 0)] = '\0';
+			match();
+		}
 		if(sel)
 			sel->out = True;
 		break;
_AT_@ -627,6 +638,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] [-a]\n", stderr);
+	      "             [-nb color] [-nf color] [-sb color] [-sf color] [-v] [-a] [-t] [-T]\n", stderr);
 	exit(EXIT_FAILURE);
 }
-- 
1.7.10.4
--x+6KMIRAuhnl3hBn--
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Mon Apr 13 2015 - 06:36:05 CEST