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

Ability to change session and client environment variables #4482

Open
alexherbo2 opened this issue Dec 26, 2021 · 0 comments · May be fixed by #5063
Open

Ability to change session and client environment variables #4482

alexherbo2 opened this issue Dec 26, 2021 · 0 comments · May be fixed by #5063

Comments

@alexherbo2
Copy link
Contributor

alexherbo2 commented Dec 26, 2021

I was wondering if we could change the session and client environment variables once Kakoune has started.

Having a way to access and change them could be nice to

  • set and update Kakoune environment without having to restart Kakoune in a new shell,
  • fine-tune tools to Kakoune client-server model.

Kakoune already does some path manipulation (#4433) by prefixing the currently running kak binary in PATH, and it could be a good way to expose shell scripts in Kakoune plugins (#4425).

The proposal is to add

  • set-environment [session|client] <name> <value>,
  • unset-environment [session|client] <name>
  • and %env{name} expansion.

Here is a plugin example with the following files:

bin/:e -> :edit
bin/:edit
bin/:fzf
rc/connect.kak

rc/connect.kak

# Exposes bin in PATH.
set-environment session PATH "%sh{dirname ""$kak_source""}/../bin:%env{PATH}"

# Exposes the session and client environment variables,
# so :edit command has the necessary information to open files in a client/session.
#
# Note: needs a hook to handle session rename.
set-environment session kak_session %val{session}
hook global ClientCreate '.+' %{
  set-environment client kak_client %val{client}
}

bin/:edit

#!/bin/sh

export file
for file do
  echo evaluate-commands -client %env{kak_client} -- edit -- %env{file} |
  kak -p "$kak_session"
  # Note: We use the %env{name} expansion, so we don’t have to “kak quote” values; passing values is straightforward.
  # We expose the `file` environment variable to the `kak` binary, and `kak_client` is already exported.
  #
  # Having `kak -p session_name -args -- 1 2 3` could be nice to pass values in %arg{@} expansion.
done

bin/:fzf

find "$@" -type f | fzf | xargs :edit --

In Kakoune, we can now do:

terminal :fzf

To tune FZF_DEFAULT_OPTS in a client:

set-environment client FZF_DEFAULT_OPTS '--height=40% --layout=reverse --border'

# Similar syntax to `buffer=name` for `set-option`.
set-environment client=client2 FZF_DEFAULT_OPTS '--height=40% --layout=reverse --border'

Assuming we have a Kakoune command to spawn a program, it still convenient for interactive usage without writing a wrapper for every windowing system.

spawn alacritty -e :fzf
spawn tmux split :fzf path/to/dir

Since we have :edit and :fzf in PATH, we can open a terminal from Kakoune and use the commands in the shell.

spawn alacritty

then in the terminal:

:e docs/*.txt
:fzf
krobelus added a commit to krobelus/kakoune that referenced this issue Dec 16, 2023
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.
krobelus added a commit to krobelus/kakoune that referenced this issue Dec 16, 2023
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant