Adding printindex patch

This commit is contained in:
bakkeby 2021-05-15 14:40:05 +02:00
parent 93f5b58d4e
commit 976d6bc04c
5 changed files with 128 additions and 31 deletions

View File

@ -1,21 +1,32 @@
Similar to [dwm-flexipatch](https://github.com/bakkeby/dwm-flexipatch) this dmenu 5.0 (1a13d0, 2020-09-02) project has a different take on patching. It uses preprocessor directives to decide whether or not to include a patch during build time. Essentially this means that this build, for better or worse, contains both the patched _and_ the original code. The aim being that you can select which patches to include and the build will contain that code and nothing more.
Similar to [dwm-flexipatch](https://github.com/bakkeby/dwm-flexipatch) this dmenu 5.0 (1a13d0,
2020-09-02) project has a different take on patching. It uses preprocessor directives to decide
whether or not to include a patch during build time. Essentially this means that this build, for
better or worse, contains both the patched _and_ the original code. The aim being that you can
select which patches to include and the build will contain that code and nothing more.
For example to include the `alpha` patch then you would only need to flip this setting from 0 to 1 in [patches.h](https://github.com/bakkeby/dmenu-flexipatch/blob/master/patches.def.h):
For example to include the `alpha` patch then you would only need to flip this setting from 0
to 1 in [patches.h](https://github.com/bakkeby/dmenu-flexipatch/blob/master/patches.def.h):
```c
#define ALPHA_PATCH 1
```
Once you have found out what works for you and what doesn't then you should be in a better position to choose patches should you want to start patching from scratch.
Once you have found out what works for you and what doesn't then you should be in a better position
to choose patches should you want to start patching from scratch.
Alternatively if you have found the patches you want, but don't want the rest of the flexipatch entanglement on your plate then you may want to have a look at [flexipatch-finalizer](https://github.com/bakkeby/flexipatch-finalizer); a custom pre-processor tool that removes all the unused flexipatch code leaving you with a build that contains the patches you selected.
Alternatively if you have found the patches you want, but don't want the rest of the flexipatch
entanglement on your plate then you may want to have a look at
[flexipatch-finalizer](https://github.com/bakkeby/flexipatch-finalizer); a custom pre-processor
tool that removes all the unused flexipatch code leaving you with a build that contains the patches
you selected.
Refer to [https://tools.suckless.org/dmenu/](https://tools.suckless.org/dmenu/) for details on dmenu, how to install it and how it works.
Refer to [https://tools.suckless.org/dmenu/](https://tools.suckless.org/dmenu/) for details on
dmenu, how to install it and how it works.
---
### Changelog:
2021-05-15 - Added the tsv patch
2021-05-15 - Added the tsv and printindex patches
2020-08-08 - Added the json, symbols, managed, morecolor, multi-selection and preselect patches
@ -25,7 +36,8 @@ Refer to [https://tools.suckless.org/dmenu/](https://tools.suckless.org/dmenu/)
2020-06-10 - Added the case-insensitive patch
2020-05-29 - Added the alpha patch (derived from Baitinq's [build](https://github.com/Baitinq/dmenu)) and the color emoji patch
2020-05-29 - Added the alpha patch (derived from Baitinq's [build](https://github.com/Baitinq/dmenu))
and the color emoji patch
2020-04-05 - Added fuzzyhighlight patch
@ -35,7 +47,9 @@ Refer to [https://tools.suckless.org/dmenu/](https://tools.suckless.org/dmenu/)
2019-10-16 - Introduced [flexipatch-finalizer](https://github.com/bakkeby/flexipatch-finalizer)
2019-09-18 - Added border, center, fuzzymatch, incremental, initialtext, instant, line-height, mouse-support, navhistory, non-blocking-stdin, password, pipeout, printinputtext, rejectnomatch, scroll, vertfull, wmtype and xyw patches
2019-09-18 - Added border, center, fuzzymatch, incremental, initialtext, instant, line-height,
mouse-support, navhistory, non-blocking-stdin, password, pipeout, printinputtext,
rejectnomatch, scroll, vertfull, wmtype and xyw patches
### Patches included:
@ -46,33 +60,42 @@ Refer to [https://tools.suckless.org/dmenu/](https://tools.suckless.org/dmenu/)
- adds a border around the dmenu window
- [case-insensitive](http://tools.suckless.org/dmenu/patches/case-insensitive/)
- makes dmenu case-insensitive by default, replacing the case-insensitive \-i option with a case sensitive \-s option
- makes dmenu case-insensitive by default, replacing the case-insensitive \-i option with a
case sensitive \-s option
- [center](https://tools.suckless.org/dmenu/patches/center/)
- this patch centers dmenu in the middle of the screen
- color_emoji
- enables color emoji in dmenu by removing a workaround for a BadLength error in the Xft library when color glyphs are used
- enabling this will crash dmenu on encountering such glyphs unless you also have an updated Xft library that can handle them
- enables color emoji in dmenu by removing a workaround for a BadLength error in the Xft
library when color glyphs are used
- enabling this will crash dmenu on encountering such glyphs unless you also have an updated
Xft library that can handle them
- [dynamic_options](https://tools.suckless.org/dmenu/patches/dynamicoptions/)
- adds a flag (`-dy`) which makes dmenu run the command given to it whenever input is changed with the current input as the last argument and update the option list according to the output of that command
- adds a flag (`-dy`) which makes dmenu run the command given to it whenever input is changed
with the current input as the last argument and update the option list according to the
output of that command
- [fuzzyhighlight](https://tools.suckless.org/dmenu/patches/fuzzyhighlight/)
- intended to be combined with the fuzzymatch patch, this makes it so that fuzzy matches are highlighted
- intended to be combined with the fuzzymatch patch, this makes it so that fuzzy matches are
highlighted
- [fuzzymatch](https://tools.suckless.org/dmenu/patches/fuzzymatch/)
- adds support for fuzzy-matching to dmenu, allowing users to type non-consecutive portions of the string to be matched
- adds support for fuzzy-matching to dmenu, allowing users to type non-consecutive portions
of the string to be matched
- [grid](https://tools.suckless.org/dmenu/patches/grid/)
- allows dmenu's entries to be rendered in a grid by adding a new `-g` flag to specify the number of grid columns
- allows dmenu's entries to be rendered in a grid by adding a new `-g` flag to specify the
number of grid columns
- the `-g` and `-l` options can be used together to create a G columns * L lines grid
- [highlight](https://tools.suckless.org/dmenu/patches/highlight/)
- this patch highlights the individual characters of matched text for each dmenu list entry
- [highpriority](https://tools.suckless.org/dmenu/patches/highpriority/)
- this patch will automatically sort the search result so that high priority items are shown first
- this patch will automatically sort the search result so that high priority items are shown
first
- [incremental](https://tools.suckless.org/dmenu/patches/incremental/)
- this patch causes dmenu to print out the current text each time a key is pressed
@ -81,7 +104,8 @@ Refer to [https://tools.suckless.org/dmenu/](https://tools.suckless.org/dmenu/)
- adds an option to provide preselected text
- [instant](https://tools.suckless.org/dmenu/patches/instant/)
- adds a flag that will cause dmenu to select an item immediately if there is only one matching option left
- adds a flag that will cause dmenu to select an item immediately if there is only one
matching option left
- [json](https://tools.suckless.org/dmenu/patches/json/)
- adds basic support for json files
@ -91,8 +115,10 @@ Refer to [https://tools.suckless.org/dmenu/](https://tools.suckless.org/dmenu/)
- this helps integrate dmenu with other UI elements that require a particular vertical size
- [managed](https://tools.suckless.org/dmenu/patches/managed/)
- adds a `-wm` flag which sets override_redirect to false; thus letting your window manager manage the dmenu window
- this may be helpful in contexts where you don't want to exclusively bind dmenu or want to treat dmenu more as a "window" rather than as an overlay
- adds a `-wm` flag which sets override_redirect to false; thus letting your window manager
manage the dmenu window
- this may be helpful in contexts where you don't want to exclusively bind dmenu or want to
treat dmenu more as a "window" rather than as an overlay
- [morecolor](https://tools.suckless.org/dmenu/patches/morecolor/)
- adds an additional color scheme for highlighting entries adjacent to the current selection
@ -101,7 +127,8 @@ Refer to [https://tools.suckless.org/dmenu/](https://tools.suckless.org/dmenu/)
- adds basic mouse support for dmenu
- [multi-selection](https://tools.suckless.org/dmenu/patches/multi-selection/)
- without this patch when you press `Ctrl+Enter` dmenu just outputs current item and it is not possible to undo that
- without this patch when you press `Ctrl+Enter` dmenu just outputs current item and it is
not possible to undo that
- with this patch dmenu will output all selected items only on exit
- it is also possible to deselect any selected item
@ -109,18 +136,22 @@ Refer to [https://tools.suckless.org/dmenu/](https://tools.suckless.org/dmenu/)
- provides dmenu the ability for history navigation similar to that of bash
- [non-blocking-stdin](https://tools.suckless.org/dmenu/patches/non_blocking_stdin/)
- this is a patch to have dmenu read stdin in a non blocking way, making it wait for input both from stdin and from X
- this is a patch to have dmenu read stdin in a non blocking way, making it wait for input
both from stdin and from X
- this means that you can continue feeding dmenu while you type
- the patch is meant to be used along with the incremental patch in order to use stdout to feed stdin
- the patch is meant to be used along with the incremental patch in order to use stdout to
feed stdin
- [numbers](https://tools.suckless.org/dmenu/patches/numbers/)
- adds text which displays the number of matched and total items in the top right corner of dmenu
- adds text which displays the number of matched and total items in the top right corner of
dmenu
- [pango](https://github.com/StillANixRookie/dmenu-pango/)
- adds simple markup for dmenu using pango markup
- [password](https://tools.suckless.org/dmenu/patches/password/)
- with this patch dmenu will not directly display the keyboard input, but instead replace it with dots
- with this patch dmenu will not directly display the keyboard input, but instead replace it
with dots
- all data from stdin will be ignored
- [pipeout](https://tools.suckless.org/dmenu/patches/pipeout/)
@ -133,22 +164,32 @@ Refer to [https://tools.suckless.org/dmenu/](https://tools.suckless.org/dmenu/)
- [preselect](https://tools.suckless.org/dmenu/patches/preselect/)
- adds an option `-ps` to preselect an item by providing the index that should be pre-selected
- [printindex](https://tools.suckless.org/dmenu/patches/printindex/)
- allows dmenu to print out the 0-based index of matched text instead of the matched text
itself
- this can be useful in cases where you would like to select entries from one array of text
but index into another, or when you are selecting from an ordered list of non-unique items
- [printinputtext](https://tools.suckless.org/dmenu/patches/printinputtext/)
- this patch adds a flag (-t) which makes Return key ignore selection and print the input text to stdout
- this patch adds a flag (-t) which makes Return key ignore selection and print the input
text to stdout
- the flag basically swaps the functions of Return and Shift+Return hotkeys
- [rejectnomatch](https://tools.suckless.org/dmenu/patches/reject-no-match/)
- adds a new flag to dmenu with which text input will be rejected if it would result in no matching item
- adds a new flag to dmenu with which text input will be rejected if it would result in no
matching item
- [scroll](https://tools.suckless.org/dmenu/patches/scroll/)
- this patch adds support for text scrolling
- it doesn't append '...' for long input anymore as it can handle long text
- [symbols](https://tools.suckless.org/dmenu/patches/symbols/)
- allows the symbols, which are printed in dmenu to indicate that either the input is too long or there are too many options to be shown in dmenu in one line, to be defined
- allows the symbols, which are printed in dmenu to indicate that either the input is too
long or there are too many options to be shown in dmenu in one line, to be defined
- [tsv](https://tools.suckless.org/dmenu/patches/tsv/)
- makes dmenu split input lines at first tab character and only display first part, but it will perform matching on and output full lines as usual
- makes dmenu split input lines at first tab character and only display first part, but it
will perform matching on and output full lines as usual
- can be useful if you want to separate data and representation
- [vertfull](https://tools.suckless.org/dmenu/patches/vertfull/)

39
dmenu.c
View File

@ -92,6 +92,9 @@ struct item {
#if FUZZYMATCH_PATCH
double distance;
#endif // FUZZYMATCH_PATCH
#if PRINTINDEX_PATCH
int index;
#endif // PRINTINDEX_PATCH
};
static char text[BUFSIZ] = "";
@ -117,6 +120,9 @@ static struct item *items = NULL;
static struct item *matches, *matchend;
static struct item *prev, *curr, *next, *sel;
static int mon = -1, screen;
#if PRINTINDEX_PATCH
static int print_index = 0;
#endif // PRINTINDEX_PATCH
#if MANAGED_PATCH
static int managed = 0;
#endif // MANAGED_PATCH
@ -937,6 +943,11 @@ insert:
strncpy(sel->text + strlen(sel->text),pipeout,8);
puts(sel->text+1);
}
#if PRINTINDEX_PATCH
if (print_index)
printf("%d\n", sel->index);
else
#endif // PRINTINDEX_PATCH
puts(sel->text);
} else {
if (text[0] == startpipe[0]) {
@ -948,11 +959,20 @@ insert:
#elif PRINTINPUTTEXT_PATCH
if (use_text_input)
puts((sel && (ev->state & ShiftMask)) ? sel->text : text);
#if PRINTINDEX_PATCH
else if (print_index)
printf("%d\n", (sel && !(ev->state & ShiftMask)) ? sel->index : -1);
#endif // PRINTINDEX_PATCH
else
puts((sel && !(ev->state & ShiftMask)) ? sel->text : text);
#elif PRINTINDEX_PATCH
if (print_index)
printf("%d\n", (sel && !(ev->state & ShiftMask)) ? sel->index : -1);
else
puts((sel && !(ev->state & ShiftMask)) ? sel->text : text);
#else
puts((sel && !(ev->state & ShiftMask)) ? sel->text : text);
#endif // PIPEOUT_PATCH
#endif // PIPEOUT_PATCH | PRINTINPUTTEXT_PATCH | PRINTINDEX_PATCH
#endif // MULTI_SELECTION_PATCH
if (!(ev->state & ControlMask)) {
#if NAVHISTORY_PATCH
@ -1121,12 +1141,21 @@ readstdin(void)
#endif // TSV_PATCH
#if MULTI_SELECTION_PATCH
items[i].id = i; /* for multiselect */
#if PRINTINDEX_PATCH
items[i].index = i;
#endif // PRINTINDEX_PATCH
#elif JSON_PATCH
item->json = NULL;
item->out = 0;
#if PRINTINDEX_PATCH
item->index = i;
#endif // PRINTINDEX_PATCH
#elif PRINTINDEX_PATCH
items[i].index = i;
#else
items[i].out = 0;
#endif // items[i].out = 0;
#endif // MULTI_SELECTION_PATCH | JSON_PATCH | PRINTINDEX_PATCH
#if HIGHPRIORITY_PATCH
items[i].hp = arrayhas(hpitems, hplength, items[i].text);
#endif // HIGHPRIORITY_PATCH
@ -1551,7 +1580,7 @@ main(int argc, char *argv[])
#if CENTER_PATCH
} else if (!strcmp(argv[i], "-c")) { /* toggles centering of dmenu window on screen */
center = !center;
#endif
#endif // CENTER_PATCH
#if !NON_BLOCKING_STDIN_PATCH
} else if (!strcmp(argv[i], "-f")) { /* grabs keyboard before reading stdin */
fast = 1;
@ -1597,6 +1626,10 @@ main(int argc, char *argv[])
} else if (!strcmp(argv[i], "-R")) { /* reject input which results in no match */
reject_no_match = 1;
#endif // REJECTNOMATCH_PATCH
#if PRINTINDEX_PATCH
} else if (!strcmp(argv[i], "-ix")) { /* adds ability to return index in list */
print_index = 1;
#endif // PRINTINDEX_PATCH
} else if (i + 1 == argc)
usage();
/* these options take one argument */

View File

@ -67,7 +67,14 @@ printjsonssel(unsigned int state)
puts(json_string_value(sel->json));
}
} else {
#if PRINTINDEX_PATCH
if (print_index)
printf("%d\n", (sel && !(state & ShiftMask)) ? sel->index : -1);
else
puts((sel && !(state & ShiftMask)) ? sel->text : text);
#else
puts((sel && !(state & ShiftMask)) ? sel->text : text);
#endif // PRINTINDEX_PATCH
}
return 1;
}

View File

@ -12,9 +12,17 @@ printsel(unsigned int state)
{
for (int i = 0;i < selidsize;i++)
if (selid[i] != -1 && (!sel || sel->id != selid[i]))
#if PRINTINDEX_PATCH
printf("%d\n", selid[i]);
#else
puts(items[selid[i]].text);
#endif // PRINTINDEX_PATCH
if (sel && !(state & ShiftMask))
#if PRINTINDEX_PATCH
printf("%d\n", sel->index);
#else
puts(sel->text);
#endif // PRINTINDEX_PATCH
else
puts(text);
}

View File

@ -209,6 +209,14 @@
*/
#define PRESELECT_PATCH 0
/* This patch allows dmenu to print out the 0-based index of matched text instead of the matched
* text itself. This can be useful in cases where you would like to select entries from one array
* of text but index into another, or when you are selecting from an ordered list of non-unique
* items.
* https://tools.suckless.org/dmenu/patches/printindex/
*/
#define PRINTINDEX_PATCH 0
/* This patch adds a flag (-t) which makes Return key to ignore selection and print the input
* text to stdout. The flag basically swaps the functions of Return and Shift+Return hotkeys.
*