dmenu-highpriority-4.9.diff (6020B)
1 From e9e1691a9b4ffa2db45cd5108b76b9a68610ba37 Mon Sep 17 00:00:00 2001 2 From: takase1121 <takase1121@outlook.com> 3 Date: Sun, 17 May 2020 07:05:32 +0800 4 Subject: [PATCH] Adds high-priority items. 5 6 This allows users to specify a list of high priority commands that should be 7 displayed before other matches with the following arrangement: 8 9 match -> prefix && high priority -> prefix -> substring 10 11 As you can see, high priority for substring matches is not implemented or will not 12 be implemented depending on needs. 13 --- 14 config.def.h | 1 + 15 dmenu.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++----- 16 2 files changed, 59 insertions(+), 6 deletions(-) 17 18 diff --git a/config.def.h b/config.def.h 19 index 1edb647..65e03fb 100644 20 --- a/config.def.h 21 +++ b/config.def.h 22 @@ -12,6 +12,7 @@ static const char *colors[SchemeLast][2] = { 23 [SchemeNorm] = { "#bbbbbb", "#222222" }, 24 [SchemeSel] = { "#eeeeee", "#005577" }, 25 [SchemeOut] = { "#000000", "#00ffff" }, 26 + [SchemeHp] = { "#bbbbbb", "#333333" } 27 }; 28 /* -l option; if nonzero, dmenu uses vertical list with given number of lines */ 29 static unsigned int lines = 0; 30 diff --git a/dmenu.c b/dmenu.c 31 index 6b8f51b..7bf4099 100644 32 --- a/dmenu.c 33 +++ b/dmenu.c 34 @@ -26,14 +26,16 @@ 35 #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad) 36 37 /* enums */ 38 -enum { SchemeNorm, SchemeSel, SchemeOut, SchemeLast }; /* color schemes */ 39 +enum { SchemeNorm, SchemeSel, SchemeHp, SchemeOut, SchemeLast }; /* color schemes */ 40 41 struct item { 42 char *text; 43 struct item *left, *right; 44 - int out; 45 + int out, hp; 46 }; 47 48 +static char **hpitems = NULL; 49 +static int hplength = 0; 50 static char text[BUFSIZ] = ""; 51 static char *embed; 52 static int bh, mw, mh; 53 @@ -58,6 +60,36 @@ static Clr *scheme[SchemeLast]; 54 static int (*fstrncmp)(const char *, const char *, size_t) = strncmp; 55 static char *(*fstrstr)(const char *, const char *) = strstr; 56 57 +static char** 58 +tokenize(char *source, const char *delim, int *llen) { 59 + int listlength = 0; 60 + char **list = malloc(1 * sizeof(char*)); 61 + char *token = strtok(source, delim); 62 + 63 + while (token) { 64 + if (!(list = realloc(list, sizeof(char*) * (listlength + 1)))) 65 + die("Unable to realloc %d bytes\n", sizeof(char*) * (listlength + 1)); 66 + if (!(list[listlength] = strdup(token))) 67 + die("Unable to strdup %d bytes\n", strlen(token) + 1); 68 + token = strtok(NULL, delim); 69 + listlength++; 70 + } 71 + 72 + *llen = listlength; 73 + return list; 74 +} 75 + 76 +static int 77 +arrayhas(char **list, int length, char *item) { 78 + for (int i = 0; i < length; i++) { 79 + int len1 = strlen(list[i]); 80 + int len2 = strlen(item); 81 + if (fstrncmp(list[i], item, len1 > len2 ? len2 : len1) == 0) 82 + return 1; 83 + } 84 + return 0; 85 +} 86 + 87 static void 88 appenditem(struct item *item, struct item **list, struct item **last) 89 { 90 @@ -118,6 +150,8 @@ drawitem(struct item *item, int x, int y, int w) 91 { 92 if (item == sel) 93 drw_setscheme(drw, scheme[SchemeSel]); 94 + else if (item->hp) 95 + drw_setscheme(drw, scheme[SchemeHp]); 96 else if (item->out) 97 drw_setscheme(drw, scheme[SchemeOut]); 98 else 99 @@ -219,7 +253,7 @@ match(void) 100 char buf[sizeof text], *s; 101 int i, tokc = 0; 102 size_t len, textsize; 103 - struct item *item, *lprefix, *lsubstr, *prefixend, *substrend; 104 + struct item *item, *lhpprefix, *lprefix, *lsubstr, *hpprefixend, *prefixend, *substrend; 105 106 strcpy(buf, text); 107 /* separate input text into tokens to be matched individually */ 108 @@ -228,7 +262,7 @@ match(void) 109 die("cannot realloc %u bytes:", tokn * sizeof *tokv); 110 len = tokc ? strlen(tokv[0]) : 0; 111 112 - matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL; 113 + matches = lhpprefix = lprefix = lsubstr = matchend = hpprefixend = prefixend = substrend = NULL; 114 textsize = strlen(text) + 1; 115 for (item = items; item && item->text; item++) { 116 for (i = 0; i < tokc; i++) 117 @@ -236,14 +270,24 @@ match(void) 118 break; 119 if (i != tokc) /* not all tokens match */ 120 continue; 121 - /* exact matches go first, then prefixes, then substrings */ 122 + /* exact matches go first, then prefixes with high priority, then prefixes, then substrings */ 123 if (!tokc || !fstrncmp(text, item->text, textsize)) 124 appenditem(item, &matches, &matchend); 125 + else if (item->hp && !fstrncmp(tokv[0], item->text, len)) 126 + appenditem(item, &lhpprefix, &hpprefixend); 127 else if (!fstrncmp(tokv[0], item->text, len)) 128 appenditem(item, &lprefix, &prefixend); 129 else 130 appenditem(item, &lsubstr, &substrend); 131 } 132 + if (lhpprefix) { 133 + if (matches) { 134 + matchend->right = lhpprefix; 135 + lhpprefix->left = matchend; 136 + } else 137 + matches = lhpprefix; 138 + matchend = hpprefixend; 139 + } 140 if (lprefix) { 141 if (matches) { 142 matchend->right = lprefix; 143 @@ -535,6 +579,7 @@ readstdin(void) 144 if (!(items[i].text = strdup(buf))) 145 die("cannot strdup %u bytes:", strlen(buf) + 1); 146 items[i].out = 0; 147 + items[i].hp = arrayhas(hpitems, hplength, items[i].text); 148 drw_font_getexts(drw->fonts, buf, strlen(buf), &tmpmax, NULL); 149 if (tmpmax > inputw) { 150 inputw = tmpmax; 151 @@ -683,7 +728,8 @@ static void 152 usage(void) 153 { 154 fputs("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n" 155 - " [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n", stderr); 156 + " [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n" 157 + " [-hb color] [-hf color] [-hp items]\n", stderr); 158 exit(1); 159 } 160 161 @@ -724,8 +770,14 @@ main(int argc, char *argv[]) 162 colors[SchemeSel][ColBg] = argv[++i]; 163 else if (!strcmp(argv[i], "-sf")) /* selected foreground color */ 164 colors[SchemeSel][ColFg] = argv[++i]; 165 + else if (!strcmp(argv[i], "-hb")) /* high priority background color */ 166 + colors[SchemeHp][ColBg] = argv[++i]; 167 + else if (!strcmp(argv[i], "-hf")) /* low priority background color */ 168 + colors[SchemeHp][ColFg] = argv[++i]; 169 else if (!strcmp(argv[i], "-w")) /* embedding window id */ 170 embed = argv[++i]; 171 + else if (!strcmp(argv[i], "-hp")) 172 + hpitems = tokenize(argv[++i], ",", &hplength); 173 else 174 usage(); 175 176 -- 177 2.26.2 178