summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaxime Coste <mawww@kakoune.org>2019-04-15 23:53:47 +1000
committerMaxime Coste <mawww@kakoune.org>2019-04-15 23:56:49 +1000
commit6b79c1e000ab3f244f0e7f0d576aba77819f2818 (patch)
treec161c0c15b525ff790a6fb8cdd5f7eaa6a936649
parentac73f7c27fc0eafc854ac1a3a17a231dee68eb52 (diff)
Support named captures in hooks
Fixes #2857
-rw-r--r--doc/pages/hooks.asciidoc4
-rw-r--r--src/hook_manager.cc3
-rw-r--r--test/compose/hook-named-captures/cmd1
-rw-r--r--test/compose/hook-named-captures/in1
-rw-r--r--test/compose/hook-named-captures/out1
-rw-r--r--test/compose/hook-named-captures/rc1
6 files changed, 10 insertions, 1 deletions
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 <<regex#groups,`:doc regex groups`>>).
== 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<ret><esc>
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 '<(?<name>\w+)>' %{ exec "<backspace>%val(hook_param_capture_name)" }