summaryrefslogtreecommitdiff
path: root/src/hook_manager.cc
diff options
context:
space:
mode:
authorFrank LENORMAND <lenormf@gmail.com>2018-08-02 12:52:48 +0300
committerFrank LENORMAND <lenormf@gmail.com>2018-08-06 15:14:20 +0300
commite84dcf72c0e525da8a2d2580488f76b08743752b (patch)
tree3bd775e67f1ae9178cc4aaecb51b0dc986158b9c /src/hook_manager.cc
parentae75032936ed9ffa2bf14589fef115d3d684a7c6 (diff)
src: Allow hooks to be run only once
This commit implements the -once flag on the `:hook` command, which automatically removes a hook after it was run, to avoid having to declare a group and remove it in the hook implementation. Closes #2277
Diffstat (limited to 'src/hook_manager.cc')
-rw-r--r--src/hook_manager.cc14
1 files changed, 12 insertions, 2 deletions
diff --git a/src/hook_manager.cc b/src/hook_manager.cc
index 378756c0..8dabb66a 100644
--- a/src/hook_manager.cc
+++ b/src/hook_manager.cc
@@ -63,7 +63,7 @@ CandidateList HookManager::complete_hook_group(StringView prefix, ByteCount pos_
return res;
}
-void HookManager::run_hook(StringView hook_name, StringView param, Context& context) const
+void HookManager::run_hook(StringView hook_name, StringView param, Context& context)
{
const bool only_always = context.hooks_disabled();
@@ -99,7 +99,7 @@ void HookManager::run_hook(StringView hook_name, StringView param, Context& cont
for (auto& hook : hook_list->value)
{
MatchResults<const char*> captures;
- if ((not only_always or (hook->flags & HookFlags::Always)) and
+ if ((not only_always or (hook->flags & HookFlags::Always)) and
(hook->group.empty() or disabled_hooks.empty() or
not regex_match(hook->group.begin(), hook->group.end(), disabled_hooks))
and regex_match(param.begin(), param.end(), captures, hook->filter))
@@ -123,6 +123,16 @@ void HookManager::run_hook(StringView hook_name, StringView param, Context& cont
CommandManager::instance().execute(to_run.hook->commands, context,
{ {}, std::move(env_vars) });
+
+ if (to_run.hook->flags & HookFlags::Once)
+ {
+ auto it = std::find_if(hook_list->value.begin(), hook_list->value.end(),
+ [&](const std::unique_ptr<Hook>& h)
+ { return h.get() == to_run.hook; });
+
+ m_hooks_trash.push_back(std::move(*it));
+ hook_list->value.erase(it);
+ }
}
catch (runtime_error& err)
{