lang/python: fix conda/virtualenv modeline segment

Reported by @ztlevi
This commit is contained in:
Henrik Lissner 2019-03-04 20:48:52 -05:00
parent 5bae67e299
commit f515bf5931
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 30 additions and 3 deletions

View file

@ -8,7 +8,7 @@
(user-error "`python-shell-interpreter' isn't set")) (user-error "`python-shell-interpreter' isn't set"))
(pop-to-buffer (pop-to-buffer
(process-buffer (process-buffer
(if-let* ((pipenv (executable-find "pipenv")) (if-let* ((pipenv (+python-executable-find "pipenv"))
(pipenv-project (pipenv-project-p))) (pipenv-project (pipenv-project-p)))
(let ((default-directory pipenv-project) (let ((default-directory pipenv-project)
(python-shell-interpreter-args (python-shell-interpreter-args
@ -23,7 +23,7 @@
(defun +python/open-ipython-repl () (defun +python/open-ipython-repl ()
"Open an IPython REPL." "Open an IPython REPL."
(interactive) (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-shell-interpreter-args (string-join +python-ipython-repl-args " ")))
(+python/open-repl))) (+python/open-repl)))
@ -32,6 +32,23 @@
"Open a Jupyter console." "Open a Jupyter console."
(interactive) (interactive)
(add-to-list 'python-shell-completion-native-disabled-interpreters "jupyter") (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-shell-interpreter-args (format "console %s" (string-join +python-jupyter-repl-args " "))))
(+python/open-repl))) (+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))))))

View file

@ -52,6 +52,16 @@ called.")
sp-point-after-word-p sp-point-after-word-p
sp-point-before-same-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)) (setq-hook! 'python-mode-hook tab-width python-indent-offset))