From 345b3437f48f36fc3b8b547c9c71fceac5e3e769 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 28 May 2018 12:24:09 +0530 Subject: [PATCH] Add a config option to set the EDITOR kitty uses Useful on systems that make it hard to set system-wide environment variables. See #580 --- kittens/unicode_input/main.py | 5 +++-- kitty/boss.py | 6 +++--- kitty/constants.py | 2 -- kitty/kitty.conf | 7 +++++++ kitty/main.py | 2 ++ kitty/utils.py | 5 +++++ 6 files changed, 20 insertions(+), 7 deletions(-) diff --git a/kittens/unicode_input/main.py b/kittens/unicode_input/main.py index 48529296b..697d7252d 100644 --- a/kittens/unicode_input/main.py +++ b/kittens/unicode_input/main.py @@ -9,7 +9,8 @@ from functools import lru_cache from gettext import gettext as _ from kitty.config import cached_values_for -from kitty.constants import config_dir, editor +from kitty.constants import config_dir +from kitty.utils import get_editor from kitty.fast_data_types import wcswidth from kitty.key_encoding import ( DOWN, ESCAPE, F1, F2, F3, F4, F12, LEFT, RELEASE, RIGHT, SHIFT, TAB, UP, @@ -426,7 +427,7 @@ class UnicodeInput(Handler): with open(favorites_path, 'wb') as f: f.write(serialize_favorites(load_favorites()).encode('utf-8')) with self.suspend(): - p = subprocess.Popen(editor + [favorites_path]) + p = subprocess.Popen(get_editor() + [favorites_path]) if p.wait() == 0: load_favorites(refresh=True) self.init_terminal_state() diff --git a/kitty/boss.py b/kitty/boss.py index c341ff9da..dbe89ebae 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -17,7 +17,7 @@ from .config import ( ) from .config_utils import to_cmdline from .constants import ( - appname, config_dir, editor, set_boss, supports_primary_selection + appname, config_dir, set_boss, supports_primary_selection ) from .fast_data_types import ( ChildMonitor, background_opacity_of, change_background_opacity, @@ -32,7 +32,7 @@ from .rgb import Color, color_from_int from .session import create_session from .tabs import SpecialWindow, SpecialWindowInstance, TabManager from .utils import ( - get_primary_selection, log_error, open_url, parse_address_spec, + get_editor, get_primary_selection, log_error, open_url, parse_address_spec, remove_socket_file, safe_print, set_primary_selection, single_instance, startup_notification_handler ) @@ -538,7 +538,7 @@ class Boss: confpath = prepare_config_file_for_editing() # On macOS vim fails to handle SIGWINCH if it occurs early, so add a # small delay. - cmd = ['kitty', '+runpy', 'import os, sys, time; time.sleep(0.05); os.execvp(sys.argv[1], sys.argv[1:])'] + editor + [confpath] + cmd = ['kitty', '+runpy', 'import os, sys, time; time.sleep(0.05); os.execvp(sys.argv[1], sys.argv[1:])'] + get_editor() + [confpath] self.new_os_window(*cmd) def get_output(self, source_window, num_lines=1): diff --git a/kitty/constants.py b/kitty/constants.py index 5d325d4a6..230925535 100644 --- a/kitty/constants.py +++ b/kitty/constants.py @@ -4,7 +4,6 @@ import os import pwd -import shlex import sys from collections import namedtuple @@ -16,7 +15,6 @@ str_version = '.'.join(map(str, version)) _plat = sys.platform.lower() is_macos = 'darwin' in _plat base = os.path.dirname(os.path.abspath(__file__)) -editor = shlex.split(os.environ.get('EDITOR', 'vim')) ScreenGeometry = namedtuple('ScreenGeometry', 'xstart ystart xnum ynum dx dy') diff --git a/kitty/kitty.conf b/kitty/kitty.conf index ec0067764..07d858cc6 100644 --- a/kitty/kitty.conf +++ b/kitty/kitty.conf @@ -314,6 +314,13 @@ color15 #ffffff # ensure that the shell starts in interactive mode and reads its startup rc files. shell . +# The console editor to use when editing the kitty config file or similar +# tasks. A value of . means to use the environment variable EDITOR. Note that +# this environment variable has to be set not just in your shell startup +# scripts but system-wide, otherwise kitty will not see it. +editor . + +# # Close the window when the child process (shell) exits. If no (the default), # the terminal will remain open when the child exits as long as there are still # processes outputting to the terminal (for example disowned or backgrounded diff --git a/kitty/main.py b/kitty/main.py index 4d660b478..5570a137e 100644 --- a/kitty/main.py +++ b/kitty/main.py @@ -175,6 +175,8 @@ def _main(): single_instance.socket.sendall(data) return opts = create_opts(args) + if opts.editor != '.': + os.environ['EDITOR'] = opts.editor init_graphics() try: with setup_profiling(args): diff --git a/kitty/utils.py b/kitty/utils.py index 3b48df120..458ed7299 100644 --- a/kitty/utils.py +++ b/kitty/utils.py @@ -392,3 +392,8 @@ def read_with_timeout(more_needed, timeout=10, src=sys.stdin.buffer): break else: break + + +def get_editor(): + import shlex + return shlex.split(os.environ.get('EDITOR', 'vim'))