Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to override environment variables in child processes #5063

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/pages/options.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ are exclusively available to built-in options.
key is provided it removes that entry regardless of the associated
value. +

Any `=` or `\` characters that in `key` and `value` must be escaped as
`\=` or `\\`.

Only `str-to-str-map` options can be created with `declare-option`.

== Builtin options
Expand Down Expand Up @@ -340,6 +343,9 @@ are exclusively available to built-in options.

The default value is '%val{bufname} %val{cursor_line}:%val{cursor_char_column} {{context_info}} {{mode_info}} - %val{client}@[%val{session}]'

*env* `str-to-str-map`::
a list of environment variable overrides to be passed to external processes.

*ui_options* `str-to-str-map`::
a list of `key=value` pairs that are forwarded to the user
interface implementation. The NCurses UI supports the following options:
Expand Down
3 changes: 3 additions & 0 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,9 @@ void register_options()
reg.declare_option<int, check_timeout>(
"fs_check_timeout", "timeout, in milliseconds, between file system buffer modification checks",
500);
reg.declare_option("env",
"a list of environment variable overrides to be passed to external processes",
HashMap<String, String, MemoryDomain::Options>{});
reg.declare_option("ui_options",
"space separated list of <key>=<value> options that are "
"passed to and interpreted by the user interface\n"
Expand Down
2 changes: 2 additions & 0 deletions src/shell_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ Vector<String> generate_env(StringView cmdline, const Context& context, GetValue
static const Regex re(R"(\bkak_(quoted_)?(\w+)\b)");

Vector<String> env;
for (const auto& [key, value] : context.options()["env"].get<HashMap<String, String, MemoryDomain::Options>>())
env.push_back(format("{}={}", key, value));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an ergonomic abstraction; notably it's incomplete in that it does not allow to unset environment variables, and neither does it allow erase the entire environment (though I don't think there is a use case for that).
A more flexible (but probably not better) approach would be to allow overriding KAKOUNE_POSIX_SHELL dynamically, then we can set it to env ... "$@"..

for (auto&& match : RegexIterator{cmdline.begin(), cmdline.end(), re})
{
StringView name{match[2].first, match[2].second};
Expand Down