diff options
Diffstat (limited to 'dmenu.c')
-rw-r--r-- | dmenu.c | 61 |
1 files changed, 43 insertions, 18 deletions
@@ -35,13 +35,12 @@ typedef struct { | |||
35 | typedef struct Item Item; | 35 | typedef struct Item Item; |
36 | struct Item { | 36 | struct Item { |
37 | char *text; | 37 | char *text; |
38 | Bool matched; | ||
39 | Item *next; /* traverses all items */ | 38 | Item *next; /* traverses all items */ |
40 | Item *left, *right; /* traverses items matching current search pattern */ | 39 | Item *left, *right; /* traverses items matching current search pattern */ |
41 | }; | 40 | }; |
42 | 41 | ||
43 | /* forward declarations */ | 42 | /* forward declarations */ |
44 | Item *appenditem(Item *i, Item *last); | 43 | void appenditem(Item *i, Item **list, Item **last); |
45 | void calcoffsets(void); | 44 | void calcoffsets(void); |
46 | char *cistrstr(const char *s, const char *sub); | 45 | char *cistrstr(const char *s, const char *sub); |
47 | void cleanup(void); | 46 | void cleanup(void); |
@@ -92,17 +91,15 @@ Window root, win; | |||
92 | int (*fstrncmp)(const char *, const char *, size_t n) = strncmp; | 91 | int (*fstrncmp)(const char *, const char *, size_t n) = strncmp; |
93 | char *(*fstrstr)(const char *, const char *) = strstr; | 92 | char *(*fstrstr)(const char *, const char *) = strstr; |
94 | 93 | ||
95 | Item * | 94 | void |
96 | appenditem(Item *i, Item *last) { | 95 | appenditem(Item *i, Item **list, Item **last) { |
97 | if(!last) | 96 | if(!(*last)) |
98 | item = i; | 97 | *list = i; |
99 | else | 98 | else |
100 | last->right = i; | 99 | (*last)->right = i; |
101 | i->left = last; | 100 | i->left = *last; |
102 | i->right = NULL; | 101 | i->right = NULL; |
103 | last = i; | 102 | *last = i; |
104 | nitem++; | ||
105 | return last; | ||
106 | } | 103 | } |
107 | 104 | ||
108 | void | 105 | void |
@@ -521,19 +518,47 @@ kpress(XKeyEvent * e) { | |||
521 | void | 518 | void |
522 | match(char *pattern) { | 519 | match(char *pattern) { |
523 | unsigned int plen; | 520 | unsigned int plen; |
524 | Item *i, *j; | 521 | Item *i, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend; |
525 | 522 | ||
526 | if(!pattern) | 523 | if(!pattern) |
527 | return; | 524 | return; |
528 | plen = strlen(pattern); | 525 | plen = strlen(pattern); |
529 | item = j = NULL; | 526 | item = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL; |
530 | nitem = 0; | 527 | nitem = 0; |
531 | for(i = allitems; i; i = i->next) | 528 | for(i = allitems; i; i = i->next) |
532 | if((i->matched = !fstrncmp(pattern, i->text, plen))) | 529 | if(!fstrncmp(pattern, i->text, plen + 1)) { |
533 | j = appenditem(i, j); | 530 | appenditem(i, &lexact, &exactend); |
534 | for(i = allitems; i; i = i->next) | 531 | nitem++; |
535 | if(!i->matched && fstrstr(i->text, pattern)) | 532 | } |
536 | j = appenditem(i, j); | 533 | else if(!fstrncmp(pattern, i->text, plen)) { |
534 | appenditem(i, &lprefix, &prefixend); | ||
535 | nitem++; | ||
536 | } | ||
537 | else if(fstrstr(i->text, pattern)) { | ||
538 | appenditem(i, &lsubstr, &substrend); | ||
539 | nitem++; | ||
540 | } | ||
541 | if(lexact) { | ||
542 | item = lexact; | ||
543 | itemend = exactend; | ||
544 | } | ||
545 | if(lprefix) { | ||
546 | if(itemend) { | ||
547 | itemend->right - lprefix; | ||
548 | lprefix->left = itemend; | ||
549 | } | ||
550 | else | ||
551 | item = lprefix; | ||
552 | itemend = prefixend; | ||
553 | } | ||
554 | if(lsubstr) { | ||
555 | if(itemend) { | ||
556 | itemend->right = lsubstr; | ||
557 | lsubstr->left = itemend; | ||
558 | } | ||
559 | else | ||
560 | item = lsubstr; | ||
561 | } | ||
537 | curr = prev = next = sel = item; | 562 | curr = prev = next = sel = item; |
538 | calcoffsets(); | 563 | calcoffsets(); |
539 | } | 564 | } |