Introduce InputModeChange hook

InputModeChange <old mode>:<new mode> is intended to replace the various
<Mode>Begin/<Mode>End hooks.

Fixes #1772
This commit is contained in:
Maxime Coste 2017-12-18 11:03:29 +11:00
parent fe49410537
commit e0b28fa421
3 changed files with 33 additions and 7 deletions

View File

@ -120,6 +120,9 @@ of the given *group*.
*RuntimeError* `error message`::
an error was encountered while executing a user command
*InputModeChange* `<old mode>:<new mode>`::
Triggered whenever the current input mode changes
*KakBegin*::
kakoune has started, this hook is called just after reading the user
configuration files

View File

@ -756,7 +756,7 @@ static constexpr auto hooks = {
"InsertBegin", "InsertChar", "InsertDelete", "InsertEnd", "InsertIdle", "InsertKey",
"InsertMove", "InsertCompletionHide", "InsertCompletionShow", "InsertCompletionSelect",
"KakBegin", "KakEnd", "FocusIn", "FocusOut", "RuntimeError", "PromptIdle",
"NormalBegin", "NormalEnd", "NormalIdle", "NormalKey", "RawKey",
"NormalBegin", "NormalEnd", "NormalIdle", "NormalKey", "InputModeChange", "RawKey",
"WinClose", "WinCreate", "WinDisplay", "WinResize", "WinSetOption",
};

View File

@ -39,6 +39,8 @@ public:
virtual KeymapMode keymap_mode() const = 0;
virtual StringView name() const = 0;
virtual std::pair<CursorMode, DisplayCoord> get_cursor_info() const
{
const auto cursor = context().selections().main().cursor();
@ -332,6 +334,8 @@ public:
KeymapMode keymap_mode() const override { return KeymapMode::Normal; }
StringView name() const override { return "normal"; }
private:
friend struct InputHandler::ScopedForceNormal;
@ -652,6 +656,8 @@ public:
KeymapMode keymap_mode() const override { return KeymapMode::Menu; }
StringView name() const override { return "menu"; }
private:
MenuCallback m_callback;
@ -893,6 +899,8 @@ public:
KeymapMode keymap_mode() const override { return KeymapMode::Prompt; }
StringView name() const override { return "prompt"; }
std::pair<CursorMode, DisplayCoord> get_cursor_info() const override
{
DisplayCoord coord{0_line, m_prompt.column_length() + m_line_editor.cursor_display_column()};
@ -1026,6 +1034,8 @@ public:
KeymapMode keymap_mode() const override { return m_keymap_mode; }
StringView name() const override { return "next-key"; }
private:
KeyCallback m_callback;
KeymapMode m_keymap_mode;
@ -1266,6 +1276,8 @@ public:
KeymapMode keymap_mode() const override { return KeymapMode::Insert; }
StringView name() const override { return "insert"; }
private:
template<typename Type>
void move(Type offset)
@ -1395,9 +1407,13 @@ InputHandler::~InputHandler() = default;
void InputHandler::push_mode(InputMode* new_mode)
{
StringView prev_name = current_mode().name();
current_mode().on_disabled(true);
m_mode_stack.emplace_back(new_mode);
new_mode->on_enabled();
context().hooks().run_hook("InputModeChange", format("{}:{}", prev_name, new_mode->name()), context());
}
void InputHandler::pop_mode(InputMode* mode)
@ -1405,20 +1421,27 @@ void InputHandler::pop_mode(InputMode* mode)
kak_assert(m_mode_stack.back().get() == mode);
kak_assert(m_mode_stack.size() > 1);
StringView prev_name = mode->name();
current_mode().on_disabled(false);
m_mode_stack.pop_back();
current_mode().on_enabled();
context().hooks().run_hook("InputModeChange", format("{}:{}", prev_name, current_mode().name()), context());
}
void InputHandler::reset_normal_mode()
{
if (m_mode_stack.size() > 1)
{
kak_assert(dynamic_cast<InputModes::Normal*>(m_mode_stack[0].get()) != nullptr);
if (m_mode_stack.size() == 1)
return;
StringView prev_name = current_mode().name();
current_mode().on_disabled(false);
m_mode_stack.resize(1);
}
kak_assert(dynamic_cast<InputModes::Normal*>(&current_mode()) != nullptr);
current_mode().on_enabled();
context().hooks().run_hook("InputModeChange", format("{}:{}", prev_name, current_mode().name()), context());
}
void InputHandler::insert(InsertMode mode, int count)