diff options
-rw-r--r-- | config.def.h | 1 | ||||
-rw-r--r-- | dmenu.c | 75 |
2 files changed, 69 insertions, 7 deletions
diff --git a/config.def.h b/config.def.h index 1edb647..47b616d 100644 --- a/config.def.h +++ b/config.def.h @@ -12,6 +12,7 @@ static const char *colors[SchemeLast][2] = { [SchemeNorm] = { "#bbbbbb", "#222222" }, [SchemeSel] = { "#eeeeee", "#005577" }, [SchemeOut] = { "#000000", "#00ffff" }, + [SchemeHp] = { "#bbbbbb", "#333333" } }; /* -l option; if nonzero, dmenu uses vertical list with given number of lines */ static unsigned int lines = 0; @@ -27,14 +27,16 @@ #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad) /* enums */ -enum { SchemeNorm, SchemeSel, SchemeOut, SchemeLast }; /* color schemes */ +enum { SchemeNorm, SchemeSel, SchemeHp, SchemeOut, SchemeLast }; /* color schemes */ struct item { char *text; struct item *left, *right; - int out; + int out, hp; }; +static char **hpitems = NULL; +static int hplength = 0; static char text[BUFSIZ] = ""; static char *embed; static int bh, mw, mh; @@ -70,6 +72,42 @@ textw_clamp(const char *str, unsigned int n) return MIN(w, n); } +static char ** +tokenize(char *source, const char *delim, int *llen) +{ + int listlength = 0, list_size = 0; + char **list = NULL, *token; + + token = strtok(source, delim); + while (token) { + if (listlength + 1 >= list_size) { + if (!(list = realloc(list, (list_size += 8) * sizeof(*list)))) + die("Unable to realloc %zu bytes\n", list_size * sizeof(*list)); + } + if (!(list[listlength] = strdup(token))) + die("Unable to strdup %zu bytes\n", strlen(token) + 1); + token = strtok(NULL, delim); + listlength++; + } + + *llen = listlength; + return list; +} + +static int +arrayhas(char **list, int length, char *item) +{ + int i; + + for (i = 0; i < length; i++) { + size_t len1 = strlen(list[i]); + size_t len2 = strlen(item); + if (!fstrncmp(list[i], item, len1 > len2 ? len2 : len1)) + return 1; + } + return 0; +} + static void appenditem(struct item *item, struct item **list, struct item **last) { @@ -111,7 +149,10 @@ cleanup(void) free(scheme[i]); for (i = 0; items && items[i].text; ++i) free(items[i].text); - free(items); + free(items); + for (i = 0; i < hplength; ++i) + free(hpitems[i]); + free(hpitems); drw_free(drw); XSync(dpy, False); XCloseDisplay(dpy); @@ -140,6 +181,8 @@ drawitem(struct item *item, int x, int y, int w) { if (item == sel) drw_setscheme(drw, scheme[SchemeSel]); + else if (item->hp) + drw_setscheme(drw, scheme[SchemeHp]); else if (item->out) drw_setscheme(drw, scheme[SchemeOut]); else @@ -247,7 +290,7 @@ match(void) char buf[sizeof text], *s; int i, tokc = 0; size_t len, textsize; - struct item *item, *lprefix, *lsubstr, *prefixend, *substrend; + struct item *item, *lhpprefix, *lprefix, *lsubstr, *hpprefixend, *prefixend, *substrend; strcpy(buf, text); /* separate input text into tokens to be matched individually */ @@ -256,7 +299,7 @@ match(void) die("cannot realloc %zu bytes:", tokn * sizeof *tokv); len = tokc ? strlen(tokv[0]) : 0; - matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL; + matches = lhpprefix = lprefix = lsubstr = matchend = hpprefixend = prefixend = substrend = NULL; textsize = strlen(text) + 1; for (item = items; item && item->text; item++) { for (i = 0; i < tokc; i++) @@ -264,14 +307,24 @@ match(void) break; if (i != tokc) /* not all tokens match */ continue; - /* exact matches go first, then prefixes, then substrings */ + /* exact matches go first, then prefixes with high priority, then prefixes, then substrings */ if (!tokc || !fstrncmp(text, item->text, textsize)) appenditem(item, &matches, &matchend); + else if (item->hp && !fstrncmp(tokv[0], item->text, len)) + appenditem(item, &lhpprefix, &hpprefixend); else if (!fstrncmp(tokv[0], item->text, len)) appenditem(item, &lprefix, &prefixend); else appenditem(item, &lsubstr, &substrend); } + if (lhpprefix) { + if (matches) { + matchend->right = lhpprefix; + lhpprefix->left = matchend; + } else + matches = lhpprefix; + matchend = hpprefixend; + } if (lprefix) { if (matches) { matchend->right = lprefix; @@ -577,6 +630,7 @@ readstdin(void) line[len - 1] = '\0'; items[i].text = line; items[i].out = 0; + items[i].hp = arrayhas(hpitems, hplength, items[i].text); } if (items) items[i].text = NULL; @@ -731,7 +785,8 @@ static void usage(void) { die("usage: dmenu [-bfivP] [-l lines] [-p prompt] [-fn font] [-m monitor]\n" - " [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]"); + " [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n" + " [-hb color] [-hf color] [-hp items]\n", stderr); } void @@ -826,8 +881,14 @@ main(int argc, char *argv[]) colortemp[2] = argv[++i]; else if (!strcmp(argv[i], "-sf")) /* selected foreground color */ colortemp[3] = argv[++i]; + else if (!strcmp(argv[i], "-hb")) /* high priority background color */ + colors[SchemeHp][ColBg] = argv[++i]; + else if (!strcmp(argv[i], "-hf")) /* low priority background color */ + colors[SchemeHp][ColFg] = argv[++i]; else if (!strcmp(argv[i], "-w")) /* embedding window id */ embed = argv[++i]; + else if (!strcmp(argv[i], "-hp")) + hpitems = tokenize(argv[++i], ",", &hplength); else usage(); |