dmenu-prefixcompletion-4.7.diff (2163B)
1 diff --git a/dmenu.c b/dmenu.c 2 index d605ab4..826604e 100644 3 --- a/dmenu.c 4 +++ b/dmenu.c 5 @@ -218,7 +218,7 @@ match(void) 6 char buf[sizeof text], *s; 7 int i, tokc = 0; 8 size_t len, textsize; 9 - struct item *item, *lprefix, *lsubstr, *prefixend, *substrend; 10 + struct item *item, *lprefix, *prefixend; 11 12 strcpy(buf, text); 13 /* separate input text into tokens to be matched individually */ 14 @@ -227,8 +227,8 @@ match(void) 15 die("cannot realloc %u bytes:", tokn * sizeof *tokv); 16 len = tokc ? strlen(tokv[0]) : 0; 17 18 - matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL; 19 - textsize = strlen(text) + 1; 20 + matches = lprefix = matchend = prefixend = NULL; 21 + textsize = strlen(text); 22 for (item = items; item && item->text; item++) { 23 for (i = 0; i < tokc; i++) 24 if (!fstrstr(item->text, tokv[i])) 25 @@ -240,8 +240,6 @@ match(void) 26 appenditem(item, &matches, &matchend); 27 else if (!fstrncmp(tokv[0], item->text, len)) 28 appenditem(item, &lprefix, &prefixend); 29 - else 30 - appenditem(item, &lsubstr, &substrend); 31 } 32 if (lprefix) { 33 if (matches) { 34 @@ -251,14 +249,6 @@ match(void) 35 matches = lprefix; 36 matchend = prefixend; 37 } 38 - if (lsubstr) { 39 - if (matches) { 40 - matchend->right = lsubstr; 41 - lsubstr->left = matchend; 42 - } else 43 - matches = lsubstr; 44 - matchend = substrend; 45 - } 46 curr = sel = matches; 47 calcoffsets(); 48 } 49 @@ -292,6 +282,7 @@ keypress(XKeyEvent *ev) 50 { 51 char buf[32]; 52 int len; 53 + struct item * item; 54 KeySym ksym = NoSymbol; 55 Status status; 56 57 @@ -447,12 +438,17 @@ keypress(XKeyEvent *ev) 58 } 59 break; 60 case XK_Tab: 61 - if (!sel) 62 - return; 63 - strncpy(text, sel->text, sizeof text - 1); 64 + if (!matches) break; /* cannot complete no matches */ 65 + strncpy(text, matches->text, sizeof text - 1); 66 text[sizeof text - 1] = '\0'; 67 - cursor = strlen(text); 68 - match(); 69 + len = cursor = strlen(text); /* length of longest common prefix */ 70 + for (item = matches; item && item->text; item = item->right) { 71 + cursor = 0; 72 + while (cursor < len && text[cursor] == item->text[cursor]) 73 + cursor++; 74 + len = cursor; 75 + } 76 + memset(text + len, '\0', strlen(text) - len); 77 break; 78 } 79 drawmenu();