dmenu-prefixcompletion-4.6.diff (2428B)
1 From 7ef1d5edfd6bd9c932452ac2fb2010c8e0d8c1c7 Mon Sep 17 00:00:00 2001 2 From: Felix Van der Jeugt <felix.vanderjeugt@gmail.com> 3 Date: Wed, 19 Oct 2016 22:18:21 +0200 4 Subject: [PATCH] tabcomplete with longest common prefix 5 6 --- 7 dmenu.c | 30 +++++++++++++----------------- 8 1 file changed, 13 insertions(+), 17 deletions(-) 9 10 diff --git a/dmenu.c b/dmenu.c 11 index 9278e91..40e717c 100644 12 --- a/dmenu.c 13 +++ b/dmenu.c 14 @@ -218,7 +218,7 @@ match(void) 15 char buf[sizeof text], *s; 16 int i, tokc = 0; 17 size_t len, textsize; 18 - struct item *item, *lprefix, *lsubstr, *prefixend, *substrend; 19 + struct item *item, *lprefix, *prefixend; 20 21 strcpy(buf, text); 22 /* separate input text into tokens to be matched individually */ 23 @@ -227,7 +227,7 @@ match(void) 24 die("cannot realloc %u bytes:", tokn * sizeof *tokv); 25 len = tokc ? strlen(tokv[0]) : 0; 26 27 - matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL; 28 + matches = lprefix = matchend = prefixend = NULL; 29 textsize = strlen(text); 30 for (item = items; item && item->text; item++) { 31 for (i = 0; i < tokc; i++) 32 @@ -240,8 +240,6 @@ match(void) 33 appenditem(item, &matches, &matchend); 34 else if (!fstrncmp(tokv[0], item->text, len)) 35 appenditem(item, &lprefix, &prefixend); 36 - else 37 - appenditem(item, &lsubstr, &substrend); 38 } 39 if (lprefix) { 40 if (matches) { 41 @@ -251,14 +249,6 @@ match(void) 42 matches = lprefix; 43 matchend = prefixend; 44 } 45 - if (lsubstr) { 46 - if (matches) { 47 - matchend->right = lsubstr; 48 - lsubstr->left = matchend; 49 - } else 50 - matches = lsubstr; 51 - matchend = substrend; 52 - } 53 curr = sel = matches; 54 calcoffsets(); 55 } 56 @@ -292,6 +282,7 @@ keypress(XKeyEvent *ev) 57 { 58 char buf[32]; 59 int len; 60 + struct item * item; 61 KeySym ksym = NoSymbol; 62 Status status; 63 64 @@ -447,12 +438,17 @@ keypress(XKeyEvent *ev) 65 } 66 break; 67 case XK_Tab: 68 - if (!sel) 69 - return; 70 - strncpy(text, sel->text, sizeof text - 1); 71 + if (!matches) break; /* cannot complete no matches */ 72 + strncpy(text, matches->text, sizeof text - 1); 73 text[sizeof text - 1] = '\0'; 74 - cursor = strlen(text); 75 - match(); 76 + len = cursor = strlen(text); /* length of longest common prefix */ 77 + for (item = matches; item && item->text; item = item->right) { 78 + cursor = 0; 79 + while (cursor < len && text[cursor] == item->text[cursor]) 80 + cursor++; 81 + len = cursor; 82 + } 83 + memset(text + len, '\0', strlen(text) - len); 84 break; 85 } 86 drawmenu(); 87 -- 88 2.10.1 89