From a6a1c34288535f71fd20f6ec8b2a46a073bd1ed4 Mon Sep 17 00:00:00 2001 From: Delapouite Date: Sun, 25 Feb 2018 16:16:25 +0100 Subject: [PATCH] Add -lock switch to enter-user-mode command --- doc/pages/commands.asciidoc | 3 ++ src/commands.cc | 46 ++++++++++++++++++--------- test/normal/user-modes/cmd | 4 --- test/normal/user-modes/lock/cmd | 1 + test/normal/user-modes/lock/in | 1 + test/normal/user-modes/lock/out | 1 + test/normal/user-modes/lock/rc | 3 ++ test/normal/user-modes/once/cmd | 1 + test/normal/user-modes/{ => once}/in | 0 test/normal/user-modes/{ => once}/out | 0 test/normal/user-modes/once/rc | 3 ++ test/run | 2 +- 12 files changed, 45 insertions(+), 20 deletions(-) delete mode 100644 test/normal/user-modes/cmd create mode 100644 test/normal/user-modes/lock/cmd create mode 100644 test/normal/user-modes/lock/in create mode 100644 test/normal/user-modes/lock/out create mode 100644 test/normal/user-modes/lock/rc create mode 100644 test/normal/user-modes/once/cmd rename test/normal/user-modes/{ => once}/in (100%) rename test/normal/user-modes/{ => once}/out (100%) create mode 100644 test/normal/user-modes/once/rc diff --git a/doc/pages/commands.asciidoc b/doc/pages/commands.asciidoc index 3be1ce1c..73930999 100644 --- a/doc/pages/commands.asciidoc +++ b/doc/pages/commands.asciidoc @@ -135,6 +135,9 @@ command *q!* has to be used). Aliases are mentionned below each commands. *enter-user-mode* :: enable keymap mode for next key + *-lock*::: + stay in mode until `` is pressed + == Hooks *hook* [-group ] :: diff --git a/src/commands.cc b/src/commands.cc index a0b75ef2..818002d4 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -2135,11 +2135,38 @@ const CommandDesc declare_user_mode_cmd = { } }; +void enter_user_mode(Context& context, const String mode_name, KeymapMode mode, bool lock) +{ + on_next_key_with_autoinfo(context, KeymapMode::None, + [mode_name, mode, lock](Key key, Context& context) mutable { + if (key == Key::Escape) + return; + if (not context.keymaps().is_mapped(key, mode)) + return; + + auto& mapping = context.keymaps().get_mapping(key, mode); + ScopedSetBool disable_keymaps(context.keymaps_disabled()); + + InputHandler::ScopedForceNormal force_normal{context.input_handler(), {}}; + + ScopedEdition edition(context); + for (auto& key : mapping.keys) + context.input_handler().handle_key(key); + + if (lock) + enter_user_mode(context, std::move(mode_name), mode, true); + }, lock ? format("{} (lock)", mode_name) : mode_name, + build_autoinfo_for_mapping(context, mode, {})); +} + const CommandDesc enter_user_mode_cmd = { "enter-user-mode", nullptr, - "enter-user-mode : enable keymap mode for next key", - ParameterDesc{ {}, ParameterDesc::Flags::None, 2, 2 }, + "enter-user-mode : enable keymap mode for next key", + ParameterDesc{ + { { "lock", { false, "stay in mode until is pressed" } } }, + ParameterDesc::Flags::SwitchesOnlyAtStart, 2, 2 + }, CommandFlags::None, CommandHelper{}, [](const Context& context, CompletionFlags flags, @@ -2159,22 +2186,11 @@ const CommandDesc enter_user_mode_cmd = { }, [](const ParametersParser& parser, Context& context, const ShellContext&) { + auto lock = (bool)parser.get_switch("lock"); KeymapManager& keymaps = get_scope(parser[0], context).keymaps(); KeymapMode mode = parse_keymap_mode(parser[1], keymaps.user_modes()); - on_next_key_with_autoinfo(context, KeymapMode::None, - [mode](Key key, Context& context) mutable { - if (not context.keymaps().is_mapped(key, mode)) - return; - auto& mapping = context.keymaps().get_mapping(key, mode); - ScopedSetBool disable_keymaps(context.keymaps_disabled()); - - InputHandler::ScopedForceNormal force_normal{context.input_handler(), {}}; - - ScopedEdition edition(context); - for (auto& key : mapping.keys) - context.input_handler().handle_key(key); - }, parser[1], build_autoinfo_for_mapping(context, mode, {})); + enter_user_mode(context, std::move(parser[1]), mode, lock); } }; diff --git a/test/normal/user-modes/cmd b/test/normal/user-modes/cmd deleted file mode 100644 index 455c6ded..00000000 --- a/test/normal/user-modes/cmd +++ /dev/null @@ -1,4 +0,0 @@ -:declare-user-mode global foo -:map global foo f 'wchello from foo' -:enter-user-mode global foo -f diff --git a/test/normal/user-modes/lock/cmd b/test/normal/user-modes/lock/cmd new file mode 100644 index 00000000..8c169dd5 --- /dev/null +++ b/test/normal/user-modes/lock/cmd @@ -0,0 +1 @@ +ffff diff --git a/test/normal/user-modes/lock/in b/test/normal/user-modes/lock/in new file mode 100644 index 00000000..28076c78 --- /dev/null +++ b/test/normal/user-modes/lock/in @@ -0,0 +1 @@ +123delete diff --git a/test/normal/user-modes/lock/out b/test/normal/user-modes/lock/out new file mode 100644 index 00000000..c8b1b423 --- /dev/null +++ b/test/normal/user-modes/lock/out @@ -0,0 +1 @@ +delete diff --git a/test/normal/user-modes/lock/rc b/test/normal/user-modes/lock/rc new file mode 100644 index 00000000..b3114fef --- /dev/null +++ b/test/normal/user-modes/lock/rc @@ -0,0 +1,3 @@ +declare-user-mode global foo +map global foo f d +map global normal ':enter-user-mode -lock global foo' diff --git a/test/normal/user-modes/once/cmd b/test/normal/user-modes/once/cmd new file mode 100644 index 00000000..c86211cb --- /dev/null +++ b/test/normal/user-modes/once/cmd @@ -0,0 +1 @@ +ff diff --git a/test/normal/user-modes/in b/test/normal/user-modes/once/in similarity index 100% rename from test/normal/user-modes/in rename to test/normal/user-modes/once/in diff --git a/test/normal/user-modes/out b/test/normal/user-modes/once/out similarity index 100% rename from test/normal/user-modes/out rename to test/normal/user-modes/once/out diff --git a/test/normal/user-modes/once/rc b/test/normal/user-modes/once/rc new file mode 100644 index 00000000..102a6163 --- /dev/null +++ b/test/normal/user-modes/once/rc @@ -0,0 +1,3 @@ +declare-user-mode global foo +map global foo f 'wchello from foo' +map global normal ':enter-user-mode global foo' diff --git a/test/run b/test/run index 661d3cf7..67367d9f 100755 --- a/test/run +++ b/test/run @@ -19,7 +19,7 @@ main() { eval -buffer *debug* write debug quit! } - try %{ exec "%sh{cat cmd}" } + try %{ exec -with-maps "%sh{cat cmd}" } exec eval -buffer *debug* write debug nop %sh{