sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

dmenu-prefixcompletion-4.8.diff (2645B)


      1 From 39df3b74610c0ddae2e6f49685f49808f91bcc91 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 when hitting tab, the current input will be set to the longest common prefix
      7 of the current selection instead of the first item of the selection
      8 ---
      9  dmenu.c | 32 ++++++++++++++------------------
     10  1 file changed, 14 insertions(+), 18 deletions(-)
     11 
     12 diff --git a/dmenu.c b/dmenu.c
     13 index 5e9c367..6ab0893 100644
     14 --- a/dmenu.c
     15 +++ b/dmenu.c
     16 @@ -218,7 +218,7 @@ match(void)
     17  	char buf[sizeof text], *s;
     18  	int i, tokc = 0;
     19  	size_t len, textsize;
     20 -	struct item *item, *lprefix, *lsubstr, *prefixend, *substrend;
     21 +	struct item *item, *lprefix, *prefixend;
     22  
     23  	strcpy(buf, text);
     24  	/* separate input text into tokens to be matched individually */
     25 @@ -227,8 +227,8 @@ match(void)
     26  			die("cannot realloc %u bytes:", tokn * sizeof *tokv);
     27  	len = tokc ? strlen(tokv[0]) : 0;
     28  
     29 -	matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL;
     30 -	textsize = strlen(text) + 1;
     31 +	matches = lprefix = matchend = prefixend = NULL;
     32 +	textsize = strlen(text);
     33  	for (item = items; item && item->text; item++) {
     34  		for (i = 0; i < tokc; i++)
     35  			if (!fstrstr(item->text, tokv[i]))
     36 @@ -240,8 +240,6 @@ match(void)
     37  			appenditem(item, &matches, &matchend);
     38  		else if (!fstrncmp(tokv[0], item->text, len))
     39  			appenditem(item, &lprefix, &prefixend);
     40 -		else
     41 -			appenditem(item, &lsubstr, &substrend);
     42  	}
     43  	if (lprefix) {
     44  		if (matches) {
     45 @@ -251,14 +249,6 @@ match(void)
     46  			matches = lprefix;
     47  		matchend = prefixend;
     48  	}
     49 -	if (lsubstr) {
     50 -		if (matches) {
     51 -			matchend->right = lsubstr;
     52 -			lsubstr->left = matchend;
     53 -		} else
     54 -			matches = lsubstr;
     55 -		matchend = substrend;
     56 -	}
     57  	curr = sel = matches;
     58  	calcoffsets();
     59  }
     60 @@ -308,6 +298,7 @@ keypress(XKeyEvent *ev)
     61  {
     62  	char buf[32];
     63  	int len;
     64 +	struct item * item;
     65  	KeySym ksym = NoSymbol;
     66  	Status status;
     67  
     68 @@ -481,12 +472,17 @@ keypress(XKeyEvent *ev)
     69  		}
     70  		break;
     71  	case XK_Tab:
     72 -		if (!sel)
     73 -			return;
     74 -		strncpy(text, sel->text, sizeof text - 1);
     75 +		if (!matches) break; /* cannot complete no matches */
     76 +		strncpy(text, matches->text, sizeof text - 1);
     77  		text[sizeof text - 1] = '\0';
     78 -		cursor = strlen(text);
     79 -		match();
     80 +		len = cursor = strlen(text); /* length of longest common prefix */
     81 +		for (item = matches; item && item->text; item = item->right) {
     82 +			cursor = 0;
     83 +			while (cursor < len && text[cursor] == item->text[cursor])
     84 +				cursor++;
     85 +			len = cursor;
     86 +		}
     87 +		memset(text + len, '\0', strlen(text) - len);
     88  		break;
     89  	}
     90  	drawmenu();
     91 -- 
     92 2.16.2
     93