Adding fuzzyhighlight patch

This commit is contained in:
bakkeby 2020-04-04 09:58:35 +02:00
parent 415fa6ccad
commit fcfde08faf
6 changed files with 87 additions and 1 deletions

View File

@ -15,6 +15,8 @@ Refer to [https://tools.suckless.org/dmenu/](https://tools.suckless.org/dmenu/)
### Changelog: ### Changelog:
2020-04-05 - Added fuzzyhighlight patch
2020-02-09 - Added revised border patch (adding command line parameter for setting border width) 2020-02-09 - Added revised border patch (adding command line parameter for setting border width)
2019-12-29 - Added xresources patch 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/) - [center](https://tools.suckless.org/dmenu/patches/center/)
- this patch centers dmenu in the middle of the screen - 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/) - [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 - adds support for fuzzy-matching to dmenu, allowing users to type non-consecutive portions of the string to be matched

View File

@ -35,6 +35,10 @@ static const char *colors[][2] =
[SchemeNorm] = { "#bbbbbb", "#222222" }, [SchemeNorm] = { "#bbbbbb", "#222222" },
[SchemeSel] = { "#eeeeee", "#005577" }, [SchemeSel] = { "#eeeeee", "#005577" },
[SchemeOut] = { "#000000", "#00ffff" }, [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 */ /* -l option; if nonzero, dmenu uses vertical list with given number of lines */
static unsigned int lines = 0; static unsigned int lines = 0;

33
dmenu.c
View File

@ -27,7 +27,16 @@
#define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad) #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
/* enums */ /* 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 { struct item {
char *text; char *text;
@ -165,6 +174,9 @@ cistrstr(const char *s, const char *sub)
static int static int
drawitem(struct item *item, int x, int y, int w) drawitem(struct item *item, int x, int y, int w)
{ {
#if FUZZYHIGHLIGHT_PATCH
int r;
#endif // FUZZYHIGHLIGHT_PATCH
if (item == sel) if (item == sel)
drw_setscheme(drw, scheme[SchemeSel]); drw_setscheme(drw, scheme[SchemeSel]);
else if (item->out) else if (item->out)
@ -172,7 +184,13 @@ drawitem(struct item *item, int x, int y, int w)
else else
drw_setscheme(drw, scheme[SchemeNorm]); 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); return drw_text(drw, x, y, w, bh, lrpad / 2, item->text, 0);
#endif // FUZZYHIGHLIGHT_PATCH
} }
static void static void
@ -1078,6 +1096,9 @@ usage(void)
#if XYW_PATCH #if XYW_PATCH
" [-X xoffset] [-Y yoffset] [-W width]" // (arguments made upper case due to conflicts) " [-X xoffset] [-Y yoffset] [-W width]" // (arguments made upper case due to conflicts)
#endif // XYW_PATCH #endif // XYW_PATCH
#if FUZZYHIGHLIGHT_PATCH
"\n [-nhb color] [-nhf color] [-shb color] [-shf color]" // highlight colors
#endif // FUZZYHIGHLIGHT_PATCH
"\n", stderr); "\n", stderr);
exit(1); exit(1);
} }
@ -1174,6 +1195,16 @@ main(int argc, char *argv[])
colors[SchemeSel][ColBg] = argv[++i]; colors[SchemeSel][ColBg] = argv[++i];
else if (!strcmp(argv[i], "-sf")) /* selected foreground color */ else if (!strcmp(argv[i], "-sf")) /* selected foreground color */
colors[SchemeSel][ColFg] = argv[++i]; 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 */ else if (!strcmp(argv[i], "-w")) /* embedding window id */
embed = argv[++i]; embed = argv[++i];
#if BORDER_PATCH #if BORDER_PATCH

37
patch/fuzzyhighlight.c Normal file
View File

@ -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++;
}
}

View File

@ -1,6 +1,9 @@
#if CENTER_PATCH #if CENTER_PATCH
#include "center.c" #include "center.c"
#endif #endif
#if FUZZYHIGHLIGHT_PATCH
#include "fuzzyhighlight.c"
#endif
#if FUZZYMATCH_PATCH #if FUZZYMATCH_PATCH
#include "fuzzymatch.c" #include "fuzzymatch.c"
#endif #endif

View File

@ -11,6 +11,12 @@
*/ */
#define CENTER_PATCH 0 #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 /* This patch adds support for fuzzy-matching to dmenu, allowing users to type non-consecutive
* portions of the string to be matched. * portions of the string to be matched.
* https://tools.suckless.org/dmenu/patches/fuzzymatch/ * https://tools.suckless.org/dmenu/patches/fuzzymatch/