aboutsummaryrefslogtreecommitdiff
path: root/dmenu.c
diff options
context:
space:
mode:
Diffstat (limited to 'dmenu.c')
-rw-r--r--dmenu.c290
1 files changed, 245 insertions, 45 deletions
diff --git a/dmenu.c b/dmenu.c
index eca67ac..2e8a4b0 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>
@@ -15,6 +16,7 @@
#include <X11/extensions/Xinerama.h>
#endif
#include <X11/Xft/Xft.h>
+#include <X11/Xresource.h>
#include "drw.h"
#include "util.h"
@@ -26,12 +28,13 @@
#define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
/* enums */
-enum { SchemeNorm, SchemeSel, SchemeOut, SchemeLast }; /* color schemes */
+enum { SchemeNorm, SchemeSel, SchemeOut, SchemeNormHighlight, SchemeSelHighlight, SchemeLast }; /* color schemes */
struct item {
char *text;
struct item *left, *right;
int out;
+ double distance;
};
static char text[BUFSIZ] = "";
@@ -53,6 +56,10 @@ static XIC xic;
static Drw *drw;
static Clr *scheme[SchemeLast];
+/* Temporary arrays to allow overriding xresources values */
+static char *colortemp[8];
+static char *tempfonts;
+
#include "config.h"
static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
@@ -74,19 +81,6 @@ appenditem(struct item *item, struct item **list, struct item **last)
static void
calcoffsets(void)
{
- int i, n;
-
- if (lines > 0)
- n = lines * bh;
- else
- n = mw - (promptw + inputw + TEXTW("<") + TEXTW(">"));
- /* calculate which items will begin the next page and previous page */
- for (i = 0, next = curr; next; next = next->right)
- if ((i += (lines > 0) ? bh : MIN(TEXTW(next->text), n)) > n)
- break;
- for (i = 0, prev = curr; prev && prev->left; prev = prev->left)
- if ((i += (lines > 0) ? bh : MIN(TEXTW(prev->left->text), n)) > n)
- break;
}
static void
@@ -120,6 +114,43 @@ cistrstr(const char *h, const char *n)
return NULL;
}
+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 - 1 < maxw)
+ drw_text(
+ drw,
+ x + indentx - lrpad - 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)
{
@@ -130,13 +161,14 @@ drawitem(struct item *item, int x, int y, int w)
else
drw_setscheme(drw, scheme[SchemeNorm]);
- return drw_text(drw, x, y, w, bh, lrpad / 2, item->text, 0);
+ int r = drw_text(drw, x, y, w, bh, lrpad, item->text, 0);
+ drawhighlights(item, x, y, w);
+ return r;
}
static void
drawmenu(void)
{
- unsigned int curpos;
struct item *item;
int x = 0, y = 0, w;
@@ -145,17 +177,7 @@ drawmenu(void)
if (prompt && *prompt) {
drw_setscheme(drw, scheme[SchemeSel]);
- x = drw_text(drw, x, 0, promptw, bh, lrpad / 2, prompt, 0);
- }
- /* draw input field */
- w = (lines > 0 || !matches) ? mw - x : inputw;
- drw_setscheme(drw, scheme[SchemeNorm]);
- drw_text(drw, x, 0, w, bh, lrpad / 2, text, 0);
-
- curpos = TEXTW(text) - TEXTW(&text[cursor]);
- if ((curpos += lrpad / 2 - 1) < w) {
- drw_setscheme(drw, scheme[SchemeNorm]);
- drw_rect(drw, x + curpos, 2, 2, bh - 4, 1, 0);
+ x = drw_text(drw, x, 0, promptw, bh, lrpad, prompt, 0);
}
if (lines > 0) {
@@ -165,18 +187,18 @@ drawmenu(void)
} else if (matches) {
/* draw horizontal list */
x += inputw;
- w = TEXTW("<");
+ w = TEXTW("");
if (curr->left) {
drw_setscheme(drw, scheme[SchemeNorm]);
- drw_text(drw, x, 0, w, bh, lrpad / 2, "<", 0);
+ drw_text(drw, x, 0, w, bh, lrpad, "", 0);
}
x += w;
for (item = curr; item != next; item = item->right)
- x = drawitem(item, x, 0, MIN(TEXTW(item->text), mw - x - TEXTW(">")));
+ x = drawitem(item, x, 0, MIN(TEXTW(item->text), mw - x - TEXTW("")));
if (next) {
- w = TEXTW(">");
+ w = TEXTW("");
drw_setscheme(drw, scheme[SchemeNorm]);
- drw_text(drw, mw - w, 0, w, bh, lrpad / 2, ">", 0);
+ drw_text(drw, mw - w, 0, w, bh, lrpad, "", 0);
}
}
drw_map(drw, win, 0, 0, mw, mh);
@@ -217,9 +239,100 @@ 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;
+
+ if (matches == NULL) {
+ cleanup();
+ exit(127);
+ }
+
+ calcoffsets();
+}
+
static void
match(void)
{
+ if (fuzzy) {
+ fuzzymatch();
+ return;
+ }
static char **tokv = NULL;
static int tokn = 0;
@@ -540,7 +653,7 @@ static void
readstdin(void)
{
char buf[sizeof text], *p;
- size_t i, imax = 0, size = 0;
+ size_t i, size = 0;
unsigned int tmpmax = 0;
/* read each line from stdin and add it to the item list */
@@ -556,12 +669,11 @@ readstdin(void)
drw_font_getexts(drw->fonts, buf, strlen(buf), &tmpmax, NULL);
if (tmpmax > inputw) {
inputw = tmpmax;
- imax = i;
}
}
if (items)
items[i].text = NULL;
- inputw = items ? TEXTW(items[imax].text) : 0;
+ inputw = 0;
lines = MIN(lines, i);
}
@@ -619,8 +731,13 @@ setup(void)
int a, di, n, area = 0;
#endif
/* init appearance */
- for (j = 0; j < SchemeLast; j++)
- scheme[j] = drw_scm_create(drw, colors[j], 2);
+ for (j = 0; j < SchemeLast; j++) {
+ scheme[j] = drw_scm_create(drw, (const char**)colors[j], 2);
+ }
+ for (j = 0; j < SchemeOut; ++j) {
+ for (i = 0; i < 2; ++i)
+ free((char *) colors[j][i]);
+ }
clip = XInternAtom(dpy, "CLIPBOARD", False);
utf8 = XInternAtom(dpy, "UTF8_STRING", False);
@@ -669,7 +786,7 @@ setup(void)
y = topbar ? 0 : wa.height - mh;
mw = wa.width;
}
- promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0;
+ promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad : 0;
inputw = MIN(inputw, mw/3);
match();
@@ -712,6 +829,57 @@ usage(void)
exit(1);
}
+void
+readxresources(void) {
+ XrmInitialize();
+
+ char* xrm;
+ if ((xrm = XResourceManagerString(drw->dpy))) {
+ char *type;
+ XrmDatabase xdb = XrmGetStringDatabase(xrm);
+ XrmValue xval;
+
+ if (XrmGetResource(xdb, "dmenu.font", "*", &type, &xval))
+ fonts[0] = strdup(xval.addr);
+ else
+ fonts[0] = strdup(fonts[0]);
+ if (XrmGetResource(xdb, "dmenu.background", "*", &type, &xval))
+ colors[SchemeNorm][ColBg] = strdup(xval.addr);
+ else
+ colors[SchemeNorm][ColBg] = strdup(colors[SchemeNorm][ColBg]);
+ if (XrmGetResource(xdb, "dmenu.foreground", "*", &type, &xval))
+ colors[SchemeNorm][ColFg] = strdup(xval.addr);
+ else
+ colors[SchemeNorm][ColFg] = strdup(colors[SchemeNorm][ColFg]);
+ if (XrmGetResource(xdb, "dmenu.selbackground", "*", &type, &xval))
+ colors[SchemeSel][ColBg] = strdup(xval.addr);
+ else
+ colors[SchemeSel][ColBg] = strdup(colors[SchemeSel][ColBg]);
+ if (XrmGetResource(xdb, "dmenu.selforeground", "*", &type, &xval))
+ colors[SchemeSel][ColFg] = strdup(xval.addr);
+ else
+ colors[SchemeSel][ColFg] = strdup(colors[SchemeSel][ColFg]);
+ if (XrmGetResource(xdb, "dmenu.selhiforeground", "*", &type, &xval))
+ colors[SchemeSelHighlight][ColFg] = strdup(xval.addr);
+ else
+ colors[SchemeSelHighlight][ColFg] = strdup(colors[SchemeSelHighlight][ColFg]);
+ if (XrmGetResource(xdb, "dmenu.selhibackground", "*", &type, &xval))
+ colors[SchemeSelHighlight][ColBg] = strdup(xval.addr);
+ else
+ colors[SchemeSelHighlight][ColBg] = strdup(colors[SchemeSelHighlight][ColBg]);
+ if (XrmGetResource(xdb, "dmenu.normhiforeground", "*", &type, &xval))
+ colors[SchemeNormHighlight][ColFg] = strdup(xval.addr);
+ else
+ colors[SchemeNormHighlight][ColFg] = strdup(colors[SchemeNormHighlight][ColFg]);
+ if (XrmGetResource(xdb, "dmenu.normhibackground", "*", &type, &xval))
+ colors[SchemeNormHighlight][ColBg] = strdup(xval.addr);
+ else
+ colors[SchemeNormHighlight][ColBg] = strdup(colors[SchemeNormHighlight][ColBg]);
+
+ XrmDestroyDatabase(xdb);
+ }
+}
+
int
main(int argc, char *argv[])
{
@@ -727,6 +895,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;
@@ -740,15 +910,23 @@ main(int argc, char *argv[])
else if (!strcmp(argv[i], "-p")) /* adds prompt to left of input field */
prompt = argv[++i];
else if (!strcmp(argv[i], "-fn")) /* font or font set */
- fonts[0] = argv[++i];
+ tempfonts = argv[++i];
else if (!strcmp(argv[i], "-nb")) /* normal background color */
- colors[SchemeNorm][ColBg] = argv[++i];
+ colortemp[0] = argv[++i];
else if (!strcmp(argv[i], "-nf")) /* normal foreground color */
- colors[SchemeNorm][ColFg] = argv[++i];
+ colortemp[1] = argv[++i];
else if (!strcmp(argv[i], "-sb")) /* selected background color */
- colors[SchemeSel][ColBg] = argv[++i];
+ colortemp[2] = argv[++i];
else if (!strcmp(argv[i], "-sf")) /* selected foreground color */
- colors[SchemeSel][ColFg] = argv[++i];
+ colortemp[3] = argv[++i];
+ else if (!strcmp(argv[i], "-shf")) /* selected highlight foreground color */
+ colortemp[4] = argv[++i];
+ else if (!strcmp(argv[i], "-shb")) /* selected highlight background color */
+ colortemp[5] = argv[++i];
+ else if (!strcmp(argv[i], "-nhf")) /* normal highlight foreground color */
+ colortemp[6] = argv[++i];
+ else if (!strcmp(argv[i], "-nhb")) /* normal highlight background color */
+ colortemp[7] = argv[++i];
else if (!strcmp(argv[i], "-w")) /* embedding window id */
embed = argv[++i];
else
@@ -766,9 +944,31 @@ main(int argc, char *argv[])
die("could not get embedding window attributes: 0x%lx",
parentwin);
drw = drw_create(dpy, screen, root, wa.width, wa.height);
- if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
+ readxresources();
+ /* Now we check whether to override xresources with commandline parameters */
+ if ( tempfonts )
+ fonts[0] = strdup(tempfonts);
+ if ( colortemp[0])
+ colors[SchemeNorm][ColBg] = strdup(colortemp[0]);
+ if ( colortemp[1])
+ colors[SchemeNorm][ColFg] = strdup(colortemp[1]);
+ if ( colortemp[2])
+ colors[SchemeSel][ColBg] = strdup(colortemp[2]);
+ if ( colortemp[3])
+ colors[SchemeSel][ColFg] = strdup(colortemp[3]);
+ if ( colortemp[4])
+ colors[SchemeSelHighlight][ColFg] = strdup(colortemp[4]);
+ if ( colortemp[5])
+ colors[SchemeSelHighlight][ColBg] = strdup(colortemp[5]);
+ if ( colortemp[6])
+ colors[SchemeNormHighlight][ColFg] = strdup(colortemp[6]);
+ if ( colortemp[7])
+ colors[SchemeNormHighlight][ColBg] = strdup(colortemp[7]);
+ if (!drw_fontset_create(drw, (const char**)fonts, LENGTH(fonts)))
die("no fonts could be loaded.");
- lrpad = drw->fonts->h;
+
+ free((char *) fonts[0]);
+ lrpad = 10;
#ifdef __OpenBSD__
if (pledge("stdio rpath", NULL) == -1)