1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-09-20 09:19:24 +03:00

Expose hook params regex captures in hook_param_capture_N

This commit is contained in:
Maxime Coste 2017-03-30 11:29:06 +01:00
parent 2cfe3cae36
commit 055ed5ff7e
3 changed files with 14 additions and 3 deletions

View File

@ -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_<name>`: value of the <name> variable in the client environment.
Example: $kak_client_env_SHELL is the SHELL variable

View File

@ -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_<name>*::
value of the *name* variable in the client environment
(e.g. *$kak_client_env_SHELL* is the SHELL variable)

View File

@ -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<const char*> 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);