Skip to content

Commit

Permalink
Added ModePush and ModePop hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
laelath committed Mar 8, 2019
1 parent 017b7c9 commit 7efe88b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/hook_manager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ enum class Hook
NormalIdle,
NormalKey,
ModeChange,
ModePop,
ModePush,
RawKey,
WinClose,
WinCreate,
Expand All @@ -60,7 +62,7 @@ enum class Hook

constexpr auto enum_desc(Meta::Type<Hook>)
{
return make_array<EnumDesc<Hook>, 39>({
return make_array<EnumDesc<Hook>, 41>({
{Hook::BufCreate, "BufCreate"},
{Hook::BufNewFile, "BufNewFile"},
{Hook::BufOpenFile, "BufOpenFile"},
Expand Down Expand Up @@ -94,6 +96,8 @@ constexpr auto enum_desc(Meta::Type<Hook>)
{Hook::NormalIdle, "NormalIdle"},
{Hook::NormalKey, "NormalKey"},
{Hook::ModeChange, "ModeChange"},
{Hook::ModePop, "ModePop"},
{Hook::ModePush, "ModePush"},
{Hook::RawKey, "RawKey"},
{Hook::WinClose, "WinClose"},
{Hook::WinCreate, "WinCreate"},
Expand Down
19 changes: 14 additions & 5 deletions src/input_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1516,6 +1516,7 @@ void InputHandler::push_mode(InputMode* new_mode)
new_mode->on_enabled();

context().hooks().run_hook(Hook::ModeChange, format("{}:{}", prev_name, new_mode->name()), context());
context().hooks().run_hook(Hook::ModePush, format("{}:{}", prev_name, new_mode->name()), context());
}

void InputHandler::pop_mode(InputMode* mode)
Expand All @@ -1530,6 +1531,7 @@ void InputHandler::pop_mode(InputMode* mode)
current_mode().on_enabled();

context().hooks().run_hook(Hook::ModeChange, format("{}:{}", prev_name, current_mode().name()), context());
context().hooks().run_hook(Hook::ModePop, format("{}:{}", prev_name, current_mode().name()), context());
}

void InputHandler::reset_normal_mode()
Expand All @@ -1538,12 +1540,19 @@ void InputHandler::reset_normal_mode()
if (m_mode_stack.size() == 1)
return;

StringView prev_name = current_mode().name();
current_mode().on_disabled(false);
m_mode_stack.resize(1);
current_mode().on_enabled();
StringView initial_name = current_mode().name();

context().hooks().run_hook(Hook::ModeChange, format("{}:{}", prev_name, current_mode().name()), context());
while (m_mode_stack.size() > 1)
{
StringView prev_name = current_mode().name();
current_mode().on_disabled(false);
m_mode_stack.pop_back();
current_mode().on_enabled();

context().hooks().run_hook(Hook::ModePop, format("{}:{}", prev_name, current_mode().name()), context());
}

context().hooks().run_hook(Hook::ModeChange, format("{}:{}", initial_name, current_mode().name()), context());
}

void InputHandler::insert(InsertMode mode, int count)
Expand Down

0 comments on commit 7efe88b

Please sign in to comment.