From 6b79c1e000ab3f244f0e7f0d576aba77819f2818 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 15 Apr 2019 23:53:47 +1000 Subject: [PATCH] Support named captures in hooks Fixes #2857 --- doc/pages/hooks.asciidoc | 4 +++- src/hook_manager.cc | 3 +++ test/compose/hook-named-captures/cmd | 1 + test/compose/hook-named-captures/in | 1 + test/compose/hook-named-captures/out | 1 + test/compose/hook-named-captures/rc | 1 + 6 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 test/compose/hook-named-captures/cmd create mode 100644 test/compose/hook-named-captures/in create mode 100644 test/compose/hook-named-captures/out create mode 100644 test/compose/hook-named-captures/rc diff --git a/doc/pages/hooks.asciidoc b/doc/pages/hooks.asciidoc index a0f00bf4..8baba215 100644 --- a/doc/pages/hooks.asciidoc +++ b/doc/pages/hooks.asciidoc @@ -189,7 +189,9 @@ vars are available: * `kak_hook_param`: filtering text passed to the currently executing hook -* `kak_hook_param_capture_N`: text captured by the hook filter regex capture N +* `kak_hook_param_capture_N`: text captured by the hook filter regex capturing + group N, N can either be the capturing group number, or its name + (See <>). == Disabling Hooks diff --git a/src/hook_manager.cc b/src/hook_manager.cc index 82f14512..d27bf731 100644 --- a/src/hook_manager.cc +++ b/src/hook_manager.cc @@ -117,6 +117,9 @@ void HookManager::run_hook(Hook hook, StringView param, Context& context) for (size_t i = 0; i < to_run.captures.size(); ++i) env_vars.insert({format("hook_param_capture_{}", i), {to_run.captures[i].first, to_run.captures[i].second}}); + for (auto& c : to_run.hook->filter.impl()->named_captures) + env_vars.insert({format("hook_param_capture_{}", c.name), + {to_run.captures[c.index].first, to_run.captures[c.index].second}}); CommandManager::instance().execute(to_run.hook->commands, context, { {}, std::move(env_vars) }); diff --git a/test/compose/hook-named-captures/cmd b/test/compose/hook-named-captures/cmd new file mode 100644 index 00000000..54aed4f8 --- /dev/null +++ b/test/compose/hook-named-captures/cmd @@ -0,0 +1 @@ +i diff --git a/test/compose/hook-named-captures/in b/test/compose/hook-named-captures/in new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/test/compose/hook-named-captures/in @@ -0,0 +1 @@ + diff --git a/test/compose/hook-named-captures/out b/test/compose/hook-named-captures/out new file mode 100644 index 00000000..4c75d7d2 --- /dev/null +++ b/test/compose/hook-named-captures/out @@ -0,0 +1 @@ +ret diff --git a/test/compose/hook-named-captures/rc b/test/compose/hook-named-captures/rc new file mode 100644 index 00000000..45efdb45 --- /dev/null +++ b/test/compose/hook-named-captures/rc @@ -0,0 +1 @@ +hook global InsertKey '<(?\w+)>' %{ exec "%val(hook_param_capture_name)" }