From fcfde08faf4d96b2edc1fff76b3665fa926f6d58 Mon Sep 17 00:00:00 2001 From: bakkeby Date: Sat, 4 Apr 2020 09:58:35 +0200 Subject: [PATCH] Adding fuzzyhighlight patch --- README.md | 5 +++++ config.def.h | 4 ++++ dmenu.c | 33 ++++++++++++++++++++++++++++++++- patch/fuzzyhighlight.c | 37 +++++++++++++++++++++++++++++++++++++ patch/include.c | 3 +++ patches.def.h | 6 ++++++ 6 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 patch/fuzzyhighlight.c diff --git a/README.md b/README.md index 2edaeb4..ba9564a 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,8 @@ Refer to [https://tools.suckless.org/dmenu/](https://tools.suckless.org/dmenu/) ### Changelog: +2020-04-05 - Added fuzzyhighlight patch + 2020-02-09 - Added revised border patch (adding command line parameter for setting border width) 2019-12-29 - Added xresources patch @@ -31,6 +33,9 @@ Refer to [https://tools.suckless.org/dmenu/](https://tools.suckless.org/dmenu/) - [center](https://tools.suckless.org/dmenu/patches/center/) - this patch centers dmenu in the middle of the screen + - [fuzzyhighlight](https://tools.suckless.org/dmenu/patches/fuzzyhighlight/) + - intended to be combined with the fuzzymatch patch, this makes it so that fuzzy matches are highlighted + - [fuzzymatch](https://tools.suckless.org/dmenu/patches/fuzzymatch/) - adds support for fuzzy-matching to dmenu, allowing users to type non-consecutive portions of the string to be matched diff --git a/config.def.h b/config.def.h index 037632e..4c9b0d1 100644 --- a/config.def.h +++ b/config.def.h @@ -35,6 +35,10 @@ static const char *colors[][2] = [SchemeNorm] = { "#bbbbbb", "#222222" }, [SchemeSel] = { "#eeeeee", "#005577" }, [SchemeOut] = { "#000000", "#00ffff" }, + #if FUZZYHIGHLIGHT_PATCH + [SchemeSelHighlight] = { "#ffc978", "#005577" }, + [SchemeNormHighlight] = { "#ffc978", "#222222" }, + #endif // FUZZYHIGHLIGHT_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 d469eea..6ae4416 100644 --- a/dmenu.c +++ b/dmenu.c @@ -27,7 +27,16 @@ #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad) /* enums */ -enum { SchemeNorm, SchemeSel, SchemeOut, SchemeLast }; /* color schemes */ +enum { + SchemeNorm, + SchemeSel, + SchemeOut, + #if FUZZYHIGHLIGHT_PATCH + SchemeNormHighlight, + SchemeSelHighlight, + #endif // FUZZYHIGHLIGHT_PATCH + SchemeLast, +}; /* color schemes */ struct item { char *text; @@ -165,6 +174,9 @@ cistrstr(const char *s, const char *sub) static int drawitem(struct item *item, int x, int y, int w) { + #if FUZZYHIGHLIGHT_PATCH + int r; + #endif // FUZZYHIGHLIGHT_PATCH if (item == sel) drw_setscheme(drw, scheme[SchemeSel]); else if (item->out) @@ -172,7 +184,13 @@ drawitem(struct item *item, int x, int y, int w) else drw_setscheme(drw, scheme[SchemeNorm]); + #if FUZZYHIGHLIGHT_PATCH + r = drw_text(drw, x, y, w, bh, lrpad / 2, item->text, 0); + drawhighlights(item, x, y, w); + return r; + #else return drw_text(drw, x, y, w, bh, lrpad / 2, item->text, 0); + #endif // FUZZYHIGHLIGHT_PATCH } static void @@ -1078,6 +1096,9 @@ usage(void) #if XYW_PATCH " [-X xoffset] [-Y yoffset] [-W width]" // (arguments made upper case due to conflicts) #endif // XYW_PATCH + #if FUZZYHIGHLIGHT_PATCH + "\n [-nhb color] [-nhf color] [-shb color] [-shf color]" // highlight colors + #endif // FUZZYHIGHLIGHT_PATCH "\n", stderr); exit(1); } @@ -1174,6 +1195,16 @@ 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 FUZZYHIGHLIGHT_PATCH + else if (!strcmp(argv[i], "-nhb")) /* normal hi background color */ + colors[SchemeNormHighlight][ColBg] = argv[++i]; + else if (!strcmp(argv[i], "-nhf")) /* normal hi foreground color */ + colors[SchemeNormHighlight][ColFg] = argv[++i]; + else if (!strcmp(argv[i], "-shb")) /* selected hi background color */ + colors[SchemeSelHighlight][ColBg] = argv[++i]; + else if (!strcmp(argv[i], "-shf")) /* selected hi foreground color */ + colors[SchemeSelHighlight][ColFg] = argv[++i]; + #endif // FUZZYHIGHLIGHT_PATCH else if (!strcmp(argv[i], "-w")) /* embedding window id */ embed = argv[++i]; #if BORDER_PATCH diff --git a/patch/fuzzyhighlight.c b/patch/fuzzyhighlight.c new file mode 100644 index 0000000..4295dc8 --- /dev/null +++ b/patch/fuzzyhighlight.c @@ -0,0 +1,37 @@ +static void +drawhighlights(struct item *item, int x, int y, int maxw) +{ + int i, indent; + char *highlight; + char c; + + if (!(strlen(item->text) && strlen(text))) + return; + + drw_setscheme(drw, scheme[item == sel + ? SchemeSelHighlight + : SchemeNormHighlight]); + for (i = 0, highlight = item->text; *highlight && text[i];) { + if (*highlight == text[i]) { + /* get indentation */ + c = *highlight; + *highlight = '\0'; + indent = TEXTW(item->text); + *highlight = c; + + /* highlight character */ + c = highlight[1]; + highlight[1] = '\0'; + drw_text( + drw, + x + indent - (lrpad / 2) - 1, + y, + MIN(maxw - indent, TEXTW(highlight) - lrpad), + bh, 0, highlight, 0 + ); + highlight[1] = c; + i++; + } + highlight++; + } +} \ No newline at end of file diff --git a/patch/include.c b/patch/include.c index 5892ed9..2503937 100644 --- a/patch/include.c +++ b/patch/include.c @@ -1,6 +1,9 @@ #if CENTER_PATCH #include "center.c" #endif +#if FUZZYHIGHLIGHT_PATCH +#include "fuzzyhighlight.c" +#endif #if FUZZYMATCH_PATCH #include "fuzzymatch.c" #endif diff --git a/patches.def.h b/patches.def.h index 7352688..ac14c58 100644 --- a/patches.def.h +++ b/patches.def.h @@ -11,6 +11,12 @@ */ #define CENTER_PATCH 0 +/* This patch make it so that fuzzy matches gets highlighted and is therefore meant + * to be used together with the fuzzymatch patch. + * https://tools.suckless.org/dmenu/patches/fuzzyhighlight/ + */ +#define FUZZYHIGHLIGHT_PATCH 0 + /* This patch adds support for fuzzy-matching to dmenu, allowing users to type non-consecutive * portions of the string to be matched. * https://tools.suckless.org/dmenu/patches/fuzzymatch/