From 7fbf9575aff62301e17b7f0601080633ae2a8a34 Mon Sep 17 00:00:00 2001 From: Matt Briancon Date: Fri, 25 Sep 2020 22:13:38 -0400 Subject: [PATCH] -S: do not sort matches --- dmenu.1 | 3 +++ dmenu.c | 23 ++++++++++++++++------- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/dmenu.1 b/dmenu.1 index 323f93c..b6af611 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 \-S +dmenu does not sort menu items after matching. +.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..efe968c 100644 --- a/dmenu.c +++ b/dmenu.c @@ -1,6 +1,7 @@ /* See LICENSE file for copyright and license details. */ #include #include +#include #include #include #include @@ -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 bool sortmatches = true; static Atom clip, utf8; static Display *dpy; @@ -236,13 +238,18 @@ match(void) break; if (i != tokc) /* not all tokens match */ continue; - /* exact matches go first, then prefixes, then substrings */ - if (!tokc || !fstrncmp(text, item->text, textsize)) + + if (!sortmatches) appenditem(item, &matches, &matchend); - else if (!fstrncmp(tokv[0], item->text, len)) - appenditem(item, &lprefix, &prefixend); - else - appenditem(item, &lsubstr, &substrend); + else { + /* exact matches go first, then prefixes, then substrings */ + if (!tokc || !fstrncmp(text, item->text, textsize)) + appenditem(item, &matches, &matchend); + else if (!fstrncmp(tokv[0], item->text, len)) + appenditem(item, &lprefix, &prefixend); + else + appenditem(item, &lsubstr, &substrend); + } } if (lprefix) { if (matches) { @@ -689,7 +696,7 @@ setup(void) static void usage(void) { - fputs("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n" + fputs("usage: dmenu [-bfivS] [-l lines] [-p prompt] [-fn font] [-m monitor]\n" " [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n", stderr); exit(1); } @@ -709,6 +716,8 @@ main(int argc, char *argv[]) topbar = 0; else if (!strcmp(argv[i], "-f")) /* grabs keyboard before reading stdin */ fast = 1; + else if (!strcmp(argv[i], "-S")) /* do not sort matches */ + sortmatches = false; else if (!strcmp(argv[i], "-i")) { /* case-insensitive item matching */ fstrncmp = strncasecmp; fstrstr = cistrstr; -- 2.25.1