aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkrolyxon <krolyxon@tutanota.com>2023-11-10 01:06:55 +0530
committerkrolyxon <krolyxon@tutanota.com>2023-11-10 01:06:55 +0530
commit9e27e7568271b6febc3dc169f052056d564666dd (patch)
tree1c294024d492361257775ff8e92c69baca917760
parent2a20fb992b07fc343b3e87e3ba92bc05f6cb4d05 (diff)
patch: fuzzymatch
-rwxr-xr-x[-rw-r--r--].gitignore0
-rwxr-xr-x[-rw-r--r--]Makefile0
-rwxr-xr-x[-rw-r--r--]README.md0
-rwxr-xr-x[-rw-r--r--]arg.h0
-rwxr-xr-x[-rw-r--r--]config.h3
-rwxr-xr-x[-rw-r--r--]config.mk2
-rwxr-xr-xdmenubin48464 -> 43632 bytes
-rwxr-xr-x[-rw-r--r--]dmenu.10
-rwxr-xr-x[-rw-r--r--]dmenu.c90
-rwxr-xr-x[-rw-r--r--]drw.c0
-rwxr-xr-x[-rw-r--r--]drw.h0
-rwxr-xr-xstestbin21536 -> 16408 bytes
-rwxr-xr-x[-rw-r--r--]stest.10
-rwxr-xr-x[-rw-r--r--]stest.c0
-rwxr-xr-x[-rw-r--r--]util.c0
-rwxr-xr-x[-rw-r--r--]util.h0
16 files changed, 93 insertions, 2 deletions
diff --git a/.gitignore b/.gitignore
index 5761abc..5761abc 100644..100755
--- a/.gitignore
+++ b/.gitignore
diff --git a/Makefile b/Makefile
index 244961a..244961a 100644..100755
--- a/Makefile
+++ b/Makefile
diff --git a/README.md b/README.md
index 876b6a4..876b6a4 100644..100755
--- a/README.md
+++ b/README.md
diff --git a/arg.h b/arg.h
index e94e02b..e94e02b 100644..100755
--- a/arg.h
+++ b/arg.h
diff --git a/config.h b/config.h
index 32aae14..a32e834 100644..100755
--- a/config.h
+++ b/config.h
@@ -2,12 +2,13 @@
/* Default settings; can be overriden by command line. */
static int topbar = 1; /* -b option; if 0, dmenu appears at bottom */
+static int fuzzy = 1;
/* -fn option overrides fonts[0]; default X11 font or font set */
static const char *fonts[] = {
"JetBrainsMono Nerd Font:size=10",
"JetBrainsMono Nerd Font:pixelsize=10:antialias=true:autohint=true",
- "emoji:size=10" // For rendering emojis
+ "emoji:size=10" // For rendering emojis
};
static const unsigned int bgalpha = 0xe0;
static const unsigned int fgalpha = OPAQUE;
diff --git a/config.mk b/config.mk
index 8531fb9..1641c06 100644..100755
--- a/config.mk
+++ b/config.mk
@@ -20,7 +20,7 @@ FREETYPEINC = /usr/include/freetype2
# includes and libs
INCS = -I$(X11INC) -I$(FREETYPEINC)
-LIBS = -L$(X11LIB) -lX11 $(XINERAMALIBS) $(FREETYPELIBS) -lXrender
+LIBS = -L$(X11LIB) -lX11 $(XINERAMALIBS) $(FREETYPELIBS) -lXrender -lm
# flags
CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=200809L -DVERSION=\"$(VERSION)\" $(XINERAMAFLAGS)
diff --git a/dmenu b/dmenu
index b5e52df..229e844 100755
--- a/dmenu
+++ b/dmenu
Binary files differ
diff --git a/dmenu.1 b/dmenu.1
index 4c87074..4c87074 100644..100755
--- a/dmenu.1
+++ b/dmenu.1
diff --git a/dmenu.c b/dmenu.c
index bde0869..bd59df0 100644..100755
--- 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>
@@ -36,6 +37,7 @@ struct item {
char *text;
struct item *left, *right;
int out;
+ double distance;
};
static char text[BUFSIZ] = "";
@@ -68,6 +70,7 @@ static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
static char *(*fstrstr)(const char *, const char *) = strstr;
+
static void
xinitvisual()
{
@@ -265,9 +268,94 @@ grabkeyboard(void)
die("cannot grab keyboard");
}
+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
match(void)
{
+ if(fuzzy) {
+ fuzzymatch();
+ return;
+ }
static char **tokv = NULL;
static int tokn = 0;
@@ -927,6 +1015,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], "-i")) { /* case-insensitive item matching */
fstrncmp = strncasecmp;
fstrstr = cistrstr;
diff --git a/drw.c b/drw.c
index 7694955..7694955 100644..100755
--- a/drw.c
+++ b/drw.c
diff --git a/drw.h b/drw.h
index 4f66f0d..4f66f0d 100644..100755
--- a/drw.h
+++ b/drw.h
diff --git a/stest b/stest
index 90339af..f193698 100755
--- a/stest
+++ b/stest
Binary files differ
diff --git a/stest.1 b/stest.1
index 2667d8a..2667d8a 100644..100755
--- a/stest.1
+++ b/stest.1
diff --git a/stest.c b/stest.c
index 7a7b0bc..7a7b0bc 100644..100755
--- a/stest.c
+++ b/stest.c
diff --git a/util.c b/util.c
index fe044fc..fe044fc 100644..100755
--- a/util.c
+++ b/util.c
diff --git a/util.h b/util.h
index f633b51..f633b51 100644..100755
--- a/util.h
+++ b/util.h