vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

commit 96d30e31411fd08e64684515a9441dd52856f72c
parent 2f563197937c86ebb1655e9072fb6de484c31b38
Author: Max Schillinger <maxschillinger@web.de>
Date:   Sat,  8 Jul 2023 19:47:51 +0200

Print keybindings containing space correctly

Fixes #1060 - :help doesn't display mappings starting with <Space>
correctly

Co-authored-by: Randy Palamar <palamar@ualberta.ca>

Diffstat:
Mvis-cmds.c | 24++++++++++++++++++++++--
1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/vis-cmds.c b/vis-cmds.c @@ -632,8 +632,26 @@ static bool cmd_earlier_later(Vis *vis, Win *win, Command *cmd, const char *argv return pos != EPOS; } +static size_t space_replace(char *dest, const char *src, size_t dlen) { + size_t i, invisiblebytes = 0, size = strlen("␣"); + for (i = 0; *src && i < dlen; src++) { + if (*src == ' ' && i < dlen - size - 1) { + memcpy(&dest[i], "␣", size); + i += size; + invisiblebytes += size - 1; + } else { + dest[i] = *src; + i++; + } + } + dest[i] = '\0'; + return invisiblebytes; +} + static bool print_keylayout(const char *key, void *value, void *data) { - return text_appendf(data, " %-18s\t%s\n", key[0] == ' ' ? "␣" : key, (char*)value); + char buf[64]; + size_t invisiblebytes = space_replace(buf, key, sizeof(buf)); + return text_appendf(data, " %-*s\t%s\n", 18+invisiblebytes, buf, (char*)value); } static bool print_keybinding(const char *key, void *value, void *data) { @@ -641,7 +659,9 @@ static bool print_keybinding(const char *key, void *value, void *data) { const char *desc = binding->alias; if (!desc && binding->action) desc = VIS_HELP_USE(binding->action->help); - return text_appendf(data, " %-18s\t%s\n", key[0] == ' ' ? "␣" : key, desc ? desc : ""); + char buf[64]; + size_t invisiblebytes = space_replace(buf, key, sizeof(buf)); + return text_appendf(data, " %-*s\t%s\n", 18+invisiblebytes, buf, desc ? desc : ""); } static void print_mode(Mode *mode, Text *txt) {