Consolidate constext disablable feature in a 'Disableable' helper

This commit is contained in:
Maxime Coste 2014-12-05 13:47:09 +00:00
parent 423030c9bd
commit 1c5975835e
4 changed files with 34 additions and 27 deletions

View File

@ -111,7 +111,7 @@ DisplayLine Client::generate_mode_line() const
status.push_back({ "[recording ("_str + m_input_handler.recording_reg() + ")]", info_face });
if (context().buffer().flags() & Buffer::Flags::New)
status.push_back({ "[new file]", info_face });
if (context().are_user_hooks_disabled())
if (context().user_hooks_support().is_disabled())
status.push_back({ "[no-hooks]", info_face });
if (context().buffer().flags() & Buffer::Flags::Fifo)
status.push_back({ "[fifo]", info_face });

View File

@ -548,7 +548,7 @@ const CommandDesc add_hook_cmd = {
Regex regex(parser[2].begin(), parser[2].end());
String command = parser[3];
auto hook_func = [=](StringView param, Context& context) {
if (context.are_user_hooks_disabled())
if (context.user_hooks_support().is_disabled())
return;
if (regex_match(param.begin(), param.end(), regex))
@ -1016,7 +1016,8 @@ void context_wrap(const ParametersParser& parser, Context& context, Func func)
DisableOption<bool> disable_autoshowcompl(context, "autoshowcompl");
DisableOption<bool> disable_incsearch(context, "incsearch");
const bool disable_hooks = parser.has_option("no-hooks") or context.are_user_hooks_disabled();
const bool disable_hooks = parser.has_option("no-hooks") or
context.user_hooks_support().is_disabled();
const bool disable_keymaps = not parser.has_option("with-maps");
ClientManager& cm = ClientManager::instance();
@ -1029,9 +1030,9 @@ void context_wrap(const ParametersParser& parser, Context& context, Func func)
InputHandler input_handler{{ buffer, Selection{} }};
// Propagate user hooks disabled status to the temporary context
if (disable_hooks)
input_handler.context().disable_user_hooks();
input_handler.context().user_hooks_support().disable();
if (disable_keymaps)
input_handler.context().disable_keymaps();
input_handler.context().keymaps_support().disable();
func(parser, input_handler.context());
}
return;
@ -1058,9 +1059,9 @@ void context_wrap(const ParametersParser& parser, Context& context, Func func)
// Propagate user hooks disabled status to the temporary context
if (disable_hooks)
input_handler.context().disable_user_hooks();
input_handler.context().user_hooks_support().disable();
if (disable_keymaps)
input_handler.context().disable_keymaps();
input_handler.context().keymaps_support().disable();
if (parser.has_option("itersel"))
{
@ -1086,15 +1087,15 @@ void context_wrap(const ParametersParser& parser, Context& context, Func func)
throw runtime_error("-itersel makes no sense without -draft");
if (disable_hooks)
real_context->disable_user_hooks();
real_context->user_hooks_support().disable();
if (disable_keymaps)
real_context->disable_keymaps();
real_context->keymaps_support().disable();
auto restore = on_scope_end([&](){
if (disable_hooks)
real_context->enable_user_hooks();
real_context->user_hooks_support().enable();
if (disable_keymaps)
real_context->enable_keymaps();
real_context->keymaps_support().enable();
});
func(parser, *real_context);

View File

@ -17,6 +17,16 @@ class DisplayLine;
class KeymapManager;
class AliasRegistry;
struct Disableable
{
void disable() { m_disable_count++; }
void enable() { kak_assert(m_disable_count > 0); m_disable_count--; }
bool is_disabled() const { return m_disable_count > 0; }
bool is_enabled() const { return m_disable_count == 0; }
private:
int m_disable_count = 0;
};
// A Context is used to access non singleton objects for various services
// in commands.
//
@ -81,15 +91,11 @@ public:
bool is_editing() const { return m_edition_level!= 0; }
void disable_undo_handling() { m_edition_level = -1; }
bool are_user_hooks_disabled() const { return m_user_hooks_disabled; }
Disableable& user_hooks_support() { return m_user_hooks_support; }
const Disableable& user_hooks_support() const { return m_user_hooks_support; }
void disable_user_hooks() { ++m_user_hooks_disabled; }
void enable_user_hooks() { --m_user_hooks_disabled; }
bool are_keymaps_disabled() const { return m_keymaps_disabled; }
void disable_keymaps() { ++m_keymaps_disabled; }
void enable_keymaps() { --m_keymaps_disabled; }
Disableable& keymaps_support() { return m_keymaps_support; }
const Disableable& keymaps_support() const { return m_keymaps_support; }
private:
void begin_edition();
@ -111,8 +117,8 @@ private:
JumpList m_jump_list;
JumpList::iterator m_current_jump = m_jump_list.begin();
int m_user_hooks_disabled = 0;
int m_keymaps_disabled = 0;
Disableable m_user_hooks_support;
Disableable m_keymaps_support;
};
struct ScopedEdition

View File

@ -95,7 +95,7 @@ public:
auto restore_hooks = on_scope_end([&, this]{
if (do_restore_hooks)
{
context().enable_user_hooks();
context().user_hooks_support().enable();
m_hooks_disabled = false;
}
});
@ -113,7 +113,7 @@ public:
if (not m_hooks_disabled)
{
m_hooks_disabled = true;
context().disable_user_hooks();
context().user_hooks_support().disable();
}
}
else if (key == '"')
@ -778,11 +778,11 @@ public:
if (m_autoshowcompl)
m_completer.update();
}},
m_disable_hooks{context().are_user_hooks_disabled()}
m_disable_hooks{context().user_hooks_support().is_disabled()}
{
// Prolongate hook disabling for the whole insert session
if (m_disable_hooks)
context().disable_user_hooks();
context().user_hooks_support().disable();
last_insert().first = mode;
last_insert().second.clear();
@ -1014,7 +1014,7 @@ private:
selections.avoid_eol();
if (m_disable_hooks)
context().enable_user_hooks();
context().user_hooks_support().enable();
}
enum class Mode { Default, Complete, InsertReg };
@ -1114,7 +1114,7 @@ void InputHandler::handle_key(Key key)
auto keymap_mode = m_mode->keymap_mode();
KeymapManager& keymaps = m_context.keymaps();
if (keymaps.is_mapped(key, keymap_mode) and
not m_context.are_keymaps_disabled())
m_context.keymaps_support().is_enabled())
{
for (auto& k : keymaps.get_mapping(key, keymap_mode))
m_mode->on_key(k);