diff --git a/README.asciidoc b/README.asciidoc index 5cf32d7d..f8082567 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -800,6 +800,7 @@ Some of Kakoune state is available through environment variables: * `kak_window_width`: width of the current kakoune window * `kak_window_height`: height of the current kakoune window * `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_client_env_`: value of the variable in the client environment. Example: $kak_client_env_SHELL is the SHELL variable diff --git a/doc/manpages/expansions.asciidoc b/doc/manpages/expansions.asciidoc index f4f16974..b1d08762 100644 --- a/doc/manpages/expansions.asciidoc +++ b/doc/manpages/expansions.asciidoc @@ -101,6 +101,8 @@ informations about Kakoune's state: height of the current kakoune window *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_client_env_*:: value of the *name* variable in the client environment (e.g. *$kak_client_env_SHELL* is the SHELL variable) diff --git a/src/commands.cc b/src/commands.cc index 2c21c78d..500d37b5 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -800,15 +800,23 @@ const CommandDesc add_hook_cmd = { if (not contains(hooks, parser[1])) throw runtime_error{format("Unknown hook '{}'", parser[1])}; - Regex regex(parser[2], Regex::optimize | Regex::nosubs | Regex::ECMAScript); + Regex regex{parser[2], Regex::optimize | Regex::ECMAScript}; const String& command = parser[3]; auto hook_func = [=](StringView param, Context& context) { ScopedSetBool disable_history{context.history_disabled()}; - if (regex_match(param.begin(), param.end(), regex)) + MatchResults res; + if (regex_match(param.begin(), param.end(), res, regex)) + { + EnvVarMap env_vars{ {"hook_param", param.str()} }; + for (size_t i = 0; i < res.size(); ++i) + env_vars.insert({format("hook_param_capture_{}", i), + {res[i].first, res[i].second}}); + CommandManager::instance().execute(command, context, - { {}, { { "hook_param", param.str() } } }); + { {}, std::move(env_vars) }); + } }; auto group = parser.get_switch("group").value_or(StringView{}); get_scope(parser[0], context).hooks().add_hook(parser[1], group.str(), hook_func);