Change remove-hooks to take a regular expression

All hooks whose group match this regex will be removed.

Fixes #2380.
This commit is contained in:
Maxime Coste 2018-09-12 21:24:38 +10:00
parent b8d312cfe0
commit 0c3d9ccd20
9 changed files with 16 additions and 8 deletions

View File

@ -3,6 +3,11 @@
This changelog contains major and/or breaking changes to Kakoune between This changelog contains major and/or breaking changes to Kakoune between
released versions. released versions.
== Development version
* `remove-hooks` <group> argument is now a regex and removes all
hooks whose group matches it.
== Kakoune 2018.09.04 == Kakoune 2018.09.04
This version contains a significant overhaul of various Kakoune This version contains a significant overhaul of various Kakoune

View File

@ -30,7 +30,7 @@ remove-hooks <scope> <group>
---------------------------- ----------------------------
A call to the command above will remove every hooks in *scope* that are part A call to the command above will remove every hooks in *scope* that are part
of the given *group*. of whose group match the given *group* regex.
Hooks declared with the `-once` switch are automatically removed after running. Hooks declared with the `-once` switch are automatically removed after running.

View File

@ -867,7 +867,7 @@ const CommandDesc add_hook_cmd = {
const CommandDesc remove_hook_cmd = { const CommandDesc remove_hook_cmd = {
"remove-hooks", "remove-hooks",
"rmhooks", "rmhooks",
"remove-hooks <scope> <group>: remove all hooks whose group is <group>", "remove-hooks <scope> <group>: remove all hooks whose group matches the regex <group>",
ParameterDesc{ {}, ParameterDesc::Flags::None, 2, 2 }, ParameterDesc{ {}, ParameterDesc::Flags::None, 2, 2 },
CommandFlags::None, CommandFlags::None,
CommandHelper{}, CommandHelper{},
@ -888,7 +888,7 @@ const CommandDesc remove_hook_cmd = {
}, },
[](const ParametersParser& parser, Context& context, const ShellContext&) [](const ParametersParser& parser, Context& context, const ShellContext&)
{ {
get_scope(parser[0], context).hooks().remove_hooks(parser[1]); get_scope(parser[0], context).hooks().remove_hooks(Regex{parser[1]});
} }
}; };

View File

@ -32,15 +32,13 @@ void HookManager::add_hook(StringView hook_name, String group, HookFlags flags,
hooks.emplace_back(new Hook{std::move(group), flags, std::move(filter), std::move(commands)}); hooks.emplace_back(new Hook{std::move(group), flags, std::move(filter), std::move(commands)});
} }
void HookManager::remove_hooks(StringView group) void HookManager::remove_hooks(const Regex& regex)
{ {
if (group.empty())
throw runtime_error("invalid id");
for (auto& list : m_hooks) for (auto& list : m_hooks)
{ {
auto it = std::remove_if(list.value.begin(), list.value.end(), auto it = std::remove_if(list.value.begin(), list.value.end(),
[&](const std::unique_ptr<Hook>& h) [&](const std::unique_ptr<Hook>& h)
{ return h->group == group; }); { return regex_match(h->group.begin(), h->group.end(), regex); });
if (not m_running_hooks.empty()) // we are running some hooks, defer deletion if (not m_running_hooks.empty()) // we are running some hooks, defer deletion
m_hooks_trash.insert(m_hooks_trash.end(), std::make_move_iterator(it), m_hooks_trash.insert(m_hooks_trash.end(), std::make_move_iterator(it),
std::make_move_iterator(list.value.end())); std::make_move_iterator(list.value.end()));

View File

@ -27,7 +27,7 @@ public:
void add_hook(StringView hook_name, String group, HookFlags flags, void add_hook(StringView hook_name, String group, HookFlags flags,
Regex filter, String commands); Regex filter, String commands);
void remove_hooks(StringView group); void remove_hooks(const Regex& regex);
CandidateList complete_hook_group(StringView prefix, ByteCount pos_in_token); CandidateList complete_hook_group(StringView prefix, ByteCount pos_in_token);
void run_hook(StringView hook_name, StringView param, void run_hook(StringView hook_name, StringView param,
Context& context); Context& context);

View File

@ -0,0 +1 @@
if<esc>:remove-hooks global group[12]<ret>if<esc>

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@
foof

View File

@ -0,0 +1,2 @@
hook global -group group1 InsertChar f %{ exec o }
hook global -group group2 InsertChar f %{ exec o }