aboutsummaryrefslogtreecommitdiffstats
path: root/dmenu.c
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-11-06 17:35:30 +0100
committerJozanLeClerc <bousset.rudy@gmail.com>2020-11-06 17:35:30 +0100
commit7398f068c26037ef469cb569b375959810ab8dcb (patch)
treeddd4928674f02c0b89e6697782c23d3146de6776 /dmenu.c
parentRemoved numbers (diff)
downloaddmenu-7398f068c26037ef469cb569b375959810ab8dcb.tar.gz
dmenu-7398f068c26037ef469cb569b375959810ab8dcb.tar.bz2
dmenu-7398f068c26037ef469cb569b375959810ab8dcb.tar.xz
dmenu-7398f068c26037ef469cb569b375959810ab8dcb.tar.zst
dmenu-7398f068c26037ef469cb569b375959810ab8dcb.zip
Update to version 5.0, new patches
Diffstat (limited to 'dmenu.c')
-rw-r--r--dmenu.c200
1 files changed, 169 insertions, 31 deletions
diff --git a/dmenu.c b/dmenu.c
index aa54f29..3c96e70 100644
--- a/dmenu.c
+++ b/dmenu.c
@@ -1,6 +1,7 @@
/* See LICENSE file for copyright and license details. */
#include <ctype.h>
#include <locale.h>
+#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -32,6 +33,7 @@ struct item {
char *text;
struct item *left, *right;
int out;
+ double distance;
};
static char text[BUFSIZ] = "";
@@ -125,39 +127,78 @@ cistrstr(const char *s, const char *sub)
static void
drawhighlights(struct item *item, int x, int y, int maxw)
{
- char restorechar, tokens[sizeof text], *highlight, *token;
- int indentx, highlightlen;
-
- drw_setscheme(drw, scheme[item == sel ? SchemeSelHighlight : SchemeNormHighlight]);
- strcpy(tokens, text);
- for (token = strtok(tokens, " "); token; token = strtok(NULL, " ")) {
- highlight = fstrstr(item->text, token);
- while (highlight) {
- // Move item str end, calc width for highlight indent, & restore
- highlightlen = highlight - item->text;
- restorechar = *highlight;
- item->text[highlightlen] = '\0';
- indentx = TEXTW(item->text);
- item->text[highlightlen] = restorechar;
-
- // Move highlight str end, draw highlight, & restore
- restorechar = highlight[strlen(token)];
- highlight[strlen(token)] = '\0';
+ 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 (!fstrncmp(&(*highlight), &text[i], 1)) {
+ /* get indentation */
+ c = *highlight;
+ *highlight = '\0';
+ indent = TEXTW(item->text);
+ *highlight = c;
+
+ /* highlight character */
+ c = highlight[1];
+ highlight[1] = '\0';
drw_text(
- drw,
- x + indentx - (lrpad / 2) - 1,
- y,
- MIN(maxw - indentx, TEXTW(highlight) - lrpad),
- bh, 0, highlight, 0
- );
- highlight[strlen(token)] = restorechar;
-
- if (strlen(highlight) - strlen(token) < strlen(token)) break;
- highlight = fstrstr(highlight + strlen(token), token);
+ drw,
+ x + indent - (lrpad / 2),
+ y,
+ MIN(maxw - indent, TEXTW(highlight) - lrpad),
+ bh, 0, highlight, 0
+ );
+ highlight[1] = c;
+ i++;
}
+ highlight++;
}
}
+/* static void */
+/* drawhighlights(struct item *item, int x, int y, int maxw) */
+/* { */
+/* char restorechar, tokens[sizeof text], *highlight, *token; */
+/* int indentx, highlightlen; */
+/* */
+/* drw_setscheme(drw, scheme[item == sel ? SchemeSelHighlight : SchemeNormHighlight]); */
+/* strcpy(tokens, text); */
+/* for (token = strtok(tokens, " "); token; token = strtok(NULL, " ")) { */
+/* highlight = fstrstr(item->text, token); */
+/* while (highlight) { */
+/* // Move item str end, calc width for highlight indent, & restore */
+/* highlightlen = highlight - item->text; */
+/* restorechar = *highlight; */
+/* item->text[highlightlen] = '\0'; */
+/* indentx = TEXTW(item->text); */
+/* item->text[highlightlen] = restorechar; */
+/* */
+/* // Move highlight str end, draw highlight, & restore */
+/* restorechar = highlight[strlen(token)]; */
+/* highlight[strlen(token)] = '\0'; */
+/* if (indentx - (lrpad / 2) - 1 < maxw) */
+/* drw_text( */
+/* drw, */
+/* x + indentx - (lrpad / 2) - 1, */
+/* y, */
+/* MIN(maxw - indentx, TEXTW(highlight) - lrpad), */
+/* bh, 0, highlight, 0 */
+/* ); */
+/* highlight[strlen(token)] = restorechar; */
+ /* */
+ /* if (strlen(highlight) - strlen(token) < strlen(token)) break; */
+ /* highlight = fstrstr(highlight + strlen(token), token); */
+ /* } */
+ /* } */
+/* } */
+
static int
drawitem(struct item *item, int x, int y, int w)
{
@@ -239,6 +280,87 @@ grabfocus(void)
die("cannot grab focus");
}
+int
+compare_distance(const void *a, const void *b)
+{
+ struct item *da = *(struct item **) a;
+ struct item *db = *(struct item **) b;
+
+ if (!db)
+ return 1;
+ if (!da)
+ return -1;
+
+ return da->distance == db->distance ? 0 : da->distance < db->distance ? -1 : 1;
+}
+
+void
+fuzzymatch(void)
+{
+ /* bang - we have so much memory */
+ struct item *it;
+ struct item **fuzzymatches = NULL;
+ char c;
+ int number_of_matches = 0, i, pidx, sidx, eidx;
+ int text_len = strlen(text), itext_len;
+
+ matches = matchend = NULL;
+
+ /* walk through all items */
+ for (it = items; it && it->text; it++) {
+ if (text_len) {
+ itext_len = strlen(it->text);
+ pidx = 0; /* pointer */
+ sidx = eidx = -1; /* start of match, end of match */
+ /* walk through item text */
+ for (i = 0; i < itext_len && (c = it->text[i]); i++) {
+ /* fuzzy match pattern */
+ if (!fstrncmp(&text[pidx], &c, 1)) {
+ if(sidx == -1)
+ sidx = i;
+ pidx++;
+ if (pidx == text_len) {
+ eidx = i;
+ break;
+ }
+ }
+ }
+ /* build list of matches */
+ if (eidx != -1) {
+ /* compute distance */
+ /* add penalty if match starts late (log(sidx+2))
+ * add penalty for long a match without many matching characters */
+ it->distance = log(sidx + 2) + (double)(eidx - sidx - text_len);
+ /* fprintf(stderr, "distance %s %f\n", it->text, it->distance); */
+ appenditem(it, &matches, &matchend);
+ number_of_matches++;
+ }
+ } else {
+ appenditem(it, &matches, &matchend);
+ }
+ }
+
+ if (number_of_matches) {
+ /* initialize array with matches */
+ if (!(fuzzymatches = realloc(fuzzymatches, number_of_matches * sizeof(struct item*))))
+ die("cannot realloc %u bytes:", number_of_matches * sizeof(struct item*));
+ for (i = 0, it = matches; it && i < number_of_matches; i++, it = it->right) {
+ fuzzymatches[i] = it;
+ }
+ /* sort matches according to distance */
+ qsort(fuzzymatches, number_of_matches, sizeof(struct item*), compare_distance);
+ /* rebuild list of matches */
+ matches = matchend = NULL;
+ for (i = 0, it = fuzzymatches[i]; i < number_of_matches && it && \
+ it->text; i++, it = fuzzymatches[i]) {
+ appenditem(it, &matches, &matchend);
+ }
+ free(fuzzymatches);
+ }
+ curr = sel = matches;
+ calcoffsets();
+}
+
static void
grabkeyboard(void)
{
@@ -260,6 +382,10 @@ grabkeyboard(void)
static void
match(void)
{
+ if (fuzzy) {
+ fuzzymatch();
+ return;
+ }
static char **tokv = NULL;
static int tokn = 0;
@@ -656,7 +782,7 @@ setup(void)
/* calculate menu geometry */
bh = drw->fonts->h + 2;
- bh = MAX(bh,lineheight); /* make a menu line AT LEAST 'lineheight' tall */
+ bh = MAX(bh,lineheight); /* make a menu line AT LEAST 'lineheight' tall */
lines = MAX(lines, 0);
mh = (lines + 1) * bh;
promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0;
@@ -714,6 +840,7 @@ setup(void)
mw = wa.width;
}
}
+ promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0;
inputw = MIN(inputw, mw/3);
match();
@@ -753,8 +880,9 @@ static void
usage(void)
{
fputs("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
- " [-h height]\n"
- " [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n", stderr);
+ " [-h height]\n"
+ " [-nb color] [-nf color] [-sb color] [-sf color]\n"
+ " [-nhb color] [-nhf color] [-shb color] [-shf color] [-w windowid]\n", stderr);
exit(1);
}
@@ -773,6 +901,8 @@ main(int argc, char *argv[])
topbar = 0;
else if (!strcmp(argv[i], "-f")) /* grabs keyboard before reading stdin */
fast = 1;
+ else if (!strcmp(argv[i], "-F")) /* grabs keyboard before reading stdin */
+ fuzzy = 0;
else if (!strcmp(argv[i], "-c")) /* centers dmenu on screen */
centered = 1;
else if (!strcmp(argv[i], "-i")) { /* case-insensitive item matching */
@@ -801,6 +931,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];
+ 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];
else if (!strcmp(argv[i], "-w")) /* embedding window id */
embed = argv[++i];
else