From 5d66d229e8309849664d9bfe591649ba9e2e4cf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Fri, 25 Sep 2015 16:06:41 +0200 Subject: vis: add rudimentary :help command --- vis.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'vis.c') diff --git a/vis.c b/vis.c index 249f837..44aa733 100644 --- a/vis.c +++ b/vis.c @@ -455,6 +455,8 @@ static bool cmd_saveas(Filerange*, enum CmdOpt, const char *argv[]); static bool cmd_filter(Filerange*, enum CmdOpt, const char *argv[]); /* switch to the previous/next saved state of the text, chronologically */ static bool cmd_earlier_later(Filerange*, enum CmdOpt, const char *argv[]); +/* dump current key bindings */ +static bool cmd_help(Filerange*, enum CmdOpt, const char *argv[]); static void action_reset(Action *a); static void switchmode_to(Mode *new_mode); @@ -2302,6 +2304,39 @@ static bool cmd_earlier_later(Filerange *range, enum CmdOpt opt, const char *arg return pos != EPOS; } +bool print_keybinding(const char *key, void *value, void *data) { + Text *txt = (Text*)data; + KeyBinding *binding = (KeyBinding*)value; + const char *desc = binding->alias; + if (!desc && binding->action) + desc = binding->action->help; + return text_printf(txt, text_size(txt), " %-15s\t%s\n", key, desc ? desc : ""); +} + +static void print_mode(Mode *mode, Text *txt, bool recursive) { + if (recursive && mode->parent) + print_mode(mode->parent, txt, recursive); + map_iterate(mode->bindings, print_keybinding, txt); +} + +static bool cmd_help(Filerange *range, enum CmdOpt opt, const char *argv[]) { + if (!vis_window_new(NULL)) + return false; + + Text *txt = vis->win->file->text; + for (int i = 0; i < LENGTH(vis_modes); i++) { + Mode *mode = &vis_modes[i]; + if (mode->isuser || i == VIS_MODE_TEXTOBJ) { + text_printf(txt, text_size(txt), "\n%s\n\n", mode->name); + print_mode(mode, txt, i == VIS_MODE_NORMAL || + i == VIS_MODE_INSERT); + } + } + + text_save(txt, NULL); + return true; +} + static Filepos parse_pos(char **cmd) { size_t pos = EPOS; View *view = vis->win->view; -- cgit v1.2.3