diff --git a/src/client.cc b/src/client.cc index dff40bb7..f4a85ccb 100644 --- a/src/client.cc +++ b/src/client.cc @@ -322,7 +322,7 @@ void Client::on_buffer_reload_key(Key key) { print_status({ format("'{}' is not a valid choice", key_to_str(key)), context().faces()["Error"] }); - m_input_handler.on_next_key(KeymapMode::None, [this](Key key, Context&){ on_buffer_reload_key(key); }); + m_input_handler.on_next_key("buffer-reload", KeymapMode::None, [this](Key key, Context&){ on_buffer_reload_key(key); }); return; } @@ -367,7 +367,7 @@ void Client::check_if_buffer_needs_reloading() bufname), {}, InfoStyle::Modal); m_buffer_reload_dialog_opened = true; - m_input_handler.on_next_key(KeymapMode::None, [this](Key key, Context&){ on_buffer_reload_key(key); }); + m_input_handler.on_next_key("buffer-reload", KeymapMode::None, [this](Key key, Context&){ on_buffer_reload_key(key); }); } else reload_buffer(); diff --git a/src/commands.cc b/src/commands.cc index e8694a92..9dac21a5 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -2110,7 +2110,10 @@ const CommandDesc on_key_cmd = { nullptr, "on-key : wait for next user key and then execute , " "with key available in the `key` value", - single_param, + ParameterDesc{ + { { "mode-name", { true, "set mode name to use" } } }, + ParameterDesc::Flags::None, 1, 1 + }, CommandFlags::None, CommandHelper{}, CommandCompleter{}, @@ -2120,6 +2123,7 @@ const CommandDesc on_key_cmd = { CapturedShellContext sc{shell_context}; context.input_handler().on_next_key( + parser.get_switch("mode-name").value_or("on-key"), KeymapMode::None, [=](Key key, Context& context) mutable { sc.env_vars["key"_sv] = key_to_str(key); ScopedSetBool disable_history{context.history_disabled()}; @@ -2409,9 +2413,9 @@ const CommandDesc declare_user_mode_cmd = { } }; -void enter_user_mode(Context& context, const String mode_name, KeymapMode mode, bool lock) +void enter_user_mode(Context& context, StringView mode_name, KeymapMode mode, bool lock) { - on_next_key_with_autoinfo(context, KeymapMode::None, + on_next_key_with_autoinfo(context, format("user:{}", mode_name), KeymapMode::None, [mode_name, mode, lock](Key key, Context& context) mutable { if (key == Key::Escape) return; diff --git a/src/input_handler.cc b/src/input_handler.cc index 5cb780ee..f4029fcd 100644 --- a/src/input_handler.cc +++ b/src/input_handler.cc @@ -262,7 +262,7 @@ public: } else if (key == '"') { - on_next_key_with_autoinfo(context(), KeymapMode::None, + on_next_key_with_autoinfo(context(), "register", KeymapMode::None, [this](Key key, Context& context) { auto cp = key.codepoint(); if (not cp or key == Key::Escape) @@ -806,7 +806,7 @@ public: } else if (key == ctrl('r')) { - on_next_key_with_autoinfo(context(), KeymapMode::None, + on_next_key_with_autoinfo(context(), "register", KeymapMode::None, [this](Key key, Context&) { auto cp = key.codepoint(); if (not cp or key == Key::Escape) @@ -823,7 +823,7 @@ public: } else if (key == ctrl('v')) { - on_next_key_with_autoinfo(context(), KeymapMode::None, + on_next_key_with_autoinfo(context(), "raw-key", KeymapMode::None, [this](Key key, Context&) { if (auto cp = get_raw_codepoint(key)) { @@ -1100,8 +1100,8 @@ private: class NextKey : public InputMode { public: - NextKey(InputHandler& input_handler, KeymapMode keymap_mode, KeyCallback callback) - : InputMode(input_handler), m_callback(std::move(callback)), m_keymap_mode(keymap_mode) {} + NextKey(InputHandler& input_handler, String name, KeymapMode keymap_mode, KeyCallback callback) + : InputMode(input_handler), m_name{std::move(name)}, m_callback(std::move(callback)), m_keymap_mode(keymap_mode) {} void on_key(Key key) override { @@ -1119,9 +1119,10 @@ public: KeymapMode keymap_mode() const override { return m_keymap_mode; } - StringView name() const override { return "next-key"; } + StringView name() const override { return m_name; } private: + String m_name; KeyCallback m_callback; KeymapMode m_keymap_mode; }; @@ -1267,7 +1268,7 @@ public: insert(*cp); else if (key == ctrl('r')) { - on_next_key_with_autoinfo(context(), KeymapMode::None, + on_next_key_with_autoinfo(context(), "register", KeymapMode::None, [this](Key key, Context&) { auto cp = key.codepoint(); if (not cp or key == Key::Escape) @@ -1296,7 +1297,7 @@ public: } else if (key == ctrl('x')) { - on_next_key_with_autoinfo(context(), KeymapMode::None, + on_next_key_with_autoinfo(context(), "explicit-completion", KeymapMode::None, [this](Key key, Context&) { if (key.key == 'f') m_completer.explicit_file_complete(); @@ -1330,7 +1331,7 @@ public: } else if (key == ctrl('v')) { - on_next_key_with_autoinfo(context(), KeymapMode::None, + on_next_key_with_autoinfo(context(), "raw-insert", KeymapMode::None, [this, transient](Key key, Context&) { if (auto cp = get_raw_codepoint(key)) { @@ -1587,9 +1588,9 @@ void InputHandler::menu(Vector choices, MenuCallback callback) push_mode(new InputModes::Menu(*this, std::move(choices), std::move(callback))); } -void InputHandler::on_next_key(KeymapMode keymap_mode, KeyCallback callback) +void InputHandler::on_next_key(StringView mode_name, KeymapMode keymap_mode, KeyCallback callback) { - push_mode(new InputModes::NextKey(*this, keymap_mode, std::move(callback))); + push_mode(new InputModes::NextKey(*this, format("next-key[{}]", mode_name), keymap_mode, std::move(callback))); } InputHandler::ScopedForceNormal::ScopedForceNormal(InputHandler& handler, NormalParams params) diff --git a/src/input_handler.hh b/src/input_handler.hh index 62c8ca65..3638785b 100644 --- a/src/input_handler.hh +++ b/src/input_handler.hh @@ -80,7 +80,7 @@ public: // execute callback on next keypress and returns to normal mode // if callback does not change the mode itself - void on_next_key(KeymapMode mode, KeyCallback callback); + void on_next_key(StringView mode_name, KeymapMode mode, KeyCallback callback); // process the given key void handle_key(Key key); @@ -175,11 +175,12 @@ bool show_auto_info_ifn(StringView title, StringView info, AutoInfo mask, const void hide_auto_info_ifn(const Context& context, bool hide); template -void on_next_key_with_autoinfo(const Context& context, KeymapMode keymap_mode, Cmd cmd, +void on_next_key_with_autoinfo(const Context& context, StringView mode_name, + KeymapMode keymap_mode, Cmd cmd, StringView title, StringView info) { const bool hide = show_auto_info_ifn(title, info, AutoInfo::OnKey, context); - context.input_handler().on_next_key( + context.input_handler().on_next_key(mode_name, keymap_mode, [hide,cmd](Key key, Context& context) mutable { hide_auto_info_ifn(context, hide); cmd(key, context); diff --git a/src/normal.cc b/src/normal.cc index 66957a2e..84ca070b 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -215,7 +215,7 @@ void goto_commands(Context& context, NormalParams params) } else { - on_next_key_with_autoinfo(context, KeymapMode::Goto, + on_next_key_with_autoinfo(context, "goto", KeymapMode::Goto, [](Key key, Context& context) { auto cp = key.codepoint(); if (not cp or key == Key::Escape) @@ -342,7 +342,7 @@ template void view_commands(Context& context, NormalParams params) { const int count = params.count; - on_next_key_with_autoinfo(context, KeymapMode::View, + on_next_key_with_autoinfo(context, "view", KeymapMode::View, [count](Key key, Context& context) { if (key == Key::Escape) return; @@ -399,7 +399,7 @@ void view_commands(Context& context, NormalParams params) void replace_with_char(Context& context, NormalParams) { - on_next_key_with_autoinfo(context, KeymapMode::None, + on_next_key_with_autoinfo(context, "replace-char", KeymapMode::None, [](Key key, Context& context) { auto cp = key.codepoint(); if (not cp or key == Key::Escape) @@ -1244,7 +1244,7 @@ void select_object(Context& context, NormalParams params) whole ? "" : (flags & ObjectFlags::ToBegin ? " begin" : " end")); }; - on_next_key_with_autoinfo(context, KeymapMode::Object, + on_next_key_with_autoinfo(context,"text-object", KeymapMode::Object, [params](Key key, Context& context) { if (key == Key::Escape) return; @@ -1494,7 +1494,7 @@ void select_to_next_char(Context& context, NormalParams params) flags & SelectFlags::Reverse ? "previous" : "next"); }; - on_next_key_with_autoinfo(context, KeymapMode::None, + on_next_key_with_autoinfo(context, "to-char", KeymapMode::None, [params](Key key, Context& context) { auto cp = key.codepoint(); if (not cp or key == Key::Escape) @@ -1841,7 +1841,7 @@ void combine_selections(Context& context, SelectionList list, Func func, StringV if (&context.buffer() != &list.buffer()) throw runtime_error{"cannot combine selections from different buffers"}; - on_next_key_with_autoinfo(context, KeymapMode::None, + on_next_key_with_autoinfo(context, "compbine-selections", KeymapMode::None, [func, list](Key key, Context& context) mutable { if (key == Key::Escape) return; @@ -1981,7 +1981,7 @@ void move_in_history(Context& context, NormalParams params) void exec_user_mappings(Context& context, NormalParams params) { - on_next_key_with_autoinfo(context, KeymapMode::None, + on_next_key_with_autoinfo(context, "user-mapping", KeymapMode::None, [params](Key key, Context& context) mutable { if (not context.keymaps().is_mapped(key, KeymapMode::User)) return;