From 50e7351c73c08b4fe3937f02fc6b52e66987b673 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 22 Jul 2019 09:29:55 +0530 Subject: [PATCH] When detecting the EDITOR from the environment, if the EDITOR env var is not present, try to read it from the shell's rc files by executing a login shell and dumping its environment macOS makes it unreasonably hard to set system-wide environment variables, so this hack might be useful there. --- kitty/main.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/kitty/main.py b/kitty/main.py index 2c5de5397..a6fc26dcf 100644 --- a/kitty/main.py +++ b/kitty/main.py @@ -203,9 +203,31 @@ def macos_cmdline(argv_args): return ans +def read_shell_environment(opts): + if not hasattr(read_shell_environment, 'ans'): + import subprocess + from .session import resolved_shell + shell = resolved_shell(opts) + p = subprocess.Popen(shell + ['-l', '-c', 'env'], stdout=subprocess.PIPE) + raw = p.stdout.read() + if p.wait() == 0: + raw = raw.decode('utf-8', 'replace') + ans = read_shell_environment.ans = {} + for line in raw.splitlines(): + k, v = line.partition('=')[::2] + if k and v: + ans[k] = v + return read_shell_environment.ans + + def setup_environment(opts, args): extra_env = opts.env.copy() - if opts.editor != '.': + if opts.editor == '.': + if 'EDITOR' not in os.environ: + shell_env = read_shell_environment(opts) + if 'EDITOR' in shell_env: + os.environ['EDITOR'] = shell_env['EDITOR'] + else: os.environ['EDITOR'] = opts.editor if args.listen_on: os.environ['KITTY_LISTEN_ON'] = args.listen_on