Skip to content

Commit a7e035d

Browse files
committed
Option to override environment variables in child processes
Today I have to restart Kakoune in order to set environment variables in shell expansions globally. For example KAK_LSP_FORCE_PROJECT_ROOT. When running ":git" commands, there is an ambiguity on whether to use the $PWD or the buffer's worktree. We use $PWD but it should be possible to override this behavior. An obvious way to do this is to override Git environment variables: set-option -add global env GIT_WORK_TREE=/path/to/my/repo GIT_DIR=/path/to/my/repo.git (another in-flight patch adds the obvious default to GIT_DIR so we don't need to set that). --- There are some minor issues left - If a user sets PATH, this will stomp the one we setenv()'d. - Today both key and value require escaping for = and \. This is not intuitive. Since neither environment variables nor ui_options ever need a = in the key name, we can get rid of the escaping. Alternative approach: The str-to-str-map can be somewhat clunky. We could instead export any option named like env_FOO. Not yet sure which approach is better. Closes mawww#4482 That issues asks for a separate client-specific env as well but we don't have a client scope so I'm not sure.
1 parent 83fb65a commit a7e035d

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

Diff for: doc/pages/options.asciidoc

+6
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ are exclusively available to built-in options.
189189
key is provided it removes that entry regardless of the associated
190190
value. +
191191

192+
Any `=` or `\` characters that in `key` and `value` must be escaped as
193+
`\=` or `\\`.
194+
192195
Only `str-to-str-map` options can be created with `declare-option`.
193196

194197
== Builtin options
@@ -340,6 +343,9 @@ are exclusively available to built-in options.
340343

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

346+
*env* `str-to-str-map`::
347+
a list of environment variable overrides to be passed to external processes.
348+
343349
*ui_options* `str-to-str-map`::
344350
a list of `key=value` pairs that are forwarded to the user
345351
interface implementation. The NCurses UI supports the following options:

Diff for: src/main.cc

+3
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,9 @@ void register_options()
574574
reg.declare_option<int, check_timeout>(
575575
"fs_check_timeout", "timeout, in milliseconds, between file system buffer modification checks",
576576
500);
577+
reg.declare_option("env",
578+
"a list of environment variable overrides to be passed to external processes",
579+
HashMap<String, String, MemoryDomain::Options>{});
577580
reg.declare_option("ui_options",
578581
"space separated list of <key>=<value> options that are "
579582
"passed to and interpreted by the user interface\n"

Diff for: src/shell_manager.cc

+2
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ Vector<String> generate_env(StringView cmdline, const Context& context, GetValue
146146
static const Regex re(R"(\bkak_(quoted_)?(\w+)\b)");
147147

148148
Vector<String> env;
149+
for (const auto& [key, value] : context.options()["env"].get<HashMap<String, String, MemoryDomain::Options>>())
150+
env.push_back(format("{}={}", key, value));
149151
for (auto&& match : RegexIterator{cmdline.begin(), cmdline.end(), re})
150152
{
151153
StringView name{match[2].first, match[2].second};

0 commit comments

Comments
 (0)