[wiki] [sites] [dmenu] [prefixcompletion]: add variant with flag to toggle behaviour || Martin Tournoij

From: <git_AT_suckless.org>
Date: Mon, 04 Feb 2019 00:30:46 +0100

commit fcf272de4695538954baddcbd48864248f4d4543
Author: Martin Tournoij <martin_AT_arp242.net>
Date: Mon Feb 4 12:29:51 2019 +1300

    [dmenu] [prefixcompletion]: add variant with flag to toggle behaviour

diff --git a/tools.suckless.org/dmenu/patches/prefix-completion/dmenu-prefixcompletion-flag-4.9.diff b/tools.suckless.org/dmenu/patches/prefix-completion/dmenu-prefixcompletion-flag-4.9.diff
new file mode 100644
index 00000000..45c3b290
--- /dev/null
+++ b/tools.suckless.org/dmenu/patches/prefix-completion/dmenu-prefixcompletion-flag-4.9.diff
_AT_@ -0,0 +1,133 @@
+diff --git i/config.def.h w/config.def.h
+index 1edb647..5d312d2 100644
+--- i/config.def.h
++++ w/config.def.h
+_AT_@ -21,3 +21,8 @@ static unsigned int lines = 0;
+ * for example: " /?\"&[]"
+ */
+ static const char worddelimiters[] = " ";
++
++/*
++ * Use prefix matching by default; can be inverted with the -x flag.
++ */
++static int use_prefix = 1;
+diff --git i/dmenu.1 w/dmenu.1
+index 323f93c..429fdfa 100644
+--- i/dmenu.1
++++ w/dmenu.1
+_AT_@ -3,7 +3,7 @@
+ dmenu \- dynamic menu
+ .SH SYNOPSIS
+ .B dmenu
+-.RB [ \-bfiv ]
++.RB [ \-bfivx ]
+ .RB [ \-l
+ .IR lines ]
+ .RB [ \-m
+_AT_@ -78,6 +78,9 @@ defines the selected foreground color.
+ .B \-v
+ prints version information to stdout, then exits.
+ .TP
++.B \-x
++Invert prefix matching setting.
++.TP
+ .BI \-w " windowid"
+ embed into windowid.
+ .SH USAGE
+diff --git i/dmenu.c w/dmenu.c
+index 6b8f51b..3cef454 100644
+--- i/dmenu.c
++++ w/dmenu.c
+_AT_@ -228,8 +228,13 @@ match(void)
+ die("cannot realloc %u bytes:", tokn * sizeof *tokv);
+ len = tokc ? strlen(tokv[0]) : 0;
+
+- matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL;
+- textsize = strlen(text) + 1;
++ if (use_prefix) {
++ matches = lprefix = matchend = prefixend = NULL;
++ textsize = strlen(text);
++ } else {
++ matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL;
++ textsize = strlen(text) + 1;
++ }
+ for (item = items; item && item->text; item++) {
+ for (i = 0; i < tokc; i++)
+ if (!fstrstr(item->text, tokv[i]))
+_AT_@ -241,7 +246,7 @@ match(void)
+ appenditem(item, &matches, &matchend);
+ else if (!fstrncmp(tokv[0], item->text, len))
+ appenditem(item, &lprefix, &prefixend);
+- else
++ else if (!use_prefix)
+ appenditem(item, &lsubstr, &substrend);
+ }
+ if (lprefix) {
+_AT_@ -252,7 +257,7 @@ match(void)
+ matches = lprefix;
+ matchend = prefixend;
+ }
+- if (lsubstr) {
++ if (!use_prefix && lsubstr) {
+ if (matches) {
+ matchend->right = lsubstr;
+ lsubstr->left = matchend;
+_AT_@ -260,6 +265,7 @@ match(void)
+ matches = lsubstr;
+ matchend = substrend;
+ }
++
+ curr = sel = matches;
+ calcoffsets();
+ }
+_AT_@ -309,6 +315,7 @@ keypress(XKeyEvent *ev)
+ {
+ char buf[32];
+ int len;
++ struct item * item;
+ KeySym ksym;
+ Status status;
+
+_AT_@ -487,12 +494,17 @@ insert:
+ }
+ break;
+ case XK_Tab:
+- if (!sel)
+- return;
+- strncpy(text, sel->text, sizeof text - 1);
++ if (!matches) break; /* cannot complete no matches */
++ strncpy(text, matches->text, sizeof text - 1);
+ text[sizeof text - 1] = '+- cursor = strlen(text);
+- match();
++ len = cursor = strlen(text); /* length of longest common prefix */
++ for (item = matches; item && item->text; item = item->right) {
++ cursor = 0;
++ while (cursor < len && text[cursor] == item->text[cursor])
++ cursor++;
++ len = cursor;
++ }
++ memset(text + len, '+ break;
+ }
+
+_AT_@ -682,7 +694,7 @@ setup(void)
+ static void
+ usage(void)
+ {
+- fputs("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]
"
++ fputs("usage: dmenu [-bfivx] [-l lines] [-p prompt] [-fn font] [-m monitor]
"
+ " [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]
", stderr);
+ exit(1);
+ }
+_AT_@ -705,7 +717,9 @@ main(int argc, char *argv[])
+ else if (!strcmp(argv[i], "-i")) { /* case-insensitive item matching */
+ fstrncmp = strncasecmp;
+ fstrstr = cistrstr;
+- } else if (i + 1 == argc)
++ } else if (!strcmp(argv[i], "-x")) /* invert use_prefix */
++ use_prefix = !use_prefix;
++ else if (i + 1 == argc)
+ usage();
+ /* these options take one argument */
+ else if (!strcmp(argv[i], "-l")) /* number of lines in vertical list */
diff --git a/tools.suckless.org/dmenu/patches/prefix-completion/index.md b/tools.suckless.org/dmenu/patches/prefix-completion/index.md
index ff7cc7d8..ad766933 100644
--- a/tools.suckless.org/dmenu/patches/prefix-completion/index.md
+++ b/tools.suckless.org/dmenu/patches/prefix-completion/index.md
_AT_@ -11,15 +11,19 @@ Changes the behaviour of the matched items and the Tab key.
   all matches. E.g. completing "f" with matches "foobar" and "fool" will become
   "foo".
 
+The `-flag` variant adds a `use_prefix` setting and `-x` flag; useful if you
+only want some instances of dmenu to do prefix matching.
+
 Download
 --------
 * For 4.6: [dmenu-prefixcompletion-4.6.diff](dmenu-prefixcompletion-4.6.diff)
 * For 4.7: [dmenu-prefixcompletion-4.7.diff](dmenu-prefixcompletion-4.7.diff)
 * For 4.8: [dmenu-prefixcompletion-4.8.diff](dmenu-prefixcompletion-4.8.diff)
 
+* [dmenu-prefixcompletion-flag-4.9.diff](dmenu-prefixcompletion-flag-4.9.diff)
+
 Authors
 -------
 
 * noctua
-
-
+* Martin Tournoij <martin_AT_arp242.net> – `-x` patch.
Received on Mon Feb 04 2019 - 00:30:46 CET

This archive was generated by hypermail 2.3.0 : Mon Feb 04 2019 - 00:36:26 CET