From da15477aaf9fea9e64deaad01a57d21438493112 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 25 Jul 2018 08:57:44 +0530 Subject: [PATCH] Make the choice of default editor a bit more robust --- kitty/utils.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/kitty/utils.py b/kitty/utils.py index 3a58532d0..3585811b8 100644 --- a/kitty/utils.py +++ b/kitty/utils.py @@ -404,6 +404,26 @@ def natsort_ints(iterable): return sorted(iterable, key=alphanum_key) +def exe_exists(exe): + for loc in os.environ.get('PATH', '').split(os.pathsep): + if loc and os.access(os.path.join(loc, exe), os.X_OK): + return os.path.join(loc, exe) + return False + + def get_editor(): - import shlex - return shlex.split(os.environ.get('EDITOR', 'vim')) + ans = getattr(get_editor, 'ans', False) + if ans is False: + import shlex + ans = os.environ.get('EDITOR') + if not ans or not exe_exists(shlex.split(ans)[0]): + for q in ('vim', 'vi', 'emacs', 'micro', 'nano'): + r = exe_exists(q) + if r: + ans = r + break + else: + ans = 'vim' + ans = shlex.split(ans) + get_editor.ans = ans + return ans