aboutsummaryrefslogtreecommitdiff
path: root/dmenu.c
diff options
context:
space:
mode:
Diffstat (limited to 'dmenu.c')
-rw-r--r--dmenu.c57
1 files changed, 34 insertions, 23 deletions
diff --git a/dmenu.c b/dmenu.c
index fd98d54..2bad8d6 100644
--- a/dmenu.c
+++ b/dmenu.c
@@ -30,7 +30,7 @@ static void drawmenu(void);
30static void grabkeyboard(void); 30static void grabkeyboard(void);
31static void insert(const char *str, ssize_t n); 31static void insert(const char *str, ssize_t n);
32static void keypress(XKeyEvent *ev); 32static void keypress(XKeyEvent *ev);
33static void match(Bool sub); 33static void match(void);
34static size_t nextrune(int inc); 34static size_t nextrune(int inc);
35static void paste(void); 35static void paste(void);
36static void readstdin(void); 36static void readstdin(void);
@@ -120,10 +120,10 @@ main(int argc, char *argv[]) {
120 120
121void 121void
122appenditem(Item *item, Item **list, Item **last) { 122appenditem(Item *item, Item **list, Item **last) {
123 if(!*last) 123 if(*last)
124 *list = item;
125 else
126 (*last)->right = item; 124 (*last)->right = item;
125 else
126 *list = item;
127 127
128 item->left = *last; 128 item->left = *last;
129 item->right = NULL; 129 item->right = NULL;
@@ -223,7 +223,7 @@ insert(const char *str, ssize_t n) {
223 if(n > 0) 223 if(n > 0)
224 memcpy(&text[cursor], str, n); 224 memcpy(&text[cursor], str, n);
225 cursor += n; 225 cursor += n;
226 match(n > 0 && text[cursor] == '\0'); 226 match();
227} 227}
228 228
229void 229void
@@ -252,7 +252,7 @@ keypress(XKeyEvent *ev) {
252 252
253 case XK_k: /* delete right */ 253 case XK_k: /* delete right */
254 text[cursor] = '\0'; 254 text[cursor] = '\0';
255 match(False); 255 match();
256 break; 256 break;
257 case XK_u: /* delete left */ 257 case XK_u: /* delete left */
258 insert(NULL, 0 - cursor); 258 insert(NULL, 0 - cursor);
@@ -355,31 +355,42 @@ keypress(XKeyEvent *ev) {
355 return; 355 return;
356 strncpy(text, sel->text, sizeof text); 356 strncpy(text, sel->text, sizeof text);
357 cursor = strlen(text); 357 cursor = strlen(text);
358 match(True); 358 match();
359 break; 359 break;
360 } 360 }
361 drawmenu(); 361 drawmenu();
362} 362}
363 363
364void 364void
365match(Bool sub) { 365match(void) {
366 size_t len = strlen(text); 366 static char **tokv = NULL;
367 Item *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend; 367 static int tokn = 0;
368 Item *item, *lnext; 368
369 369 char buf[sizeof text], *s;
370 lexact = lprefix = lsubstr = exactend = prefixend = substrend = NULL; 370 int i, tokc = 0;
371 for(item = sub ? matches : items; item && item->text; item = lnext) { 371 size_t len;
372 lnext = sub ? item->right : item + 1; 372 Item *item, *lprefix, *lsubstr, *prefixend, *substrend;
373 if(!fstrncmp(text, item->text, len + 1)) 373
374 appenditem(item, &lexact, &exactend); 374 strcpy(buf, text);
375 else if(!fstrncmp(text, item->text, len)) 375 for(s = strtok(buf, " "); s; tokv[tokc-1] = s, s = strtok(NULL, " "))
376 if(++tokc > tokn && !(tokv = realloc(tokv, ++tokn * sizeof *tokv)))
377 eprintf("cannot realloc %u bytes\n", tokn * sizeof *tokv);
378 len = tokc ? strlen(tokv[0]) : 0;
379
380 matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL;
381 for(item = items; item && item->text; item++) {
382 for(i = 0; i < tokc; i++)
383 if(!fstrstr(item->text, tokv[i]))
384 break;
385 if(i != tokc)
386 continue;
387 if(!tokc || !fstrncmp(tokv[0], item->text, len+1))
388 appenditem(item, &matches, &matchend);
389 else if(!fstrncmp(tokv[0], item->text, len))
376 appenditem(item, &lprefix, &prefixend); 390 appenditem(item, &lprefix, &prefixend);
377 else if(fstrstr(item->text, text)) 391 else
378 appenditem(item, &lsubstr, &substrend); 392 appenditem(item, &lsubstr, &substrend);
379 } 393 }
380 matches = lexact;
381 matchend = exactend;
382
383 if(lprefix) { 394 if(lprefix) {
384 if(matchend) { 395 if(matchend) {
385 matchend->right = lprefix; 396 matchend->right = lprefix;
@@ -514,7 +525,7 @@ setup(void) {
514 } 525 }
515 promptw = prompt ? textw(dc, prompt) : 0; 526 promptw = prompt ? textw(dc, prompt) : 0;
516 inputw = MIN(inputw, mw/3); 527 inputw = MIN(inputw, mw/3);
517 match(False); 528 match();
518 529
519 /* menu window */ 530 /* menu window */
520 wa.override_redirect = True; 531 wa.override_redirect = True;