diff -r f6b31468f983 dmenu.c --- a/dmenu.c Sun Jul 24 20:04:58 2011 +0100 +++ b/dmenu.c Sun Sep 11 22:44:05 2011 +0100 @@ -30,7 +30,8 @@ static void grabkeyboard(void); static void insert(const char *str, ssize_t n); static void keypress(XKeyEvent *ev); -static void match(Bool sub); +static void matchstr(Bool sub); +static void matchtok(Bool sub); static size_t nextrune(int inc); static void paste(void); static void readstdin(void); @@ -51,8 +52,9 @@ static const char *selfgcolor = "#ffffff"; static unsigned long normcol[ColLast]; static unsigned long selcol[ColLast]; -static Atom utf8; +static Atom utf8, clip; static Bool topbar = True; +static Bool stable = False; static DC *dc; static Item *items = NULL; static Item *matches, *matchend; @@ -61,6 +63,7 @@ static int (*fstrncmp)(const char *, const char *, size_t) = strncmp; static char *(*fstrstr)(const char *, const char *) = strstr; +static void (*match)(Bool) = matchstr; int main(int argc, char *argv[]) { @@ -81,6 +84,10 @@ fstrncmp = strncasecmp; fstrstr = cistrstr; } + else if(!strcmp(argv[i], "-s")) + stable = True; + else if(!strcmp(argv[i], "-t")) + match = matchtok; else if(i+1 == argc) usage(); /* double flags */ @@ -257,6 +264,9 @@ case XK_u: /* delete left */ insert(NULL, 0 - cursor); break; + case XK_v: /* paste clipboard */ + XConvertSelection(dc->dpy, clip, utf8, utf8, win, CurrentTime); + return; case XK_w: /* delete word */ while(cursor > 0 && text[nextrune(-1)] == ' ') insert(NULL, nextrune(-1) - cursor); @@ -362,7 +372,7 @@ } void -match(Bool sub) { +matchstr(Bool sub) { size_t len = strlen(text); Item *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend; Item *item, *lnext; @@ -370,9 +380,9 @@ lexact = lprefix = lsubstr = exactend = prefixend = substrend = NULL; for(item = sub ? matches : items; item && item->text; item = lnext) { lnext = sub ? item->right : item + 1; - if(!fstrncmp(text, item->text, len + 1)) + if(!stable && !fstrncmp(text, item->text, len + 1)) appenditem(item, &lexact, &exactend); - else if(!fstrncmp(text, item->text, len)) + else if(!stable && !fstrncmp(text, item->text, len)) appenditem(item, &lprefix, &prefixend); else if(fstrstr(item->text, text)) appenditem(item, &lsubstr, &substrend); @@ -402,6 +412,33 @@ calcoffsets(); } +void +matchtok(Bool sub) { + char buf[sizeof text]; + char **tokv, *s; + int tokc, i; + Item *item, *end; + + tokc = 0; + tokv = NULL; + strcpy(buf, text); + for(s = strtok(buf, " "); s; tokv[tokc-1] = s, s = strtok(NULL, " ")) + if(!(tokv = realloc(tokv, ++tokc * sizeof *tokv))) + eprintf("cannot realloc %u bytes\n", tokc * sizeof *tokv); + + matches = end = NULL; + for(item = items; item->text; item++) { + for(i = 0; i < tokc; i++) + if(!fstrstr(item->text, tokv[i])) + break; + if(i == tokc) + appenditem(item, &matches, &end); + } + free(tokv); + curr = prev = next = sel = matches; + calcoffsets(); +} + size_t nextrune(int inc) { ssize_t n; @@ -485,6 +522,7 @@ selcol[ColFG] = getcolor(dc, selfgcolor); utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False); + clip = XInternAtom(dc->dpy, "CLIPBOARD", False); /* menu geometry */ bh = dc->font.height + 2; @@ -532,7 +570,7 @@ void usage(void) { - fputs("usage: dmenu [-b] [-f] [-i] [-l lines] [-p prompt] [-fn font]\n" + fputs("usage: dmenu [-b] [-f] [-i] [-s] [-t] [-l lines] [-p prompt] [-fn font]\n" " [-nb color] [-nf color] [-sb color] [-sf color] [-v]\n", stderr); exit(EXIT_FAILURE); }