From 7398f068c26037ef469cb569b375959810ab8dcb Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 6 Nov 2020 17:35:30 +0100 Subject: Update to version 5.0, new patches --- LICENSE | 2 +- Makefile | 9 +-- README | 24 +++++++ config.def.h | 31 --------- config.mk | 6 +- dmenu.1 | 20 ++++++ dmenu.c | 200 ++++++++++++++++++++++++++++++++++++++++++++++++++--------- drw.c | 1 + 8 files changed, 221 insertions(+), 72 deletions(-) create mode 100644 README delete mode 100644 config.def.h diff --git a/LICENSE b/LICENSE index 9762166..3afd28e 100644 --- a/LICENSE +++ b/LICENSE @@ -8,7 +8,7 @@ MIT/X Consortium License © 2009 Markus Schnalke © 2009 Evan Gates © 2010-2012 Connor Lane Smith -© 2014-2019 Hiltjo Posthuma +© 2014-2020 Hiltjo Posthuma © 2015-2019 Quentin Rameau Permission is hereby granted, free of charge, to any person obtaining a diff --git a/Makefile b/Makefile index cabc022..b42c96e 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ include config.mk SRC = drw.c dmenu.c stest.c util.c OBJ = $(SRC:.c=.o) -all: config.h options dmenu stest +all: options dmenu stest options: @echo dmenu build options: @@ -17,9 +17,6 @@ options: .c.o: $(CC) -c $(CFLAGS) $< -config.h: - cp config.def.h $@ - $(OBJ): arg.h config.h config.mk drw.h dmenu: dmenu.o drw.o util.o @@ -29,11 +26,11 @@ stest: stest.o $(CC) -o $@ stest.o $(LDFLAGS) clean: - rm -f config.h dmenu stest $(OBJ) dmenu-$(VERSION).tar.gz + rm -f dmenu stest $(OBJ) dmenu-$(VERSION).tar.gz dist: clean mkdir -p dmenu-$(VERSION) - cp LICENSE Makefile README arg.h config.def.h config.mk dmenu.1\ + cp LICENSE Makefile README arg.h config.h config.mk dmenu.1\ drw.h util.h dmenu_path dmenu_run stest.1 $(SRC)\ dmenu-$(VERSION) tar -cf dmenu-$(VERSION).tar dmenu-$(VERSION) diff --git a/README b/README new file mode 100644 index 0000000..a8fcdfe --- /dev/null +++ b/README @@ -0,0 +1,24 @@ +dmenu - dynamic menu +==================== +dmenu is an efficient dynamic menu for X. + + +Requirements +------------ +In order to build dmenu you need the Xlib header files. + + +Installation +------------ +Edit config.mk to match your local setup (dmenu is installed into +the /usr/local namespace by default). + +Afterwards enter the following command to build and install dmenu +(if necessary as root): + + make clean install + + +Running dmenu +------------- +See the man page for details. diff --git a/config.def.h b/config.def.h deleted file mode 100644 index 8a90f88..0000000 --- a/config.def.h +++ /dev/null @@ -1,31 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -/* Default settings; can be overriden by command line. */ - -static int topbar = 1; /* -b option; if 0, dmenu appears at bottom */ -static int centered = 0; /* -c option; centers dmenu on screen */ -static int min_width = 500; /* minimum width when centered */ -/* -fn option overrides fonts[0]; default X11 font or font set */ -static const char *fonts[] = { - "monospace:size=10" -}; -static const char *prompt = NULL; /* -p option; prompt to the left of input field */ -static const char *colors[SchemeLast][2] = { - /* fg bg */ - [SchemeNorm] = { "#bbbbbb", "#222222" }, - [SchemeSel] = { "#eeeeee", "#005577" }, - [SchemeSelHighlight] = { "#ffc978", "#b92121" }, - [SchemeNormHighlight] = { "#ffc978", "#222222" }, - [SchemeOut] = { "#000000", "#00ffff" }, -}; -/* -l option; if nonzero, dmenu uses vertical list with given number of lines */ -static unsigned int lines = 0; -static unsigned int lineheight = 0; /* -h option; minimum height of a menu line */ - -/* - * Characters not considered part of a word while deleting words - * for example: " /?\"&[]" - */ -static const char worddelimiters[] = " "; - -/* Size of the window border */ -static const unsigned int border_width = 2; diff --git a/config.mk b/config.mk index 6c4cb8e..c4964db 100644 --- a/config.mk +++ b/config.mk @@ -1,5 +1,5 @@ # dmenu version -VERSION = 4.9 +VERSION = 5.0 # paths PREFIX = /usr/local @@ -20,7 +20,7 @@ FREETYPEINC = /usr/local/include/freetype2 # includes and libs INCS = -I$(X11INC) -I$(FREETYPEINC) -LIBS = -L$(X11LIB) -lX11 $(XINERAMALIBS) $(FREETYPELIBS) +LIBS = -L$(X11LIB) -lX11 $(XINERAMALIBS) $(FREETYPELIBS) -lm # flags CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=200809L -DVERSION=\"$(VERSION)\" $(XINERAMAFLAGS) @@ -28,4 +28,4 @@ CFLAGS = -std=c99 -pedantic -Wall -march=ivybridge -O3 -pipe $(INCS) $(CPPFLAG LDFLAGS = $(LIBS) # compiler and linker -CC = clang-10 +CC = cc diff --git a/dmenu.1 b/dmenu.1 index 868c7df..488e095 100644 --- a/dmenu.1 +++ b/dmenu.1 @@ -20,6 +20,14 @@ dmenu \- dynamic menu .IR color ] .RB [ \-sf .IR color ] +.RB [ \-nhb +.IR color ] +.RB [ \-nhf +.IR color ] +.RB [ \-shb +.IR color ] +.RB [ \-shf +.IR color ] .RB [ \-w .IR windowid ] .P @@ -75,6 +83,18 @@ and X color names are supported. .BI \-nf " color" defines the normal foreground color. .TP +.BI \-nhb " color" +defines the normal highlight background color. +.TP +.BI \-nhf " color" +defines the normal highlight foreground color. +.TP +.BI \-shb " color" +defines the selected highlight background color. +.TP +.BI \-shf " color" +defines the selected highlight foreground color. +.TP .BI \-sb " color" defines the selected background color. .TP 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 #include +#include #include #include #include @@ -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 diff --git a/drw.c b/drw.c index 8fd1ca4..4cdbcbe 100644 --- a/drw.c +++ b/drw.c @@ -95,6 +95,7 @@ drw_free(Drw *drw) { XFreePixmap(drw->dpy, drw->drawable); XFreeGC(drw->dpy, drw->gc); + drw_fontset_free(drw->fonts); free(drw); } -- cgit v1.2.3