efficient incremental search
43540746
1 file(s) · +19 −18
| 27 | 27 | static void drawmenu(void); |
|
| 28 | 28 | static char *fstrstr(const char *s, const char *sub); |
|
| 29 | 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 | 31 | static void keypress(XKeyEvent *ev); |
|
| 32 | - | static void match(void); |
|
| 32 | + | static void match(Bool sub); |
|
| 33 | 33 | static size_t nextrune(int incr); |
|
| 34 | 34 | static void paste(void); |
|
| 35 | 35 | static void readstdin(void); |
|
| 218 | 218 | } |
|
| 219 | 219 | ||
| 220 | 220 | void |
|
| 221 | - | insert(const char *s, ssize_t n) { |
|
| 221 | + | insert(const char *str, ssize_t n) { |
|
| 222 | 222 | if(strlen(text) + n > sizeof text - 1) |
|
| 223 | 223 | return; |
|
| 224 | 224 | memmove(&text[cursor + n], &text[cursor], sizeof text - cursor - MAX(n, 0)); |
|
| 225 | 225 | if(n > 0) |
|
| 226 | - | memcpy(&text[cursor], s, n); |
|
| 226 | + | memcpy(&text[cursor], str, n); |
|
| 227 | 227 | cursor += n; |
|
| 228 | - | match(); |
|
| 228 | + | match(n > 0); |
|
| 229 | 229 | } |
|
| 230 | 230 | ||
| 231 | 231 | void |
|
| 269 | 269 | break; |
|
| 270 | 270 | case XK_k: /* delete right */ |
|
| 271 | 271 | text[cursor] = '\0'; |
|
| 272 | - | match(); |
|
| 272 | + | match(False); |
|
| 273 | 273 | break; |
|
| 274 | 274 | case XK_n: |
|
| 275 | 275 | ksym = XK_Down; |
|
| 375 | 375 | return; |
|
| 376 | 376 | strncpy(text, sel->text, sizeof text); |
|
| 377 | 377 | cursor = strlen(text); |
|
| 378 | - | match(); |
|
| 378 | + | match(True); |
|
| 379 | 379 | break; |
|
| 380 | 380 | } |
|
| 381 | 381 | drawmenu(); |
|
| 382 | 382 | } |
|
| 383 | 383 | ||
| 384 | 384 | void |
|
| 385 | - | match(void) { |
|
| 385 | + | match(Bool sub) { |
|
| 386 | 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 | - | for(item = items; item && item->text; item++) |
|
| 390 | + | lexact = lprefix = lsubstr = exactend = prefixend = substrend = NULL; |
|
| 391 | + | for(item = sub ? matches : items; item && item->text; item = next) { |
|
| 392 | + | next = sub ? item->right : item + 1; |
|
| 391 | 393 | if(!fstrncmp(text, item->text, len + 1)) |
|
| 392 | 394 | appenditem(item, &lexact, &exactend); |
|
| 393 | 395 | else if(!fstrncmp(text, item->text, len)) |
|
| 394 | 396 | appenditem(item, &lprefix, &prefixend); |
|
| 395 | 397 | else if(fstrstr(item->text, text)) |
|
| 396 | 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 | 403 | if(lprefix) { |
|
| 403 | 404 | if(matchend) { |
|
| 404 | 405 | matchend->right = lprefix; |
|
| 498 | 499 | ||
| 499 | 500 | normcol[ColBG] = getcolor(dc, normbgcolor); |
|
| 500 | 501 | normcol[ColFG] = getcolor(dc, normfgcolor); |
|
| 501 | - | selcol[ColBG] = getcolor(dc, selbgcolor); |
|
| 502 | - | selcol[ColFG] = getcolor(dc, selfgcolor); |
|
| 502 | + | selcol[ColBG] = getcolor(dc, selbgcolor); |
|
| 503 | + | selcol[ColFG] = getcolor(dc, selfgcolor); |
|
| 503 | 504 | ||
| 504 | 505 | utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False); |
|
| 505 | 506 | ||
| 532 | 533 | } |
|
| 533 | 534 | inputw = MIN(inputw, mw/3); |
|
| 534 | 535 | promptw = prompt ? textw(dc, prompt) : 0; |
|
| 535 | - | match(); |
|
| 536 | + | match(False); |
|
| 536 | 537 | ||
| 537 | 538 | /* menu window */ |
|
| 538 | 539 | wa.override_redirect = True; |
|