diff --git a/README.md b/README.md index e1bc066..2e1b0e6 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Refer to [https://tools.suckless.org/dmenu/](https://tools.suckless.org/dmenu/) ### Changelog: -2020-08-05 - Added the grid, highlight, dynamic options and numbers patches +2020-08-05 - Added the grid, highlight, highpriority, dynamic options and numbers patches 2020-06-13 - Added the pango patch @@ -67,6 +67,9 @@ Refer to [https://tools.suckless.org/dmenu/](https://tools.suckless.org/dmenu/) - [highlight](https://tools.suckless.org/dmenu/patches/highlight/) - this patch highlights the individual characters of matched text for each dmenu list entry + - [highpriority](https://tools.suckless.org/dmenu/patches/highpriority/) + - this patch will automatically sort the search result so that high priority items are shown first + - [incremental](https://tools.suckless.org/dmenu/patches/incremental/) - this patch causes dmenu to print out the current text each time a key is pressed diff --git a/config.def.h b/config.def.h index 2ad816f..ce9ccfc 100644 --- a/config.def.h +++ b/config.def.h @@ -60,6 +60,9 @@ static const char *colors[][2] = [SchemeSelHighlight] = { "#ffc978", "#005577" }, [SchemeNormHighlight] = { "#ffc978", "#222222" }, #endif // HIGHLIGHT_PATCH | FUZZYHIGHLIGHT_PATCH + #if HIGHPRIORITY_PATCH + [SchemeHp] = { "#bbbbbb", "#333333" }, + #endif // HIGHPRIORITY_PATCH }; /* -l option; if nonzero, dmenu uses vertical list with given number of lines */ static unsigned int lines = 0; diff --git a/dmenu.c b/dmenu.c index d8998ec..f233bcb 100644 --- a/dmenu.c +++ b/dmenu.c @@ -44,6 +44,9 @@ enum { SchemeNormHighlight, SchemeSelHighlight, #endif // HIGHLIGHT_PATCH || FUZZYHIGHLIGHT_PATCH + #if HIGHPRIORITY_PATCH + SchemeHp, + #endif // HIGHPRIORITY_PATCH SchemeLast, }; /* color schemes */ @@ -54,6 +57,9 @@ struct item { struct item *next; #endif // NON_BLOCKING_STDIN_PATCH int out; + #if HIGHPRIORITY_PATCH + int hp; + #endif // HIGHPRIORITY_PATCH #if FUZZYMATCH_PATCH double distance; #endif // FUZZYMATCH_PATCH @@ -217,6 +223,10 @@ drawitem(struct item *item, int x, int y, int w) int r; if (item == sel) drw_setscheme(drw, scheme[SchemeSel]); + #if HIGHPRIORITY_PATCH + else if (item->hp) + drw_setscheme(drw, scheme[SchemeHp]); + #endif // HIGHPRIORITY_PATCH else if (item->out) drw_setscheme(drw, scheme[SchemeOut]); else @@ -470,6 +480,9 @@ match(void) int i, tokc = 0; size_t len, textsize; struct item *item, *lprefix, *lsubstr, *prefixend, *substrend; + #if HIGHPRIORITY_PATCH + struct item *lhpprefix, *hpprefixend; + #endif // HIGHPRIORITY_PATCH #if NON_BLOCKING_STDIN_PATCH int preserve = 0; #endif // NON_BLOCKING_STDIN_PATCH @@ -493,6 +506,9 @@ match(void) matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL; textsize = strlen(text) + 1; #endif // PREFIXCOMPLETION_PATCH + #if HIGHPRIORITY_PATCH + lhpprefix = hpprefixend = NULL; + #endif // HIGHPRIORITY_PATCH #if NON_BLOCKING_STDIN_PATCH for (item = items; item; item = item->next) #else @@ -509,9 +525,17 @@ match(void) if (i != tokc) /* not all tokens match */ continue; #endif // DYNAMIC_OPTIONS_PATCH + #if HIGHPRIORITY_PATCH + /* exact matches go first, then prefixes with high priority, then prefixes, then substrings */ + #else /* exact matches go first, then prefixes, then substrings */ + #endif // HIGHPRIORITY_PATCH if (!tokc || !fstrncmp(text, item->text, textsize)) appenditem(item, &matches, &matchend); + #if HIGHPRIORITY_PATCH + else if (item->hp && !fstrncmp(tokv[0], item->text, len)) + appenditem(item, &lhpprefix, &hpprefixend); + #endif // HIGHPRIORITY_PATCH else if (!fstrncmp(tokv[0], item->text, len)) appenditem(item, &lprefix, &prefixend); #if PREFIXCOMPLETION_PATCH @@ -525,6 +549,16 @@ match(void) preserve = 1; #endif // NON_BLOCKING_STDIN_PATCH } + #if HIGHPRIORITY_PATCH + if (lhpprefix) { + if (matches) { + matchend->right = lhpprefix; + lhpprefix->left = matchend; + } else + matches = lhpprefix; + matchend = hpprefixend; + } + #endif // HIGHPRIORITY_PATCH if (lprefix) { if (matches) { matchend->right = lprefix; @@ -958,6 +992,9 @@ readstdin(void) if (!(items[i].text = strdup(buf))) die("cannot strdup %u bytes:", strlen(buf) + 1); items[i].out = 0; + #if HIGHPRIORITY_PATCH + items[i].hp = arrayhas(hpitems, hplength, items[i].text); + #endif // HIGHPRIORITY_PATCH #if PANGO_PATCH drw_font_getexts(drw->font, buf, strlen(buf), &tmpmax, NULL, True); #else @@ -1278,7 +1315,7 @@ usage(void) #endif // GRID_PATCH "[-l lines] [-p prompt] [-fn font] [-m monitor]" "\n [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]" - #if ALPHA_PATCH || BORDER_PATCH || INITIALTEXT_PATCH || LINE_HEIGHT_PATCH || NAVHISTORY_PATCH || XYW_PATCH || DYNAMIC_OPTIONS_PATCH + #if ALPHA_PATCH || BORDER_PATCH || HIGHPRIORITY_PATCH || INITIALTEXT_PATCH || LINE_HEIGHT_PATCH || NAVHISTORY_PATCH || XYW_PATCH || DYNAMIC_OPTIONS_PATCH "\n " #endif #if DYNAMIC_OPTIONS_PATCH @@ -1290,6 +1327,9 @@ usage(void) #if BORDER_PATCH " [-bw width]" #endif // BORDER_PATCH + #if HIGHPRIORITY_PATCH + " [-hb color] [-hf color] [-hp items]" + #endif // HIGHPRIORITY_PATCH #if INITIALTEXT_PATCH " [-it text]" #endif // INITIALTEXT_PATCH @@ -1422,6 +1462,14 @@ main(int argc, char *argv[]) colors[SchemeSel][ColBg] = argv[++i]; else if (!strcmp(argv[i], "-sf")) /* selected foreground color */ colors[SchemeSel][ColFg] = argv[++i]; + #if HIGHPRIORITY_PATCH + 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], "-hp")) + hpitems = tokenize(argv[++i], ",", &hplength); + #endif // HIGHPRIORITY_PATCH #if HIGHLIGHT_PATCH || FUZZYHIGHLIGHT_PATCH else if (!strcmp(argv[i], "-nhb")) /* normal hi background color */ colors[SchemeNormHighlight][ColBg] = argv[++i]; diff --git a/patch/dynamicoptions.c b/patch/dynamicoptions.c index 71ddd18..82c3e5a 100644 --- a/patch/dynamicoptions.c +++ b/patch/dynamicoptions.c @@ -34,6 +34,9 @@ readstream(FILE* stream) if (!(items[i].text = strdup(buf))) die("cannot strdup %u bytes:", strlen(buf) + 1); items[i].out = 0; + #if HIGHPRIORITY_PATCH + items[i].hp = arrayhas(hpitems, hplength, items[i].text); + #endif // HIGHPRIORITY_PATCH #if PANGO_PATCH drw_font_getexts(drw->font, buf, strlen(buf), &tmpmax, NULL, True); #else diff --git a/patch/highpriority.c b/patch/highpriority.c new file mode 100644 index 0000000..568c705 --- /dev/null +++ b/patch/highpriority.c @@ -0,0 +1,32 @@ +static char **hpitems = NULL; +static int hplength = 0; + +static char** +tokenize(char *source, const char *delim, int *llen) { + int listlength = 0; + char **list = malloc(1 * sizeof(char*)); + char *token = strtok(source, delim); + + while (token) { + if (!(list = realloc(list, sizeof(char*) * (listlength + 1)))) + die("Unable to realloc %d bytes\n", sizeof(char*) * (listlength + 1)); + if (!(list[listlength] = strdup(token))) + die("Unable to strdup %d bytes\n", strlen(token) + 1); + token = strtok(NULL, delim); + listlength++; + } + + *llen = listlength; + return list; +} + +static int +arrayhas(char **list, int length, char *item) { + for (int i = 0; i < length; i++) { + int len1 = strlen(list[i]); + int len2 = strlen(item); + if (fstrncmp(list[i], item, len1 > len2 ? len2 : len1) == 0) + return 1; + } + return 0; +} \ No newline at end of file diff --git a/patch/include.c b/patch/include.c index 1b38790..65576a6 100644 --- a/patch/include.c +++ b/patch/include.c @@ -12,6 +12,9 @@ #if FUZZYMATCH_PATCH #include "fuzzymatch.c" #endif +#if HIGHPRIORITY_PATCH +#include "highpriority.c" +#endif #if MOUSE_SUPPORT_PATCH #include "mousesupport.c" #endif diff --git a/patches.def.h b/patches.def.h index 75448e2..758f5e2 100644 --- a/patches.def.h +++ b/patches.def.h @@ -63,6 +63,11 @@ */ #define HIGHLIGHT_PATCH 0 +/* This will automatically sort the search result so that high priority items are shown first. + * https://tools.suckless.org/dmenu/patches/highpriority/ + */ +#define HIGHPRIORITY_PATCH 0 + /* This patch causes dmenu to print out the current text each time a key is pressed. * https://tools.suckless.org/dmenu/patches/incremental/ */