]> git.armaanb.net Git - dmenu.git/blobdiff - dmenu.c
Partially revert 44c7de3: fix items text width offset calculation
[dmenu.git] / dmenu.c
diff --git a/dmenu.c b/dmenu.c
index cf5d97655d03db9cac670352dee971708813cb6f..8e84fbdf9afb6e572d2cae240d0d2801851c1765 100644 (file)
--- a/dmenu.c
+++ b/dmenu.c
@@ -1,12 +1,12 @@
 /* See LICENSE file for copyright and license details. */
 #include <ctype.h>
 #include <locale.h>
-#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <strings.h>
 #include <time.h>
+
 #include <X11/Xlib.h>
 #include <X11/Xatom.h>
 #include <X11/Xutil.h>
 #define INTERSECT(x,y,w,h,r)  (MAX(0, MIN((x)+(w),(r).x_org+(r).width)  - MAX((x),(r).x_org)) \
                              * MAX(0, MIN((y)+(h),(r).y_org+(r).height) - MAX((y),(r).y_org)))
 #define LENGTH(X)             (sizeof X / sizeof X[0])
-#define TEXTNW(X,N)           (drw_font_getexts_width(drw->fonts[0], (X), (N)))
-#define TEXTW(X)              (drw_text(drw, 0, 0, 0, 0, (X), 0) + drw->fonts[0]->h)
+#define TEXTW(X)              (drw_fontset_getwidth(drw, (X)) + lrpad)
 
 /* enums */
 enum { SchemeNorm, SchemeSel, SchemeOut, SchemeLast }; /* color schemes */
 
