diff --git a/docs/changelog.rst b/docs/changelog.rst index 4f723464c..49f89f8e6 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -11,6 +11,9 @@ To update |kitty|, :doc:`follow the instructions `. scrollback buffer onto the screen. Useful, for example, to restore the screen after showing completions below the shell prompt. +- A new remote control command :ref:`at_env` to change the default + environment passed to newly created windows (:iss:`3529`) + - Linux: Fix binary kitty builds not able to load fonts in WOFF2 format (:iss:`3506`) diff --git a/kitty/rc/env.py b/kitty/rc/env.py new file mode 100644 index 000000000..336f9d6e1 --- /dev/null +++ b/kitty/rc/env.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python +# vim:fileencoding=utf-8 +# License: GPLv3 Copyright: 2020, Kovid Goyal + +from typing import Any, Optional + +from .base import ( + ArgsType, Boss, PayloadGetType, PayloadType, RCOptions, RemoteCommand, + ResponseType, Window +) + + +class Env(RemoteCommand): + + ''' + env+: dictionary of environment variables to values. Empty values cause the variable to be deleted. + ''' + + short_desc = 'Change environment variables seen by future children' + desc = ( + 'Change the environment variables seen by processing in newly launched windows.' + ' Similar to the :opt:`env` option in kitty.conf, but affects running kitty instances.' + ' Empty values cause the environment variable to be removed.' + ) + argspec = 'env_var1=val env_var2=val ...' + + def message_to_kitty(self, global_opts: RCOptions, opts: Any, args: ArgsType) -> PayloadType: + if len(args) < 1: + self.fatal('Must specify at least one env var to set') + env = {} + for x in args: + key, val = x.split('=', 1) + env[key] = val + return {'env': env} + + def response_from_kitty(self, boss: Boss, window: Optional[Window], payload_get: PayloadGetType) -> ResponseType: + from kitty.child import default_env, set_default_env + from kitty.utils import expandvars + new_env = payload_get('env') or {} + env = default_env().copy() + for k, v in new_env.items(): + if v: + env[k] = expandvars(v, env) + else: + env.pop(k, None) + set_default_env(env) + + +env = Env()