From 1c44da2b4aeb202fb7c990705880181c2c44d4fd Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 13 Sep 2022 17:24:11 +0530 Subject: [PATCH] Nicer error message when trying to open the config file and no editor is available --- kitty/boss.py | 4 +--- kitty/entry_points.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/kitty/boss.py b/kitty/boss.py index 7cd41d77a..5dd538ad7 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -1522,9 +1522,7 @@ class Boss: @ac('misc', 'Edit the kitty.conf config file in your favorite text editor') def edit_config_file(self, *a: Any) -> None: confpath = prepare_config_file_for_editing() - # On macOS vim fails to handle SIGWINCH if it occurs early, so add a - # small delay. - cmd = [kitty_exe(), '+runpy', 'import os, sys, time; time.sleep(0.05); os.execvp(sys.argv[1], sys.argv[1:])'] + get_editor(get_options()) + [confpath] + cmd = [kitty_exe(), '+edit'] + get_editor(get_options()) + [confpath] self.new_os_window(*cmd) def run_kitten_with_metadata( diff --git a/kitty/entry_points.py b/kitty/entry_points.py index 8813e6d22..f210e4025 100644 --- a/kitty/entry_points.py +++ b/kitty/entry_points.py @@ -81,6 +81,25 @@ def launch(args: List[str]) -> None: runpy.run_path(exe, run_name='__main__') +def edit(args: List[str]) -> None: + import shutil + from .constants import is_macos + if is_macos: + # On macOS vim fails to handle SIGWINCH if it occurs early, so add a small delay. + import time + time.sleep(0.05) + exe = args[1] + if not os.path.isabs(exe): + exe = shutil.which(exe) or '' + if not exe or not os.access(exe, os.X_OK): + print('Cannot find an editor on your system. Set the \x1b[33meditor\x1b[39m value in kitty.conf' + ' to the absolute path of your editor of choice.', file=sys.stderr) + from kitty.utils import hold_till_enter + hold_till_enter() + raise SystemExit(1) + os.execv(exe, args[1:]) + + def shebang(args: List[str]) -> None: script_path = args[1] cmd = args[2:] @@ -152,6 +171,7 @@ namespaced_entry_points['open'] = open_urls namespaced_entry_points['kitten'] = run_kitten namespaced_entry_points['edit-config'] = edit_config_file namespaced_entry_points['shebang'] = shebang +namespaced_entry_points['edit'] = edit def setup_openssl_environment(ext_dir: str) -> None: