From e5a1ce7fca66c1d991f41df421bb7f1a4a3a8c45 Mon Sep 17 00:00:00 2001 From: Jackson Abascal Date: Mon, 26 Apr 2021 15:39:29 -0400 Subject: [PATCH] gives dmenu the ability to print the index of matched text instead of the text itself --- dmenu.1 | 3 +++ dmenu.c | 13 +++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/dmenu.1 b/dmenu.1 index 323f93c..b3c5f13 100644 --- a/dmenu.1 +++ b/dmenu.1 @@ -47,6 +47,9 @@ is faster, but will lock up X until stdin reaches end\-of\-file. .B \-i dmenu matches menu items case insensitively. .TP +.B \-ix +dmenu prints the index of matched text instead of the text itself. +.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..172a2b4 100644 --- a/dmenu.c +++ b/dmenu.c @@ -32,6 +32,7 @@ struct item { char *text; struct item *left, *right; int out; + int index; }; static char text[BUFSIZ] = ""; @@ -44,6 +45,7 @@ static struct item *items = NULL; static struct item *matches, *matchend; static struct item *prev, *curr, *next, *sel; static int mon = -1, screen; +static int print_index = 0; static Atom clip, utf8; static Display *dpy; @@ -464,7 +466,11 @@ insert: break; case XK_Return: case XK_KP_Enter: - puts((sel && !(ev->state & ShiftMask)) ? sel->text : text); + if (print_index) + printf("%d\n", (sel && !(ev->state & ShiftMask)) ? sel->index : -1); + else + puts((sel && !(ev->state & ShiftMask)) ? sel->text : text); + if (!(ev->state & ControlMask)) { cleanup(); exit(0); @@ -535,6 +541,7 @@ readstdin(void) if (!(items[i].text = strdup(buf))) die("cannot strdup %u bytes:", strlen(buf) + 1); items[i].out = 0; + items[i].index = i; drw_font_getexts(drw->fonts, buf, strlen(buf), &tmpmax, NULL); if (tmpmax > inputw) { inputw = tmpmax; @@ -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], "-ix")) /* adds ability to return index in list */ + print_index = 1; + else if (i + 1 == argc) usage(); /* these options take one argument */ else if (!strcmp(argv[i], "-l")) /* number of lines in vertical list */ -- 2.31.1