Skip to content

Instantly share code, notes, and snippets.

@pedrominicz
Last active April 9, 2023 14:56
Show Gist options
  • Save pedrominicz/9c032ace42ec5f0eaa33bac0eb328d9e to your computer and use it in GitHub Desktop.
Save pedrominicz/9c032ace42ec5f0eaa33bac0eb328d9e to your computer and use it in GitHub Desktop.
dmenu config
/* See LICENSE file for copyright and license details. */
/* Default settings; can be overriden by command line. */
static int topbar = 1; /* -b option; if 0, dmenu appears at bottom */
/* -fn option overrides fonts[0]; default X11 font or font set */
static const char *fonts[] = {
"Source Code Pro:style=Bold:pixelsize=16:antialias=true:autohint=true"
};
static const char *prompt = NULL; /* -p option; prompt to the left of input field */
static const char *colors[SchemeLast][2] = {
/* fg bg */
[SchemeNorm] = { "#a8a8a8", "#000000" },
[SchemeSel] = { "#000000", "#a8a8a8" },
[SchemeOut] = { "#000000", "#00ffff" },
};
/* -l option; if nonzero, dmenu uses vertical list with given number of lines */
static unsigned int lines = 0;
/*
* Characters not considered part of a word while deleting words
* for example: " /?\"&[]"
*/
static const char worddelimiters[] = " ";
diff --git a/dmenu.c b/dmenu.c
index 62f1089..67f93b8 100644
--- a/dmenu.c
+++ b/dmenu.c
@@ -236,7 +236,7 @@ match(void)
char buf[sizeof text], *s;
int i, tokc = 0;
size_t len, textsize;
- struct item *item, *lprefix, *lsubstr, *prefixend, *substrend;
+ struct item *item, *lprefix, *prefixend;
strcpy(buf, text);
/* separate input text into tokens to be matched individually */
@@ -245,21 +245,20 @@ match(void)
die("cannot realloc %zu bytes:", tokn * sizeof *tokv);
len = tokc ? strlen(tokv[0]) : 0;
- matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL;
+ matches = lprefix = matchend = prefixend = NULL;
textsize = strlen(text) + 1;
+ textsize = strlen(text);
for (item = items; item && item->text; item++) {
for (i = 0; i < tokc; i++)
if (!fstrstr(item->text, tokv[i]))
break;
if (i != tokc) /* not all tokens match */
continue;
- /* exact matches go first, then prefixes, then substrings */
+ /* exact matches go first, then prefixes */
if (!tokc || !fstrncmp(text, item->text, textsize))
appenditem(item, &matches, &matchend);
else if (!fstrncmp(tokv[0], item->text, len))
appenditem(item, &lprefix, &prefixend);
- else
- appenditem(item, &lsubstr, &substrend);
}
if (lprefix) {
if (matches) {
@@ -269,14 +268,6 @@ match(void)
matches = lprefix;
matchend = prefixend;
}
- if (lsubstr) {
- if (matches) {
- matchend->right = lsubstr;
- lsubstr->left = matchend;
- } else
- matches = lsubstr;
- matchend = substrend;
- }
curr = sel = matches;
calcoffsets();
}
@@ -326,6 +317,7 @@ keypress(XKeyEvent *ev)
{
char buf[64];
int len;
+ struct item *item;
KeySym ksym = NoSymbol;
Status status;
@@ -515,10 +507,17 @@ insert:
}
break;
case XK_Tab:
- if (!sel)
- return;
- cursor = strnlen(sel->text, sizeof text - 1);
- memcpy(text, sel->text, cursor);
+ if (!matches)
+ break; /* cannot complete no matches */
+ /* length of longest common prefix */
+ len = strnlen(matches->text, sizeof text - 1);
+ memcpy(text, matches->text, len);
+ for (item = matches; item && item->text; item = item->right) {
+ cursor = 0;
+ while (cursor < len && tolower((unsigned char)text[cursor]) == tolower((unsigned char)item->text[cursor]))
+ cursor++;
+ len = cursor;
+ }
text[cursor] = '\0';
match();
break;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment