diff --git a/doc/pages/changelog.asciidoc b/doc/pages/changelog.asciidoc index 1d2cc574..e01c41f5 100644 --- a/doc/pages/changelog.asciidoc +++ b/doc/pages/changelog.asciidoc @@ -3,6 +3,11 @@ This changelog contains major and/or breaking changes to Kakoune between released versions. +== Development version + +* `remove-hooks` argument is now a regex and removes all + hooks whose group matches it. + == Kakoune 2018.09.04 This version contains a significant overhaul of various Kakoune diff --git a/doc/pages/hooks.asciidoc b/doc/pages/hooks.asciidoc index 28c648a2..982b8daf 100644 --- a/doc/pages/hooks.asciidoc +++ b/doc/pages/hooks.asciidoc @@ -30,7 +30,7 @@ remove-hooks ---------------------------- 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. diff --git a/src/commands.cc b/src/commands.cc index 19c02cf6..e94fe090 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -867,7 +867,7 @@ const CommandDesc add_hook_cmd = { const CommandDesc remove_hook_cmd = { "remove-hooks", "rmhooks", - "remove-hooks : remove all hooks whose group is ", + "remove-hooks : remove all hooks whose group matches the regex ", ParameterDesc{ {}, ParameterDesc::Flags::None, 2, 2 }, CommandFlags::None, CommandHelper{}, @@ -888,7 +888,7 @@ const CommandDesc remove_hook_cmd = { }, [](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]}); } }; diff --git a/src/hook_manager.cc b/src/hook_manager.cc index 46d38fb0..93a4fc1e 100644 --- a/src/hook_manager.cc +++ b/src/hook_manager.cc @@ -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)}); } -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) { auto it = std::remove_if(list.value.begin(), list.value.end(), [&](const std::unique_ptr& 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 m_hooks_trash.insert(m_hooks_trash.end(), std::make_move_iterator(it), std::make_move_iterator(list.value.end())); diff --git a/src/hook_manager.hh b/src/hook_manager.hh index 9e75c086..6da5fe30 100644 --- a/src/hook_manager.hh +++ b/src/hook_manager.hh @@ -27,7 +27,7 @@ public: void add_hook(StringView hook_name, String group, HookFlags flags, 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); void run_hook(StringView hook_name, StringView param, Context& context); diff --git a/test/hooks/remove-regex/cmd b/test/hooks/remove-regex/cmd new file mode 100644 index 00000000..9f24a7bc --- /dev/null +++ b/test/hooks/remove-regex/cmd @@ -0,0 +1 @@ +if:remove-hooks global group[12]if diff --git a/test/hooks/remove-regex/in b/test/hooks/remove-regex/in new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/test/hooks/remove-regex/in @@ -0,0 +1 @@ + diff --git a/test/hooks/remove-regex/out b/test/hooks/remove-regex/out new file mode 100644 index 00000000..b82b56ac --- /dev/null +++ b/test/hooks/remove-regex/out @@ -0,0 +1 @@ +foof diff --git a/test/hooks/remove-regex/rc b/test/hooks/remove-regex/rc new file mode 100644 index 00000000..e6dc8dcc --- /dev/null +++ b/test/hooks/remove-regex/rc @@ -0,0 +1,2 @@ +hook global -group group1 InsertChar f %{ exec o } +hook global -group group2 InsertChar f %{ exec o }