dmenu-printindex-5.0.diff (2374B)
1 From e5a1ce7fca66c1d991f41df421bb7f1a4a3a8c45 Mon Sep 17 00:00:00 2001 2 From: Jackson Abascal <jacksonabascal@gmail.com> 3 Date: Mon, 26 Apr 2021 15:39:29 -0400 4 Subject: [PATCH] gives dmenu the ability to print the index of matched text 5 instead of the text itself 6 7 --- 8 dmenu.1 | 3 +++ 9 dmenu.c | 13 +++++++++++-- 10 2 files changed, 14 insertions(+), 2 deletions(-) 11 12 diff --git a/dmenu.1 b/dmenu.1 13 index 323f93c..b3c5f13 100644 14 --- a/dmenu.1 15 +++ b/dmenu.1 16 @@ -47,6 +47,9 @@ is faster, but will lock up X until stdin reaches end\-of\-file. 17 .B \-i 18 dmenu matches menu items case insensitively. 19 .TP 20 +.B \-ix 21 +dmenu prints the index of matched text instead of the text itself. 22 +.TP 23 .BI \-l " lines" 24 dmenu lists items vertically, with the given number of lines. 25 .TP 26 diff --git a/dmenu.c b/dmenu.c 27 index 65f25ce..172a2b4 100644 28 --- a/dmenu.c 29 +++ b/dmenu.c 30 @@ -32,6 +32,7 @@ struct item { 31 char *text; 32 struct item *left, *right; 33 int out; 34 + int index; 35 }; 36 37 static char text[BUFSIZ] = ""; 38 @@ -44,6 +45,7 @@ static struct item *items = NULL; 39 static struct item *matches, *matchend; 40 static struct item *prev, *curr, *next, *sel; 41 static int mon = -1, screen; 42 +static int print_index = 0; 43 44 static Atom clip, utf8; 45 static Display *dpy; 46 @@ -464,7 +466,11 @@ insert: 47 break; 48 case XK_Return: 49 case XK_KP_Enter: 50 - puts((sel && !(ev->state & ShiftMask)) ? sel->text : text); 51 + if (print_index) 52 + printf("%d\n", (sel && !(ev->state & ShiftMask)) ? sel->index : -1); 53 + else 54 + puts((sel && !(ev->state & ShiftMask)) ? sel->text : text); 55 + 56 if (!(ev->state & ControlMask)) { 57 cleanup(); 58 exit(0); 59 @@ -535,6 +541,7 @@ readstdin(void) 60 if (!(items[i].text = strdup(buf))) 61 die("cannot strdup %u bytes:", strlen(buf) + 1); 62 items[i].out = 0; 63 + items[i].index = i; 64 drw_font_getexts(drw->fonts, buf, strlen(buf), &tmpmax, NULL); 65 if (tmpmax > inputw) { 66 inputw = tmpmax; 67 @@ -712,7 +719,9 @@ main(int argc, char *argv[]) 68 else if (!strcmp(argv[i], "-i")) { /* case-insensitive item matching */ 69 fstrncmp = strncasecmp; 70 fstrstr = cistrstr; 71 - } else if (i + 1 == argc) 72 + } else if (!strcmp(argv[i], "-ix")) /* adds ability to return index in list */ 73 + print_index = 1; 74 + else if (i + 1 == argc) 75 usage(); 76 /* these options take one argument */ 77 else if (!strcmp(argv[i], "-l")) /* number of lines in vertical list */ 78 -- 79 2.31.1 80