-typedef struct Item Item;
-struct Item {
+struct item {
        char *text;
-       Item *left, *right;
-       bool out;
+       struct item *left, *right;
+       int out;
 };
 
-static void appenditem(Item *, Item **, Item **);
-static void calcoffsets(void);
-static char *cistrstr(const char *, const char *);
-static void cleanup(void);
-static void drawmenu(void);
-static void grabkeyboard(void);
-static void insert(const char *, ssize_t);
-static void keypress(XKeyEvent *);
-static void match(void);
-static size_t nextrune(int);
-static void paste(void);
-static void readstdin(void);
-static void run(void);
-static void setup(void);
-static void usage(void);
-
 static char text[BUFSIZ] = "";
 static int bh, mw, mh;
 static int sw, sh; /* X display screen geometry width, height */
-static int inputw, promptw;
+static int inputw = 0, promptw;
+static int lrpad; /* sum of left and right padding */
 static size_t cursor;
-static Item *items = NULL;
-static Item *matches, *matchend;
-static Item *prev, *curr, *next, *sel;
+static struct item *items = NULL;
+static struct item *matches, *matchend;
+static struct item *prev, *curr, *next, *sel;
 static int mon = -1, screen;
 
 static Atom clip, utf8;
@@ -66,8 +49,8 @@ static Display *dpy;
 static Window root, win;
 static XIC xic;
 
-static ClrScheme scheme[SchemeLast];
 static Drw *drw;
+static Clr *scheme[SchemeLast];
 
 #include "config.h"
 
@@ -75,7 +58,7 @@ static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
 static char *(*fstrstr)(const char *, const char *) = strstr;
 
 static void
-appenditem(Item *item, Item **list, Item **last)
+appenditem(struct item *item, struct item **list, struct item **last)
 {
        if (*last)
                (*last)->right = item;
@@ -108,13 +91,11 @@ calcoffsets(void)
 static void
 cleanup(void)
 {
+       size_t i;
+
        XUngrabKey(dpy, AnyKey, AnyModifier, root);
-       drw_clr_free(scheme[SchemeNorm].bg);
-       drw_clr_free(scheme[SchemeNorm].fg);
-       drw_clr_free(scheme[SchemeSel].fg);
-       drw_clr_free(scheme[SchemeSel].bg);
-       drw_clr_free(scheme[SchemeOut].fg);
-       drw_clr_free(scheme[SchemeOut].bg);
+       for (i = 0; i < SchemeLast; i++)
+               free(scheme[i]);
        drw_free(drw);
        XSync(dpy, False);
        XCloseDisplay(dpy);
@@ -131,70 +112,63 @@ cistrstr(const char *s, const char *sub)
        return NULL;
 }
 
+static int
+drawitem(struct item *item, int x, int y, int w)
+{
+       if (item == sel)
+               drw_setscheme(drw, scheme[SchemeSel]);
+       else if (item->out)
+               drw_setscheme(drw, scheme[SchemeOut]);
+       else
+               drw_setscheme(drw, scheme[SchemeNorm]);
+
+       return drw_text(drw, x, y, w, bh, lrpad / 2, item->text, 0);
+}
+
 static void
 drawmenu(void)
 {
-       int curpos;
-       Item *item;
-       int x = 0, y = 0, h = bh, w;
+       unsigned int curpos;
+       struct item *item;
+       int x = 0, y = 0, w;
 
-       drw_setscheme(drw, &scheme[SchemeNorm]);
-       drw_rect(drw, 0, 0, mw, mh, 1, 1, 1);
+       drw_setscheme(drw, scheme[SchemeNorm]);
+       drw_rect(drw, 0, 0, mw, mh, 1, 1);
 
        if (prompt && *prompt) {
-               drw_setscheme(drw, &scheme[SchemeSel]);
-               drw_text(drw, x, 0, promptw, bh, prompt, 0);
-               x += promptw;
+               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, text, 0);
+       drw_setscheme(drw, scheme[SchemeNorm]);
+       drw_text(drw, x, 0, w, bh, lrpad / 2, text, 0);
 
-       if ((curpos = TEXTNW(text, cursor) + bh / 2 - 2) < w) {
-               drw_setscheme(drw, &scheme[SchemeNorm]);
-               drw_rect(drw, x + curpos + 2, 2, 1, bh - 4, 1, 1, 0);
+       drw_font_getexts(drw->fonts, text, cursor, &curpos, NULL);
+       if ((curpos += lrpad / 2 - 1) < w) {
+               drw_setscheme(drw, scheme[SchemeNorm]);
+               drw_rect(drw, x + curpos, 2, 2, bh - 4, 1, 0);
        }
 
        if (lines > 0) {
                /* draw vertical list */
-               w = mw - x;
-               for (item = curr; item != next; item = item->right) {
-                       y += h;
-                       if (item == sel)
-                               drw_setscheme(drw, &scheme[SchemeSel]);
-                       else if (item->out)
-                               drw_setscheme(drw, &scheme[SchemeOut]);
-                       else
-                               drw_setscheme(drw, &scheme[SchemeNorm]);
-
-                       drw_text(drw, x, y, w, bh, item->text, 0);
-               }
+               for (item = curr; item != next; item = item->right)
+                       drawitem(item, x, y += bh, mw - x);
        } else if (matches) {
                /* draw horizontal list */
                x += inputw;
                w = TEXTW("<");
                if (curr->left) {
-                       drw_setscheme(drw, &scheme[SchemeNorm]);
-                       drw_text(drw, x, 0, w, bh, "<", 0);
+                       drw_setscheme(drw, scheme[SchemeNorm]);
+                       drw_text(drw, x, 0, w, bh, lrpad / 2, "<", 0);
                }
-               for (item = curr; item != next; item = item->right) {
-                       x += w;
-                       w = MIN(TEXTW(item->text), mw - x - TEXTW(">"));
-
-                       if (item == sel)
-                               drw_setscheme(drw, &scheme[SchemeSel]);
-                       else if (item->out)
-                               drw_setscheme(drw, &scheme[SchemeOut]);
-                       else
-                               drw_setscheme(drw, &scheme[SchemeNorm]);
-                       drw_text(drw, x, 0, w, bh, item->text, 0);
-               }
-               w = TEXTW(">");
-               x = mw - w;
+               x += w;
+               for (item = curr; item != next; item = item->right)
+                       x = drawitem(item, x, 0, MIN(TEXTW(item->text), mw - x - TEXTW(">")));
                if (next) {
-                       drw_setscheme(drw, &scheme[SchemeNorm]);
-                       drw_text(drw, x, 0, w, bh, ">", 0);
+                       w = TEXTW(">");
+                       drw_setscheme(drw, scheme[SchemeNorm]);
+                       drw_text(drw, mw - w, 0, w, bh, lrpad / 2, ">", 0);
                }
        }
        drw_map(drw, win, 0, 0, mw, mh);
@@ -208,14 +182,68 @@ grabkeyboard(void)
 
        /* try to grab keyboard, we may have to wait for another process to ungrab */
        for (i = 0; i < 1000; i++) {
-               if (XGrabKeyboard(dpy, DefaultRootWindow(dpy), True,
-                                GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
+               if (XGrabKeyboard(dpy, DefaultRootWindow(dpy), True, GrabModeAsync,
+                                 GrabModeAsync, CurrentTime) == GrabSuccess)
                        return;
                nanosleep(&ts, NULL);
        }
        die("cannot grab keyboard\n");
 }
 
+static void
+match(void)
+{
+       static char **tokv = NULL;
+       static int tokn = 0;
+
+       char buf[sizeof text], *s;
+       int i, tokc = 0;
+       size_t len, textsize;
+       struct item *item, *lprefix, *lsubstr, *prefixend, *substrend;
+
+       strcpy(buf, text);
+       /* separate input text into tokens to be matched individually */
+       for (s = strtok(buf, " "); s; tokv[tokc - 1] = s, s = strtok(NULL, " "))
+               if (++tokc > tokn && !(tokv = realloc(tokv, ++tokn * sizeof *tokv)))
+                       die("cannot realloc %u bytes\n", tokn * sizeof *tokv);
+       len = tokc ? strlen(tokv[0]) : 0;
+
+       matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL;
+       textsize = strlen(text);
+       for (item = items; item && item->text; item++) {
+               for (i = 0; i < tokc; i++)
+                       if (!fstrstr(item->text, tokv[i]))
+                               break;
+               if (i != tokc) /* not all tokens match */
+                       continue;
+               /* exact matches go first, then prefixes, then substrings */
+               if (!tokc || !fstrncmp(text, item->text, textsize))
+                       appenditem(item, &matches, &matchend);
+               else if (!fstrncmp(tokv[0], item->text, len))
+                       appenditem(item, &lprefix, &prefixend);
+               else
+                       appenditem(item, &lsubstr, &substrend);
+       }
+       if (lprefix) {
+               if (matches) {
+                       matchend->right = lprefix;
+                       lprefix->left = matchend;
+               } else
+                       matches = lprefix;
+               matchend = prefixend;
+       }
+       if (lsubstr) {
+               if (matches) {
+                       matchend->right = lsubstr;
+                       lsubstr->left = matchend;
+               } else
+                       matches = lsubstr;
+               matchend = substrend;
+       }
+       curr = sel = matches;
+       calcoffsets();
+}
+
 static void
 insert(const char *str, ssize_t n)
 {
@@ -229,6 +257,17 @@ insert(const char *str, ssize_t n)
        match();
 }
 
+static size_t
+nextrune(int inc)
+{
+       ssize_t n;
+
+       /* return location of next utf8 rune in the given direction (+1 or -1) */
+       for (n = cursor + inc; n + inc >= 0 && (text[n] & 0xc0) == 0x80; n += inc)
+               ;
+       return n;
+}
+
 static void
 keypress(XKeyEvent *ev)
 {
@@ -266,12 +305,13 @@ keypress(XKeyEvent *ev)
                        insert(NULL, 0 - cursor);
                        break;
                case XK_w: /* delete word */
-                       while (cursor > 0 && text[nextrune(-1)] == ' ')
+                       while (cursor > 0 && strchr(worddelimiters, text[nextrune(-1)]))
                                insert(NULL, nextrune(-1) - cursor);
-                       while (cursor > 0 && text[nextrune(-1)] != ' ')
+                       while (cursor > 0 && !strchr(worddelimiters, text[nextrune(-1)]))
                                insert(NULL, nextrune(-1) - cursor);
                        break;
                case XK_y: /* paste selection */
+               case XK_Y:
                        XConvertSelection(dpy, (ev->state & ShiftMask) ? clip : XA_PRIMARY,
                                          utf8, utf8, win, CurrentTime);
                        return;
@@ -371,7 +411,7 @@ keypress(XKeyEvent *ev)
                        exit(0);
                }
                if (sel)
-                       sel->out = true;
+                       sel->out = 1;
                break;
        case XK_Right:
                if (text[cursor] != '\0') {
@@ -399,70 +439,6 @@ keypress(XKeyEvent *ev)
        drawmenu();
 }
 
-static void
-match(void)
-{
-       static char **tokv = NULL;
-       static int tokn = 0;
-
-       char buf[sizeof text], *s;
-       int i, tokc = 0;
-       size_t len;
-       Item *item, *lprefix, *lsubstr, *prefixend, *substrend;
-
-       strcpy(buf, text);
-       /* separate input text into tokens to be matched individually */
-       for (s = strtok(buf, " "); s; tokv[tokc - 1] = s, s = strtok(NULL, " "))
-               if (++tokc > tokn && !(tokv = realloc(tokv, ++tokn * sizeof *tokv)))
-                       die("cannot realloc %u bytes\n", tokn * sizeof *tokv);
-       len = tokc ? strlen(tokv[0]) : 0;
-
-       matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL;
-       for (item = items; item && item->text; item++) {
-               for (i = 0; i < tokc; i++)
-                       if (!fstrstr(item->text, tokv[i]))
-                               break;
-               if (i != tokc) /* not all tokens match */
-                       continue;
-               /* exact matches go first, then prefixes, then substrings */
-               if (!tokc || !fstrncmp(tokv[0], item->text, len + 1))
-                       appenditem(item, &matches, &matchend);
-               else if (!fstrncmp(tokv[0], item->text, len))
-                       appenditem(item, &lprefix, &prefixend);
-               else
-                       appenditem(item, &lsubstr, &substrend);
-       }
-       if (lprefix) {
-               if (matches) {
-                       matchend->right = lprefix;
-                       lprefix->left = matchend;
-               } else
-                       matches = lprefix;
-               matchend = prefixend;
-       }
-       if (lsubstr) {
-               if (matches) {
-                       matchend->right = lsubstr;
-                       lsubstr->left = matchend;
-               } else
-                       matches = lsubstr;
-               matchend = substrend;
-       }
-       curr = sel = matches;
-       calcoffsets();
-}
-
-static size_t
-nextrune(int inc)
-{
-       ssize_t n;
-
-       /* return location of next utf8 rune in the given direction (+1 or -1) */
-       for (n = cursor + inc; n + inc >= 0 && (text[n] & 0xc0) == 0x80; n += inc)
-               ;
-       return n;
-}
-
 static void
 paste(void)
 {
@@ -482,8 +458,9 @@ paste(void)
 static void
 readstdin(void)
 {
-       char buf[sizeof text], *p, *maxstr = NULL;
-       size_t i, max = 0, size = 0;
+       char buf[sizeof text], *p;
+       size_t i, imax = 0, size = 0;
+       unsigned int tmpmax = 0;
 
        /* read each line from stdin and add it to the item list */
        for (i = 0; fgets(buf, sizeof buf, stdin); i++) {
@@ -494,13 +471,16 @@ readstdin(void)
                        *p = '\0';
                if (!(items[i].text = strdup(buf)))
                        die("cannot strdup %u bytes:", strlen(buf) + 1);
-               items[i].out = false;
-               if (strlen(items[i].text) > max)
-                       max = strlen(maxstr = items[i].text);
+               items[i].out = 0;
+               drw_font_getexts(drw->fonts, buf, strlen(buf), &tmpmax, NULL);
+               if (tmpmax > inputw) {
+                       inputw = tmpmax;
+                       imax = i;
+               }
        }
        if (items)
                items[i].text = NULL;
-       inputw = maxstr ? TEXTW(maxstr) : 0;
+       inputw = items ? TEXTW(items[imax].text) : 0;
        lines = MIN(lines, i);
 }
 
@@ -547,18 +527,15 @@ setup(void)
 #endif
 
        /* init appearance */
-       scheme[SchemeNorm].bg = drw_clr_create(drw, normbgcolor);
-       scheme[SchemeNorm].fg = drw_clr_create(drw, normfgcolor);
-       scheme[SchemeSel].bg = drw_clr_create(drw, selbgcolor);
-       scheme[SchemeSel].fg = drw_clr_create(drw, selfgcolor);
-       scheme[SchemeOut].bg = drw_clr_create(drw, outbgcolor);
-       scheme[SchemeOut].fg = drw_clr_create(drw, outfgcolor);
+       scheme[SchemeNorm] = drw_scm_create(drw, colors[SchemeNorm], 2);
+       scheme[SchemeSel] = drw_scm_create(drw, colors[SchemeSel], 2);
+       scheme[SchemeOut] = drw_scm_create(drw, colors[SchemeOut], 2);
 
        clip = XInternAtom(dpy, "CLIPBOARD",   False);
        utf8 = XInternAtom(dpy, "UTF8_STRING", False);
 
        /* calculate menu geometry */
-       bh = drw->fonts[0]->h + 2;
+       bh = drw->fonts->h + 2;
        lines = MAX(lines, 0);
        mh = (lines + 1) * bh;
 #ifdef XINERAMA
@@ -566,7 +543,7 @@ setup(void)
                XGetInputFocus(dpy, &w, &di);
                if (mon != -1 && mon < n)
                        i = mon;
-               if (!i && w != root && w != PointerRoot && w != None) {
+               else if (w != root && w != PointerRoot && w != None) {
                        /* find top-level window containing current input focus */
                        do {
                                if (XQueryTree(dpy, (pw = w), &dw, &w, &dws, &du) && dws)
@@ -597,13 +574,13 @@ setup(void)
                y = topbar ? 0 : sh - mh;
                mw = sw;
        }
-       promptw = (prompt && *prompt) ? TEXTW(prompt) : 0;
+       promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0;
        inputw = MIN(inputw, mw/3);
        match();
 
        /* create menu window */
        swa.override_redirect = True;
-       swa.background_pixel = scheme[SchemeNorm].bg->pix;
+       swa.background_pixel = scheme[SchemeNorm][ColBg].pixel;
        swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
        win = XCreateWindow(dpy, root, x, y, mw, mh, 0,
                            DefaultDepth(dpy, screen), CopyFromParent,
@@ -631,18 +608,17 @@ usage(void)
 int
 main(int argc, char *argv[])
 {
-       bool fast = false;
-       int i;
+       int i, fast = 0;
 
        for (i = 1; i < argc; i++)
                /* these options take no arguments */
                if (!strcmp(argv[i], "-v")) {      /* prints version information */
                        puts("dmenu-"VERSION);
                        exit(0);
-               } else if (!strcmp(argv[i], "-b"))   /* appears at the bottom of the screen */
-                       topbar = false;
+               } else if (!strcmp(argv[i], "-b")) /* appears at the bottom of the screen */
+                       topbar = 0;
                else if (!strcmp(argv[i], "-f"))   /* grabs keyboard before reading stdin */
-                       fast = true;
+                       fast = 1;
                else if (!strcmp(argv[i], "-i")) { /* case-insensitive item matching */
                        fstrncmp = strncasecmp;
                        fstrstr = cistrstr;
@@ -658,13 +634,13 @@ main(int argc, char *argv[])
                else if (!strcmp(argv[i], "-fn"))  /* font or font set */
                        fonts[0] = argv[++i];
                else if (!strcmp(argv[i], "-nb"))  /* normal background color */
-                       normbgcolor = argv[++i];
+                       colors[SchemeNorm][ColBg] = argv[++i];
                else if (!strcmp(argv[i], "-nf"))  /* normal foreground color */
-                       normfgcolor = argv[++i];
+                       colors[SchemeNorm][ColFg] = argv[++i];
                else if (!strcmp(argv[i], "-sb"))  /* selected background color */
-                       selbgcolor = argv[++i];
+                       colors[SchemeSel][ColBg] = argv[++i];
                else if (!strcmp(argv[i], "-sf"))  /* selected foreground color */
-                       selfgcolor = argv[++i];
+                       colors[SchemeSel][ColFg] = argv[++i];
                else
                        usage();
 
@@ -677,10 +653,9 @@ main(int argc, char *argv[])
        sw = DisplayWidth(dpy, screen);
        sh = DisplayHeight(dpy, screen);
        drw = drw_create(dpy, screen, root, sw, sh);
-       drw_load_fonts(drw, fonts, LENGTH(fonts));
-       if (!drw->fontcount)
+       if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
                die("no fonts could be loaded.\n");
-       drw_setscheme(drw, &scheme[SchemeNorm]);
+       lrpad = drw->fonts->h;
 
        if (fast) {
                grabkeyboard();