Add -lock switch to enter-user-mode command

This commit is contained in:
Delapouite 2018-02-25 16:16:25 +01:00
parent 0d838f80a0
commit a6a1c34288
12 changed files with 45 additions and 20 deletions

View File

@ -135,6 +135,9 @@ command *q!* has to be used). Aliases are mentionned below each commands.
*enter-user-mode* <scope> <name>::
enable <name> keymap mode for next key
*-lock*:::
stay in mode until `<esc>` is pressed
== Hooks
*hook* [-group <group>] <scope> <hook_name> <filtering_regex> <command>::

View File

@ -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 <scope> <name>: enable <name> keymap mode for next key",
ParameterDesc{ {}, ParameterDesc::Flags::None, 2, 2 },
"enter-user-mode <switches> <scope> <name>: enable <name> keymap mode for next key",
ParameterDesc{
{ { "lock", { false, "stay in mode until <esc> 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);
}
};

View File

@ -1,4 +0,0 @@
:declare-user-mode global foo<ret>
:map global foo f '<esc>wchello from foo<esc>'
:enter-user-mode global foo
f

View File

@ -0,0 +1 @@
<a-,>fff<esc>f

View File

@ -0,0 +1 @@
123delete

View File

@ -0,0 +1 @@
delete

View File

@ -0,0 +1,3 @@
declare-user-mode global foo
map global foo f d
map global normal <a-,> ':enter-user-mode -lock global foo<ret>'

View File

@ -0,0 +1 @@
<a-,>ff

View File

@ -0,0 +1,3 @@
declare-user-mode global foo
map global foo f 'wchello from foo<esc>'
map global normal <a-,> ':enter-user-mode global foo<ret>'

View File

@ -19,7 +19,7 @@ main() {
eval -buffer *debug* write debug
quit!
}
try %{ exec "%sh{cat cmd}" }
try %{ exec -with-maps "%sh{cat cmd}" }
exec <c-l>
eval -buffer *debug* write debug
nop %sh{