]> git.armaanb.net Git - dmenu.git/blobdiff - dmenu.c
Add config option for word delimiters
[dmenu.git] / dmenu.c
diff --git a/dmenu.c b/dmenu.c
index 32a8330df9dffe12ee13720df2fa032677bdd54e..e0c2f80444f9fb8b3ad4f864d16e8b358117b29e 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>
@@ -31,7 +31,7 @@ enum { SchemeNorm, SchemeSel, SchemeOut, SchemeLast }; /* color schemes */
 struct item {
        char *text;
        struct item *left, *right;
-       bool out;
+       int out;
 };
 
 static char text[BUFSIZ] = "";
@@ -91,13 +91,13 @@ 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++) {
+               drw_clr_free(scheme[i].bg);
+               drw_clr_free(scheme[i].fg);
+       }
        drw_free(drw);
        XSync(dpy, False);
        XCloseDisplay(dpy);
@@ -207,7 +207,7 @@ match(void)
 
        char buf[sizeof text], *s;
        int i, tokc = 0;
-       size_t len;
+       size_t len, textsize;
        struct item *item, *lprefix, *lsubstr, *prefixend, *substrend;
 
        strcpy(buf, text);
@@ -218,6 +218,7 @@ match(void)
        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]))
@@ -225,7 +226,7 @@ match(void)
                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))
+               if (!tokc || !fstrncmp(text, item->text, textsize))
                        appenditem(item, &matches, &matchend);
                else if (!fstrncmp(tokv[0], item->text, len))
                        appenditem(item, &lprefix, &prefixend);
@@ -313,12 +314,15 @@ 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;
@@ -418,7 +422,7 @@ keypress(XKeyEvent *ev)
                        exit(0);
                }
                if (sel)
-                       sel->out = true;
+                       sel->out = 1;
                break;
        case XK_Right:
                if (text[cursor] != '\0') {
@@ -477,7 +481,7 @@ readstdin(void)
                        *p = '\0';
                if (!(items[i].text = strdup(buf)))
                        die("cannot strdup %u bytes:", strlen(buf) + 1);
-               items[i].out = false;
+               items[i].out = 0;
                if (strlen(items[i].text) > max)
                        max = strlen(maxstr = items[i].text);
        }
@@ -549,7 +553,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)
@@ -614,18 +618,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;