1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-11-29 10:02:57 +03:00

Rename Disableable to more general NestedBool

A NestedBool can be set multiple times, and will be considered false
only when unset the same number.
This commit is contained in:
Maxime Coste 2015-08-18 23:16:53 +01:00
parent a33c8d9677
commit 3f493fa186
4 changed files with 45 additions and 43 deletions

View File

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

View File

@ -661,11 +661,11 @@ const CommandDesc add_hook_cmd = {
const String& command = parser[3];
auto hook_func = [=](StringView param, Context& context) {
if (context.user_hooks_support().is_disabled())
if (context.user_hooks_disabled())
return;
// Do not let hooks touch prompt history
ScopedDisable disable_history{context.history_support()};
ScopedSetBool disable_history{context.history_disabled()};
if (regex_match(param.begin(), param.end(), regex))
CommandManager::instance().execute(command, context, {},
@ -1210,7 +1210,7 @@ void context_wrap(const ParametersParser& parser, Context& context, Func func)
DisableOption<bool> disable_incsearch(context, "incsearch");
const bool disable_hooks = parser.get_switch("no-hooks") or
context.user_hooks_support().is_disabled();
context.user_hooks_disabled();
const bool disable_keymaps = not parser.get_switch("with-maps");
ClientManager& cm = ClientManager::instance();
@ -1222,9 +1222,9 @@ void context_wrap(const ParametersParser& parser, Context& context, Func func)
Context& c = input_handler.context();
// Propagate user hooks disabled status to the temporary context
ScopedDisable hook_disable(c.user_hooks_support(), disable_hooks);
ScopedDisable keymaps_disable(c.keymaps_support(), disable_keymaps);
ScopedDisable disable_history{c.history_support()};
ScopedSetBool hook_disable(c.user_hooks_disabled(), disable_hooks);
ScopedSetBool keymaps_disable(c.keymaps_disabled(), disable_keymaps);
ScopedSetBool disable_history{c.history_disabled()};
func(parser, c);
};
@ -1258,9 +1258,9 @@ void context_wrap(const ParametersParser& parser, Context& context, Func func)
if (real_context->is_editing())
c.disable_undo_handling();
ScopedDisable hook_disable(c.user_hooks_support(), disable_hooks);
ScopedDisable keymaps_disable(c.keymaps_support(), disable_keymaps);
ScopedDisable disable_history{c.history_support()};
ScopedSetBool hook_disable(c.user_hooks_disabled(), disable_hooks);
ScopedSetBool keymaps_disable(c.keymaps_disabled(), disable_keymaps);
ScopedSetBool disable_history{c.history_disabled()};
if (parser.get_switch("itersel"))
{
@ -1285,9 +1285,9 @@ void context_wrap(const ParametersParser& parser, Context& context, Func func)
if (parser.get_switch("itersel"))
throw runtime_error("-itersel makes no sense without -draft");
ScopedDisable hook_disable(real_context->user_hooks_support(), disable_hooks);
ScopedDisable keymaps_disable(real_context->keymaps_support(), disable_keymaps);
ScopedDisable disable_history{real_context->history_support()};
ScopedSetBool hook_disable(real_context->user_hooks_disabled(), disable_hooks);
ScopedSetBool keymaps_disable(real_context->keymaps_disabled(), disable_keymaps);
ScopedSetBool disable_history{real_context->history_disabled()};
func(parser, *real_context);
}

View File

@ -18,33 +18,35 @@ class DisplayLine;
class KeymapManager;
class AliasRegistry;
struct Disableable
// bool that can be set (to true) multiple times, and will
// be false only when unset the same time;
struct NestedBool
{
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; }
void set() { m_count++; }
void unset() { kak_assert(m_count > 0); m_count--; }
explicit operator bool() const { return m_count > 0; }
private:
int m_disable_count = 0;
int m_count = 0;
};
struct ScopedDisable
struct ScopedSetBool
{
ScopedDisable(Disableable& disableable, bool condition = true)
: m_disableable(disableable), m_condition(condition)
ScopedSetBool(NestedBool& nested_bool, bool condition = true)
: m_nested_bool(nested_bool), m_condition(condition)
{
if (m_condition)
m_disableable.disable();
m_nested_bool.set();
}
~ScopedDisable()
~ScopedSetBool()
{
if (m_condition)
m_disableable.enable();
m_nested_bool.unset();
}
private:
Disableable& m_disableable;
NestedBool& m_nested_bool;
bool m_condition;
};
@ -122,14 +124,14 @@ public:
bool is_editing() const { return m_edition_level!= 0; }
void disable_undo_handling() { m_edition_level = -1; }
Disableable& user_hooks_support() { return m_user_hooks_support; }
const Disableable& user_hooks_support() const { return m_user_hooks_support; }
NestedBool& user_hooks_disabled() { return m_user_hooks_disabled; }
const NestedBool& user_hooks_disabled() const { return m_user_hooks_disabled; }
Disableable& keymaps_support() { return m_keymaps_support; }
const Disableable& keymaps_support() const { return m_keymaps_support; }
NestedBool& keymaps_disabled() { return m_keymaps_disabled; }
const NestedBool& keymaps_disabled() const { return m_keymaps_disabled; }
Disableable& history_support() { return m_history_support; }
const Disableable& history_support() const { return m_history_support; }
NestedBool& history_disabled() { return m_history_disabled; }
const NestedBool& history_disabled() const { return m_history_disabled; }
Flags flags() const { return m_flags; }
@ -154,9 +156,9 @@ private:
JumpList m_jump_list;
JumpList::iterator m_current_jump = m_jump_list.begin();
Disableable m_user_hooks_support;
Disableable m_keymaps_support;
Disableable m_history_support;
NestedBool m_user_hooks_disabled;
NestedBool m_keymaps_disabled;
NestedBool m_history_disabled;
};
template<>

