From f515bf5931f9142f87c109a6acf7041bd2a6d2bf Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Mon, 4 Mar 2019 20:48:52 -0500 Subject: [PATCH] lang/python: fix conda/virtualenv modeline segment Reported by @ztlevi --- modules/lang/python/autoload/python.el | 23 ++++++++++++++++++++--- modules/lang/python/config.el | 10 ++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/modules/lang/python/autoload/python.el b/modules/lang/python/autoload/python.el index 27ed691a3..753f0f636 100644 --- a/modules/lang/python/autoload/python.el +++ b/modules/lang/python/autoload/python.el @@ -8,7 +8,7 @@ (user-error "`python-shell-interpreter' isn't set")) (pop-to-buffer (process-buffer - (if-let* ((pipenv (executable-find "pipenv")) + (if-let* ((pipenv (+python-executable-find "pipenv")) (pipenv-project (pipenv-project-p))) (let ((default-directory pipenv-project) (python-shell-interpreter-args @@ -23,7 +23,7 @@ (defun +python/open-ipython-repl () "Open an IPython REPL." (interactive) - (let ((python-shell-interpreter "ipython") + (let ((python-shell-interpreter (or (+python-executable-find "ipython") "ipython")) (python-shell-interpreter-args (string-join +python-ipython-repl-args " "))) (+python/open-repl))) @@ -32,6 +32,23 @@ "Open a Jupyter console." (interactive) (add-to-list 'python-shell-completion-native-disabled-interpreters "jupyter") - (let ((python-shell-interpreter "jupyter") + (let ((python-shell-interpreter (or (+python-executable-find "jupyter") "jupyter")) (python-shell-interpreter-args (format "console %s" (string-join +python-jupyter-repl-args " ")))) (+python/open-repl))) + +;;;###autoload +(defun +python-executable-find (exe) + "TODO" + (if (file-name-absolute-p exe) + (file-executable-p exe) + (let ((exe-root (format "bin/%s" exe))) + (cond ((when python-shell-virtualenv-root + (let ((bin (expand-file-name exe-root python-shell-virtualenv-root))) + (if (file-exists-p bin) bin)))) + ((when (bound-and-true-p conda-env-current-name) + (let ((bin (expand-file-name (concat conda-env-current-name "/" exe-root) + (conda-env-location)))) + (if (file-executable-p bin) bin)))) + ((when-let* ((bin (projectile-locate-dominating-file default-directory "bin/python"))) + (setq-local doom-modeline-python-executable (expand-file-name "bin/python" bin)))) + ((executable-find exe)))))) diff --git a/modules/lang/python/config.el b/modules/lang/python/config.el index 4da346a20..abd46e591 100644 --- a/modules/lang/python/config.el +++ b/modules/lang/python/config.el @@ -52,6 +52,16 @@ called.") sp-point-after-word-p sp-point-before-same-p)) + (defun +python|detect-local-python-executable () + (let ((path (+python-executable-find "python"))) + (when path + (make-local-variable 'exec-path) + (add-to-list 'exec-path path)) + (when (bound-and-true-p doom-modeline-mode) + (setq-local doom-modeline-python-executable + (or path python-shell-interpreter))))) + (add-hook 'python-mode-hook #'+python|detect-local-python-executable) + (setq-hook! 'python-mode-hook tab-width python-indent-offset))