1 From a06d0d3d7bbb3c0f5bad44934dbbf1e88e7d9558 Mon Sep 17 00:00:00 2001 2 From: Miles Alan <m@milesalan.com> 3 Date: Sat, 4 Jul 2020 11:49:04 -0500 4 Subject: [PATCH] Highlight matched text in a different color scheme 5 6 --- 7 config.def.h | 2 ++ 8 dmenu.c | 43 +++++++++++++++++++++++++++++++++++++++++-- 9 2 files changed, 43 insertions(+), 2 deletions(-) 10 11 diff --git a/config.def.h b/config.def.h 12 index 1edb647..64eab2a 100644 13 --- a/config.def.h 14 +++ b/config.def.h 15 @@ -11,6 +11,8 @@ static const char *colors[SchemeLast][2] = { 16 /* fg bg */ 17 [SchemeNorm] = { "#bbbbbb", "#222222" }, 18 [SchemeSel] = { "#eeeeee", "#005577" }, 19 + [SchemeSelHighlight] = { "#ffc978", "#005577" }, 20 + [SchemeNormHighlight] = { "#ffc978", "#222222" }, 21 [SchemeOut] = { "#000000", "#00ffff" }, 22 }; 23 /* -l option; if nonzero, dmenu uses vertical list with given number of lines */ 24 diff --git a/dmenu.c b/dmenu.c 25 index 6b8f51b..d5e1991 100644 26 --- a/dmenu.c 27 +++ b/dmenu.c 28 @@ -26,7 +26,7 @@ 29 #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad) 30 31 /* enums */ 32 -enum { SchemeNorm, SchemeSel, SchemeOut, SchemeLast }; /* color schemes */ 33 +enum { SchemeNorm, SchemeSel, SchemeOut, SchemeNormHighlight, SchemeSelHighlight, SchemeLast }; /* color schemes */ 34 35 struct item { 36 char *text; 37 @@ -113,6 +113,43 @@ cistrstr(const char *s, const char *sub) 38 return NULL; 39 } 40 41 +static void 42 +drawhighlights(struct item *item, int x, int y, int maxw) 43 +{ 44 + char restorechar, tokens[sizeof text], *highlight, *token; 45 + int indentx, highlightlen; 46 + 47 + drw_setscheme(drw, scheme[item == sel ? SchemeSelHighlight : SchemeNormHighlight]); 48 + strcpy(tokens, text); 49 + for (token = strtok(tokens, " "); token; token = strtok(NULL, " ")) { 50 + highlight = fstrstr(item->text, token); 51 + while (highlight) { 52 + // Move item str end, calc width for highlight indent, & restore 53 + highlightlen = highlight - item->text; 54 + restorechar = *highlight; 55 + item->text[highlightlen] = '\0'; 56 + indentx = TEXTW(item->text); 57 + item->text[highlightlen] = restorechar; 58 + 59 + // Move highlight str end, draw highlight, & restore 60 + restorechar = highlight[strlen(token)]; 61 + highlight[strlen(token)] = '\0'; 62 + if (indentx - (lrpad / 2) - 1 < maxw) 63 + drw_text( 64 + drw, 65 + x + indentx - (lrpad / 2) - 1, 66 + y, 67 + MIN(maxw - indentx, TEXTW(highlight) - lrpad), 68 + bh, 0, highlight, 0 69 + ); 70 + highlight[strlen(token)] = restorechar; 71 + 72 + if (strlen(highlight) - strlen(token) < strlen(token)) break; 73 + highlight = fstrstr(highlight + strlen(token), token); 74 + } 75 + } 76 +} 77 + 78 static int 79 drawitem(struct item *item, int x, int y, int w) 80 { 81 @@ -123,7 +160,9 @@ drawitem(struct item *item, int x, int y, int w) 82 else 83 drw_setscheme(drw, scheme[SchemeNorm]); 84 85 - return drw_text(drw, x, y, w, bh, lrpad / 2, item->text, 0); 86 + int r = drw_text(drw, x, y, w, bh, lrpad / 2, item->text, 0); 87 + drawhighlights(item, x, y, w); 88 + return r; 89 } 90 91 static void 92 -- 93 2.23.1 94