View File

@ -203,7 +203,7 @@ public:
auto restore_hooks = on_scope_end([&, this]{
if (do_restore_hooks)
{
context().user_hooks_support().enable();
context().user_hooks_disabled().unset();
m_hooks_disabled = false;
}
});
@ -222,7 +222,7 @@ public:
if (not m_hooks_disabled)
{
m_hooks_disabled = true;
context().user_hooks_support().disable();
context().user_hooks_disabled().set();
}
}
else if (key == '"')
@ -621,7 +621,7 @@ public:
if (key == ctrl('m')) // enter
{
if (context().history_support().is_enabled())
if (not context().history_disabled())
history_push(history, line);
context().print_status(DisplayLine{});
if (context().has_ui())
@ -634,7 +634,7 @@ public:
}
else if (key == Key::Escape or key == ctrl('c'))
{
if (context().history_support().is_enabled())
if (not context().history_disabled())
history_push(history, line);
context().print_status(DisplayLine{});
if (context().has_ui())
@ -906,11 +906,11 @@ public:
if (m_autoshowcompl)
m_completer.update();
}},
m_disable_hooks{context().user_hooks_support().is_disabled()}
m_disable_hooks{context().user_hooks_disabled()}
{
// Prolongate hook disabling for the whole insert session
if (m_disable_hooks)
context().user_hooks_support().disable();
context().user_hooks_disabled().set();
last_insert().mode = mode;
last_insert().keys.clear();
@ -929,7 +929,7 @@ public:
selections.avoid_eol();
if (m_disable_hooks)
context().user_hooks_support().enable();
context().user_hooks_disabled().unset();
}
void on_enabled() override
@ -1297,7 +1297,7 @@ void InputHandler::handle_key(Key key)
auto keymap_mode = current_mode().keymap_mode();
KeymapManager& keymaps = m_context.keymaps();
if (keymaps.is_mapped(key, keymap_mode) and
m_context.keymaps_support().is_enabled())
not m_context.keymaps_disabled())
{
for (auto& k : keymaps.get_mapping(key, keymap_mode))
current_mode().handle_key(k);