Add +python-{ipython,jupyter}-command vars for REPLs

This commit is contained in:
Henrik Lissner 2020-08-25 21:22:56 -04:00
parent 76eae7bc46
commit 1c99aed0c0
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 34 additions and 28 deletions

View file

@ -1,5 +1,25 @@
;;; lang/python/autoload/python.el -*- lexical-binding: t; -*-
;;;###autoload
(defun +python-executable-find (exe)
"Resolve the path to the EXE executable.
Tries to be aware of your active conda/pipenv/virtualenv environment, before
falling back on searching your PATH."
(if (file-name-absolute-p exe)
(and (file-executable-p exe)
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 (require 'conda nil t)
(let ((bin (expand-file-name (concat conda-env-current-name "/" exe-root)
(conda-env-default-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))))))
;;;###autoload
(defun +python/open-repl ()
"Open the Python REPL."
@ -25,8 +45,11 @@
"Open an IPython REPL."
(interactive)
(require 'python)
(let ((python-shell-interpreter (or (+python-executable-find "ipython") "ipython"))
(python-shell-interpreter-args (string-join +python-ipython-repl-args " ")))
(let ((python-shell-interpreter
(or (+python-executable-find (car +python-ipython-command))
"ipython"))
(python-shell-interpreter-args
(string-join (cdr +python-ipython-command) " ")))
(+python/open-repl)))
;;;###autoload
@ -35,28 +58,13 @@
(interactive)
(require 'python)
(add-to-list 'python-shell-completion-native-disabled-interpreters "jupyter")
(let ((python-shell-interpreter (or (+python-executable-find "jupyter") "jupyter"))
(python-shell-interpreter-args (format "console %s" (string-join +python-jupyter-repl-args " "))))
(let ((python-shell-interpreter
(or (+python-executable-find (car +python-jupyter-command))
"jupyter"))
(python-shell-interpreter-args
(string-join (cdr +python-jupyter-command) " ")))
(+python/open-repl)))
;;;###autoload
(defun +python-executable-find (exe)
"TODO"
(if (file-name-absolute-p exe)
(and (file-executable-p exe)
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 (require 'conda nil t)
(let ((bin (expand-file-name (concat conda-env-current-name "/" exe-root)
(conda-env-default-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))))))
;;;###autoload
(defun +python/optimize-imports ()
"organize imports"