diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2015-09-25 16:06:41 +0200 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2015-10-05 16:54:05 +0200 |
| commit | 5d66d229e8309849664d9bfe591649ba9e2e4cf3 (patch) | |
| tree | 2b1058e5ef7cff23caa6a8bc889d5ee9680422c5 | |
| parent | 88f65bfc2a704d5bb3f0c95368bdda69bcde447a (diff) | |
| download | vis-5d66d229e8309849664d9bfe591649ba9e2e4cf3.tar.gz vis-5d66d229e8309849664d9bfe591649ba9e2e4cf3.tar.xz | |
vis: add rudimentary :help command
| -rw-r--r-- | config.def.h | 1 | ||||
| -rw-r--r-- | vis.c | 35 |
2 files changed, 36 insertions, 0 deletions
diff --git a/config.def.h b/config.def.h index 822ad75..e84629e 100644 --- a/config.def.h +++ b/config.def.h @@ -36,6 +36,7 @@ static Command cmds[] = { /* command name / optional alias, function, options */ { { "bdelete" }, cmd_bdelete, CMD_OPT_FORCE }, { { "edit" }, cmd_edit, CMD_OPT_FORCE }, + { { "help" }, cmd_help, CMD_OPT_NONE }, { { "new" }, cmd_new, CMD_OPT_NONE }, { { "open" }, cmd_open, CMD_OPT_NONE }, { { "qall" }, cmd_qall, CMD_OPT_FORCE }, @@ -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; |
