diff options
author | Connor Lane Smith <cls@lubutu.com> | 2011-05-15 16:05:32 +0100 |
---|---|---|
committer | Connor Lane Smith <cls@lubutu.com> | 2011-05-15 16:05:32 +0100 |
commit | 43540746077d4fd8ef963b5939934d32d59c9417 (patch) | |
tree | 51bb41bdc6e98c5f2fb34aa5730c34fee534db7c | |
parent | 4776d6dc6148498a1eab11dd3a15c03736f15d42 (diff) |
efficient incremental search
-rw-r--r-- | dmenu.c | 37 |
1 files changed, 19 insertions, 18 deletions
@@ -27,9 +27,9 @@ static void calcoffsets(void); | |||
27 | static void drawmenu(void); | 27 | static void drawmenu(void); |
28 | static char *fstrstr(const char *s, const char *sub); | 28 | static char *fstrstr(const char *s, const char *sub); |
29 | static void grabkeyboard(void); | 29 | static void grabkeyboard(void); |
30 | static void insert(const char *s, ssize_t n); | 30 | static void insert(const char *str, ssize_t n); |
31 | static void keypress(XKeyEvent *ev); | 31 | static void keypress(XKeyEvent *ev); |
32 | static void match(void); | 32 | static void match(Bool sub); |
33 | static size_t nextrune(int incr); | 33 | static size_t nextrune(int incr); |
34 | static void paste(void); | 34 | static void paste(void); |
35 | static void readstdin(void); | 35 | static void readstdin(void); |
@@ -218,14 +218,14 @@ grabkeyboard(void) { | |||
218 | } | 218 | } |
219 | 219 | ||
220 | void | 220 | void |
221 | insert(const char *s, ssize_t n) { | 221 | insert(const char *str, ssize_t n) { |
222 | if(strlen(text) + n > sizeof text - 1) | 222 | if(strlen(text) + n > sizeof text - 1) |
223 | return; | 223 | return; |
224 | memmove(&text[cursor + n], &text[cursor], sizeof text - cursor - MAX(n, 0)); | 224 | memmove(&text[cursor + n], &text[cursor], sizeof text - cursor - MAX(n, 0)); |
225 | if(n > 0) | 225 | if(n > 0) |
226 | memcpy(&text[cursor], s, n); | 226 | memcpy(&text[cursor], str, n); |
227 | cursor += n; | 227 | cursor += n; |
228 | match(); | 228 | match(n > 0); |
229 | } | 229 | } |
230 | 230 | ||
231 | void | 231 | void |
@@ -269,7 +269,7 @@ keypress(XKeyEvent *ev) { | |||
269 | break; | 269 | break; |
270 | case XK_k: /* delete right */ | 270 | case XK_k: /* delete right */ |
271 | text[cursor] = '\0'; | 271 | text[cursor] = '\0'; |
272 | match(); | 272 | match(False); |
273 | break; | 273 | break; |
274 | case XK_n: | 274 | case XK_n: |
275 | ksym = XK_Down; | 275 | ksym = XK_Down; |
@@ -375,30 +375,31 @@ keypress(XKeyEvent *ev) { | |||
375 | return; | 375 | return; |
376 | strncpy(text, sel->text, sizeof text); | 376 | strncpy(text, sel->text, sizeof text); |
377 | cursor = strlen(text); | 377 | cursor = strlen(text); |
378 | match(); | 378 | match(True); |
379 | break; | 379 | break; |
380 | } | 380 | } |
381 | drawmenu(); | 381 | drawmenu(); |
382 | } | 382 | } |
383 | 383 | ||
384 | void | 384 | void |
385 | match(void) { | 385 | match(Bool sub) { |
386 | size_t len = strlen(text); | 386 | size_t len = strlen(text); |
387 | Item *item, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend; | 387 | Item *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend; |
388 | Item *item, *next = NULL; | ||
388 | 389 | ||
389 | matches = lexact = lprefix = lsubstr = matchend = exactend = prefixend = substrend = NULL; | 390 | lexact = lprefix = lsubstr = exactend = prefixend = substrend = NULL; |
390 | for(item = items; item && item->text; item++) | 391 | for(item = sub ? matches : items; item && item->text; item = next) { |
392 | next = sub ? item->right : item + 1; | ||
391 | if(!fstrncmp(text, item->text, len + 1)) | 393 | if(!fstrncmp(text, item->text, len + 1)) |
392 | appenditem(item, &lexact, &exactend); | 394 | appenditem(item, &lexact, &exactend); |
393 | else if(!fstrncmp(text, item->text, len)) | 395 | else if(!fstrncmp(text, item->text, len)) |
394 | appenditem(item, &lprefix, &prefixend); | 396 | appenditem(item, &lprefix, &prefixend); |
395 | else if(fstrstr(item->text, text)) | 397 | else if(fstrstr(item->text, text)) |
396 | appenditem(item, &lsubstr, &substrend); | 398 | appenditem(item, &lsubstr, &substrend); |
397 | |||
398 | if(lexact) { | ||
399 | matches = lexact; | ||
400 | matchend = exactend; | ||
401 | } | 399 | } |
400 | matches = lexact; | ||
401 | matchend = exactend; | ||
402 | |||
402 | if(lprefix) { | 403 | if(lprefix) { |
403 | if(matchend) { | 404 | if(matchend) { |
404 | matchend->right = lprefix; | 405 | matchend->right = lprefix; |
@@ -498,8 +499,8 @@ setup(void) { | |||
498 | 499 | ||
499 | normcol[ColBG] = getcolor(dc, normbgcolor); | 500 | normcol[ColBG] = getcolor(dc, normbgcolor); |
500 | normcol[ColFG] = getcolor(dc, normfgcolor); | 501 | normcol[ColFG] = getcolor(dc, normfgcolor); |
501 | selcol[ColBG] = getcolor(dc, selbgcolor); | 502 | selcol[ColBG] = getcolor(dc, selbgcolor); |
502 | selcol[ColFG] = getcolor(dc, selfgcolor); | 503 | selcol[ColFG] = getcolor(dc, selfgcolor); |
503 | 504 | ||
504 | utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False); | 505 | utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False); |
505 | 506 | ||
@@ -532,7 +533,7 @@ setup(void) { | |||
532 | } | 533 | } |
533 | inputw = MIN(inputw, mw/3); | 534 | inputw = MIN(inputw, mw/3); |
534 | promptw = prompt ? textw(dc, prompt) : 0; | 535 | promptw = prompt ? textw(dc, prompt) : 0; |
535 | match(); | 536 | match(False); |
536 | 537 | ||
537 | /* menu window */ | 538 | /* menu window */ |
538 | wa.override_redirect = True; | 539 | wa.override_redirect = True; |